How to use environment variables in Netlify functions
By Flavio Copes
Learn how to read environment variables in Netlify Functions through process.env, where to set them, and how Edge Functions read them with Netlify.env.get.
To use environment variables in your Netlify Functions, read them from process.env, like in any Node.js program:
process.env.STRIPE_SECRET_KEY
Environment variables are how you keep secrets out of your code. API keys, database URLs, tokens: they all live in the environment, and the function reads them at runtime.
You can use object destructuring at the beginning of your JS file, to make the code nicer:
const { STRIPE_SECRET_KEY } = process.env
so you can just use STRIPE_SECRET_KEY in the rest of the program.
Here’s a full function that uses one. Save it as netlify/functions/orders.mjs:
export default async (req, context) => {
const { API_TOKEN } = process.env
const response = await fetch('https://api.airtable.com/v0/appXyz/Orders', {
headers: { Authorization: `Bearer ${API_TOKEN}` }
})
return Response.json(await response.json())
}
This is the current Functions syntax. The default export receives a standard Request and returns a standard Response.
In older functions you’ll see exports.handler returning an object with statusCode and body. That’s the Lambda-compatible mode, and it still works. process.env reads the same way in both.
Where do you set the variables?
You set them through the Netlify administration interface, under your site’s environment variables settings.
On the Pro plan and above you can also choose the scopes of each variable. Make sure the Functions scope is selected, or your function won’t see the variable.
You might also find variables declared in netlify.toml. Those are only available while the site builds. Functions and Edge Functions can’t read them at runtime, so set the variables your functions need in the Netlify UI.
There’s a second reason to keep secrets out of that file: it’s committed to Git, so anyone with repo access can read it.
One thing that catches people: values in process.env are always strings. If you store MAX_ITEMS=50, comparing it to a number needs a conversion first, with parseInt() or Number().
Why is my variable undefined?
The most common cause: you added or changed the variable in the UI, but the running functions still have the old environment. Variables are applied when the site deploys, so trigger a new deploy after changing them.
Check the scope too. A variable without the Functions scope stays undefined inside your function.
When testing locally, run your site with the Netlify CLI (netlify dev). It injects the variables from your site settings, so the function behaves like it does in production.
If your site uses a Vite-based framework, the @netlify/vite-plugin package does the same inside the regular Vite dev server.
What about Edge Functions?
Edge Functions don’t run on Node.js. They run on Deno, and Netlify gives them their own API to read variables:
Netlify.env.get('API_TOKEN')
Example:
export default () => new Response(Netlify.env.get('API_TOKEN'))
If you need all of them at once, Netlify.env.toObject() returns them as an object.
Older examples use Deno.env.get() instead. Netlify.env is the API Netlify documents now.
Same variables, same settings page. Only the way you read them changes.
Want me to talk about your product? You can sponsor this site.
Related posts about services: