Functions /

Environment variables and functions

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

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

# Declare variables

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

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

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

Want to configure your functions? Use build environment variables

This document focuses on how to use environment variables in your functions during runtime. If you need to set specific variables to configure how Netlify builds your functions, leverage build environment variables instead.

# Netlify configuration variables

By setting custom values for certain reserved environment variables, you can change some aspects of your functions.

# Netlify read-only variables

While Netlify offers a number of configuration and read-only environment variables during the build step, only the following read-only variables are available to functions during runtime.

Note that read-only environment variables are reserved in Netlify’s system. You can’t set or override these values manually.

# Functions

The following read-only environment variables are available to serverless functions (including scheduled functions, background functions, and On-demand Builders):

  • SITE_NAME: name of the site, its Netlify subdomain; for example, petsof.
  • SITE_ID: unique ID for the site; for example, 1d01c0c0-4554-4747-93b8-34ce3448ab95.
  • URL: URL representing the main address to your site. It can be either a Netlify subdomain or your own custom domain if you set one; for example, https://petsof.netlify.app orhttps://www.petsofnetlify.com.

Also available are the read-only, reserved runtime environment variables provided by AWS.

Note that other Netlify read-only environment variables are available when you use Netlify Dev to help with local development, but only the above subset are available in all other environments.

# Edge Functions

The following read-only environment variable is available to edge functions. The variable is provided by Deno:

  • DENO_DEPLOYMENT_ID: unique Deno ID of the deployment.

You can also 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.

# Overrides and limitations

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

  • Environment variables declared in a Netlify configuration file (netlify.toml) are not available to functions.
  • Because Netlify Functions are powered by AWS Lambda, AWS’s environment property limits apply to environment variables used with Netlify Functions. If you have the option to set specific scopes for your environment variables, you can adjust the scope of your variables to avoid hitting these limits.
  • In addition to Netlify’s read-only variables, AWS’s reserved environment variables are reserved in Netlify’s system. You can’t create environment variables with a key that’s reserved by a read-only variable. You also can’t set or override these values manually.
  • Changes to environment variables for functions require a require a build and deploy to take effect.

# Access environment variables

Once you declare environment variables for the Functions scope, you can access them in two different ways depending on if you are using serverless functions or edge functions. The following sections include examples of how to access these variables during runtime.

Want to use build environment variables in your functions?

Unlike environment variables set for the Functions scope, variables set for the Build scope are only available during the build step for your site. If you would like to use build environment variables in your functions, you’ll need to embed their values into the functions while your site is building. You can write a script to do this or use a plugin like Netlify Bundle ENV.

# Functions

In your serverless functions (including scheduled functions, background functions, and On-demand Builders), use the format process.env.VARIABLE_NAME to access environment variables available in the functions scope.

import type { Handler, HandlerEvent, HandlerContext } from "@netlify/functions";

const handler: Handler = async (event: HandlerEvent, context: HandlerContext) => {
  
  const value = process.env.MY_IMPORTANT_VARIABLE;

  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Value of MY_IMPORTANT_VARIABLE is ${value}.`}),
  };  
};

export { handler };
exports.handler = async function (event, context) {
  const value = process.env.MY_IMPORTANT_VARIABLE;

  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Value of MY_IMPORTANT_VARIABLE is ${value}.` }),
  };  
};

# Edge Functions

Netlify provides environment variables to 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" },
  });
};
export default async (request, 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" },
  });
};

Using Edge Functions for Middleware with Next.js? Use process.env

If you’re using Netlify Edge Functions for Middleware with your Next.js site, you can also use process.env to access environment variables.

# More environment variables and functions resources