created by Peter Kay
I created this simple backend using node.js, with the express framework, to store my API Keys. You will be able to make calls to this backend to get any data without exposing your precious keys. Enjoy!
As an example, this backend uses the openweather API to demonstrate its function.
git clone https://github.com/deuscode/API-Key-Backend.git
cd API-Key-Backend
andnpm install
- Go to
openweathermap.org
and create an account & API Key - Create a .env file and store your Open Weather API Key within it, like this:
WEATHER_KEY=pasteyourapikeyhere12345678thecakeisalie
- Then run
node apikey.js
- In your browser, go to
localhost:11646/weather/insertLAT/insertLON
to get a JSON response from the openweather API!
Example:
localhost:11646/weather/38/139
or localhost:11646/forecast/38/139
You can specify whatever port you require:
app.listen(PORT_NUMBER, function () {
console.log("I'm alive!");
})
- You can create more API calls by adding additional variables to store the API URLs and adding the API Key environment variable to the end:
var apiURL = `http://pathtoAPI.org/data/whatever/api?&APPID=${process.env.ENTRYINDOTENVFILE}`
- CORS has been enabled with the following:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
next();
})
- Make sure to specify the path to your .env file within your dotenv require module:
require('dotenv').config({path: 'your_env_file.env'});
- You may host this with any webhost that supports node.js
- Make sure you install the npm modules
- Check with your host's software documentation for more info
If you have any questions, feel free to e-mail me: contact@kaypeter.com