├── .gitignore ├── LICENSE ├── README.md ├── apikey.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.env 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) ''2019'', ''Peter Kay'' 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 | OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # API Keys on the Backend 2 | created by Peter Kay 3 | 4 | ## Introduction 5 | 6 | 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! 7 | 8 | ## How to Run 9 | 10 | As an example, this backend uses the openweather API to demonstrate its function. 11 | 12 | 1. `git clone https://github.com/deuscode/API-Key-Backend.git` 13 | 2. `cd API-Key-Backend` and `npm install` 14 | 3. Go to `openweathermap.org` and create an account & API Key 15 | 4. Create a .env file and store your Open Weather API Key within it, like this: 16 | 17 | ``` 18 | WEATHER_KEY=pasteyourapikeyhere12345678thecakeisalie 19 | ``` 20 | 5. Then run `node apikey.js` 21 | 6. In your browser, go to `localhost:11646/weather/insertLAT/insertLON` to get a JSON response from the openweather API! 22 | 23 | Example: 24 | `localhost:11646/weather/38/139` or `localhost:11646/forecast/38/139` 25 | 26 | ### Note 27 | You can specify whatever port you require: 28 | ``` 29 | app.listen(PORT_NUMBER, function () { 30 | console.log("I'm alive!"); 31 | }) 32 | ``` 33 | 34 | 35 | ## API Calls 36 | 1. 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: 37 | 38 | ``` 39 | var apiURL = `http://pathtoAPI.org/data/whatever/api?&APPID=${process.env.ENTRYINDOTENVFILE}` 40 | ``` 41 | 42 | 2. CORS has been enabled with the following: 43 | 44 | ``` 45 | app.use(function(req, res, next) { 46 | res.header('Access-Control-Allow-Origin', '*'); 47 | res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); 48 | res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 49 | next(); 50 | }) 51 | ``` 52 | 53 | 3. Make sure to specify the path to your .env file within your dotenv require module: 54 | 55 | ``` 56 | require('dotenv').config({path: 'your_env_file.env'}); 57 | ``` 58 | 59 | ## Hosting 60 | 1. You may host this with any webhost that supports node.js 61 | 2. Make sure you install the npm modules 62 | 3. Check with your host's software documentation for more info 63 | 64 | ## Thank you! 65 | 66 | If you have any questions, feel free to e-mail me: `contact@kaypeter.com` 67 | -------------------------------------------------------------------------------- /apikey.js: -------------------------------------------------------------------------------- 1 | // specify env file path within the dotenv require module 2 | require('dotenv').config({path: 'keys.env'}); 3 | var fetch = require('isomorphic-fetch'); 4 | var express = require('express'); 5 | var app = express(); 6 | 7 | // set variables with API call URL 8 | var weatherURL = `http://api.openweathermap.org/data/2.5/weather?units=imperial&APPID=${process.env.WEATHER_KEY}`; 9 | var forecastURL = `http://api.openweathermap.org/data/2.5/forecast?units=imperial&APPID=${process.env.WEATHER_KEY}`; 10 | var twitchURL = `https://api.twitch.tv/kraken/channels/`; 11 | var twitchURLStreams = 'https://api.twitch.tv/kraken/streams/'; 12 | 13 | // enable CORS for cross-browser support 14 | app.use(function(req, res, next) { 15 | res.header('Access-Control-Allow-Origin', '*'); 16 | res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); 17 | res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 18 | next(); 19 | }); 20 | 21 | // populate express get methods for JSON fetch 22 | app.get('/weather/:lat/:lon', function (req, res) { 23 | fetch(`${weatherURL}&lat=${req.params.lat}&lon=${req.params.lon}`) 24 | .then(response => response.json()) 25 | .then(weather => res.send(weather)) 26 | .catch(error => res.send(error)) 27 | }); 28 | app.get('/forecast/:lat/:lon', function (req, res) { 29 | fetch(`${forecastURL}&lat=${req.params.lat}&lon=${req.params.lon}`) 30 | .then(response => response.json()) 31 | .then(forecast => res.send(forecast)) 32 | .catch(error => res.send(error)) 33 | }); 34 | app.get('/channels/:channel', function (req, res) { 35 | fetch(`${twitchURL}${req.params.channel}?client_id=${process.env.TWITCH_KEY}`) 36 | .then(response => response.json()) 37 | .then(stream => res.send(stream)) 38 | .catch(error => res.send(error)) 39 | }); 40 | app.get('/streams/:stream', function (req, res) { 41 | fetch(`${twitchURLStreams}${req.params.stream}?client_id=${process.env.TWITCH_KEY}`) 42 | .then(response => response.json()) 43 | .then(stream => res.send(stream)) 44 | .catch(error => res.send(error)) 45 | }); 46 | 47 | // verify server is running 48 | app.listen(11646, function () { 49 | console.log("I'm alive! Call me at 11646!"); 50 | }); 51 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "apikeybackend", 3 | "version": "1.0.0", 4 | "description": "API Key Server", 5 | "main": "apikey.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node apikey.js" 9 | }, 10 | "dependencies": { 11 | "dotenv": "^4.0.0", 12 | "express": "^4.15.3", 13 | "isomorphic-fetch": "^2.2.1" 14 | }, 15 | "author": "Peter Kay", 16 | "license": "ISC" 17 | } 18 | --------------------------------------------------------------------------------