Last active
January 23, 2021 10:24
-
-
Save marklchaves/7d664f71062447189f4e423d28925bc3 to your computer and use it in GitHub Desktop.
How to read in an environment variable on Netlify using a lambda function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This was a proof of concept for me to be able to | |
* retrieve an API key using a Netlify environment variable | |
* using this lambda function. I spent a lot of time looking | |
* for a complete example like this--without any luck. | |
* | |
* So, here's a step-by-step of what I did. | |
* | |
* 1) For this function to work, define the env variable in | |
* Netlify > Site > Build & Deploy > Environment > Edit Variables | |
* | |
* 2) Place this function in your /functions directory. Add | |
* ./functions to your Site > Functions > Edit Settings | |
* | |
* 3) To find out how to call this, the lambda functions endpoint | |
* will follow this pattern. | |
* | |
* https://yourapp.netlify.com/.netlify/functions/yourfunction | |
* | |
* 4) You can verify this in your Site > Functions tab. | |
* | |
* For my proof of concept, the endpoint was | |
* https://myapp.netlify.com/.netlify/functions/forecast?longitude=40.7720232&latitude=-73.9732319 | |
* | |
* 5) I invoked this endpoint in my app.js code that looked like this. | |
* Notice that I hardcoded the coordinates. I just needed to know if | |
* this would work. And, it did. I stopped here once I proved it worked. | |
* | |
* function getForecastFromNetwork(coords) { | |
* return fetch(`/.netlify/functions/forecast?longitude=40.7720232&latitude=-73.9732319`) | |
* .then((response) => { | |
* return response.json(); | |
* }) | |
* .catch(() => { | |
* return null; | |
* }); | |
* } | |
* | |
* 6) I added `npm install` to my site's build command. | |
*/ | |
const fetch = require('node-fetch'); | |
/* Can do this too. See further below. | |
const { DARKSKY_API_KEY } = process.env; | |
*/ | |
exports.handler = async (event, context) => { | |
try{ | |
const latitude = event.queryStringParameters.latitude; | |
const longitude = event.queryStringParameters.longitude; | |
/** | |
* This lambda (aws) function has access to Netlify build env variables. | |
* That's how process.env.DARKSKY_API_KEY can work. There's no need to | |
* use dotenv or write a .env file to disk. | |
*/ | |
const response = await fetch(`https://api.darksky.net/forecast/${process.env.DARKSKY_API_KEY}/${latitude},${longitude}`); | |
const data = await response.json(); | |
return { | |
statusCode: 200, | |
body: JSON.stringify(data) | |
}; | |
} catch (err) { | |
return { | |
statusCode: 500, | |
body: err.toString() | |
}; | |
} | |
}; |
Hi @mdestafadilah,
Thanks for your comment. I don't have any plans on making a video out of this. I worked on this so long ago, and I've never needed to do it again. I created this gist only to document my proof of concept.
If I make a screencast of this process, I will let you know! :-)
mark
nice here come tobe true ... thanks for your process it.
Thanks for describing the process.
You're welcome @gurdeepsinghsaini :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for describing the process.