A concise guide on effectively using environment variables in Netlify functions.

To utilize environment variables in your Netlify Functions, you can access the process.env variable:

process.env.YOUR_VARIABLE

To enhance code readability, you can use object destructuring at the beginning of your JavaScript file:

const { YOUR_VARIABLE } = process.env;

Then, you can simply use the YOUR_VARIABLE throughout the rest of your program.

To set the variables, you can do so through the Netlify administration interface (although you can also add them in your repository, it is recommended to use the Netlify UI to avoid exposing secrets in your Git repository).

Please note that this method works specifically for Netlify “regular” Functions that operate on AWS Lambda. If you are using Netlify Edge Functions, you need to utilize Deno.env.get() as follows:

Deno.env.get('YOUR_VARIABLE')

For instance:

export default () => new Response(Deno.env.get('YOUR_VARIABLE'))