How to use environment variables in Netlify functions

How to use environment variables in Netlify functions

Perttu Lähteenlahti
3 min read
netlify-functionsenvironment-variablesserverlesstypescript
Share:

Why environment variables matter on Netlify

At some point, every real project needs secrets or configuration that should not live in code. API keys, private tokens, feature flags, and environment specific values are all common examples.

Hardcoding those values is risky and hard to maintain. This is why environment variables exist.

Netlify makes it easy to work with environment variables, especially inside Netlify Functions. Once you understand how they work, you can keep your code clean and your secrets safe.

Using environment variables in Netlify Functions

Regular Netlify Functions run on AWS Lambda using Node.js. Because of that, environment variables are available through process.env.

In a Netlify Function written in TypeScript, you can access a variable like this:

process.env.MY_API_KEY

This works exactly the same way it does in any Node.js environment.

A cleaner way using destructuring

If you use the same environment variable in multiple places, it is often nicer to pull it out once at the top of the file.

Here is an example:

const { MY_API_KEY } = process.env

After that, you can simply use MY_API_KEY anywhere in the function:

import { Handler } from "@netlify/functions"

const { MY_API_KEY } = process.env

export const handler: Handler = async () => {
  return {
    statusCode: 200,
    body: `API key length: ${MY_API_KEY?.length ?? 0}`
  }
}

This keeps the code easier to read and avoids repeating process.env everywhere.

Where to set environment variables in Netlify

You can define environment variables in multiple ways, but the recommended approach is using the Netlify dashboard.

In the Netlify admin UI, you can:

  • Add environment variables per site
  • Scope them to specific deploy contexts
  • Rotate secrets without changing code

This keeps sensitive values out of your Git repository, which is always a good idea.

Important note about Netlify Edge Functions

Everything above applies only to regular Netlify Functions.

Netlify Edge Functions are different. They do not run on Node.js. They run on Deno.

Because of that, process.env does not exist in Edge Functions.

Using environment variables in Netlify Edge Functions

In Netlify Edge Functions, you must use the Deno API to read environment variables.

Here is the basic syntax:

Deno.env.get("MY_EDGE_VARIABLE")

A full example Edge Function looks like this:

export default () => {
  const value = Deno.env.get("MY_EDGE_VARIABLE")

  return new Response(value ?? "No environment variable found")
}

If the variable is not set, Deno.env.get() returns undefined, so it is a good idea to handle that case.

Regular Functions vs Edge Functions recap

It is easy to mix these up, so here is a quick summary:

  • Netlify Functions use Node.js
  • Netlify Functions read variables from process.env
  • Netlify Edge Functions use Deno
  • Netlify Edge Functions read variables from Deno.env.get()

Knowing which runtime you are using makes everything much clearer.

Common use cases for environment variables on Netlify

Environment variables are commonly used for:

  • API keys and secrets
  • Database connection strings
  • Feature toggles
  • Environment specific configuration
  • Tokens for third party services

Using them properly helps you avoid security issues and makes your project easier to maintain.

Conclusion

Using environment variables in Netlify Functions is simple once you know which runtime you are working with.

For regular Netlify Functions:

  • Use process.env
  • Destructure variables for cleaner code
  • Set values in the Netlify dashboard

For Netlify Edge Functions:

  • Use Deno.env.get()
  • Remember that Node.js APIs are not available

With this setup, you can keep secrets out of your codebase and build safer, more flexible Netlify applications.

Manage Netlify on the go

Download Netli.fyi and monitor your sites, check deploys, and manage your projects from anywhere.

Related articles