Skip to content

Unlimited seats on Netlify Pro for $20/month → Learn more 👥

Environment variables and edge functions

Netlify environment variables are accessible when you run Edge Functions. This allows you to securely provide sensitive values for your edge functions to use while they run – values such as API keys and tokens.

This page describes how to create environment variables for edge functions, the specific read-only variables that are available during runtime, and how to access environment variables within your edge functions.

You can declare and set environment variables using the Netlify UI, CLI, or API for use with edge functions. If you have the option to set specific scopes for your environment variables, the scope must include Functions to be available to edge functions during runtime.

Note that environment variables declared in a Netlify configuration file (netlify.toml) are not available to edge functions.

Visit the environment variables overview to learn more about environment variables at Netlify.

You can leverage the Context object to access read-only information within your edge functions. For example, the Context object includes a server property with the region where the deployment is running and a Netlify-specific site property with the site’s id, name, and url.

The general environment variable overrides and limitations apply to environment variables used with edge functions. Here are some additional limitations to note:

  • Environment variables declared in a Netlify configuration file (netlify.toml) are not available to edge functions.
  • Changes to environment variables for edge functions require a build and deploy to take effect.

Once you declare environment variables for the Functions scope, you can access them in your edge functions using the Netlify.env API.

  • get(key): get the value of an environment variable
  • toObject(): get all environment variables as an object

Note that you can’t use the Netlify.env.set and Netlify.env.delete methods to update environment variables from within edge functions. Instead, use the Netlify env API endpoints.

You can also leverage the Netlify-specific context object to access read-only information within your edge functions.

import type { Context } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
const value = Netlify.env.get("MY_IMPORTANT_VARIABLE");
return new Response(`Value of MY_IMPORTANT_VARIABLE for ${context.site.name} is ${value}.`, {
headers: { "content-type": "text/html" },
});
};