Skip to main content

How to use environment variables in Netlify functions

2 min read
How to use environment variables in Netlify functions

When building things using Netlify Functions, there are times you need to access environment variables in your functions. These variables are available at runtime through process.env when using TypeScript or JavaScript:

process.env.VARIABLE_NAME

A cleaner way to access them is to destructure like this:

const { VARIABLE_ONE, VARIABLE_TWO } = process.env

Define these at the root level, and they'll be accessible throughout your function code.

Managing Variables

There are two main ways to manage environment variables in Netlify:

  1. In your repository – Store them in a .env file and use a library like dotenv to load them. However the next way is better as it allows you keep secrets separate from your code.

  2. Through Netlify's UI – The better way is to set them in Netlify's dashboard under Site settings → Environment variables. This keeps them secure and available during builds and runtime.

What you can't do is to use the variables defined in your netlify.toml file.

Local Development

If you need access to environment variables while developing locally, create a .env file at the root of your project:

# .env file
API_KEY=your-api-key

Then access them in your function:

const { API_KEY } = process.env

Run netlify dev to load them automatically.


More to read

How to invoice Apple for App Store proceeds

Apple hands you a CSV, not an invoice. Drop your App Store Connect financial report below and instantly generate the self-issued invoice and itemized report your accountant needs — one PDF, entirely in your browser.

How to check if an iOS app is installed in Expo and React Native

Use Linking.canOpenURL to detect if another app is installed on iOS in Expo. Covers Expo config plugin setup, URL scheme configuration, CNG workflow, and a hook to conditionally show a deep link button.

People found this searching for

  • netlify environment variables functions docs
  • netlify functions environment variables runtime docs
  • netlify functions environment variables available at runtime docs