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, such as the Node.js version, leverage build environment variables instead.
# Netlify read-only variables
While Netlify offers a number of configuration and read-only environment variables during the build step, only a subset of these are available to functions during runtime.
# Functions
The following read-only environment variables are available to serverless functions (including scheduled functions, background functions, and On-demand Builders):
NETLIFY_IMAGES_CDN_DOMAIN
: if image compression post processing is enabled, the base URL used for all processed images; for example,d33wubrfki0l68.cloudfront.net
.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 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
Edge Functions are in BETA
.
The following read-only environment variables are available to edge functions, both provided by Deno:
DENO_REGION
: the region code where the deployment is running; for example,us-east1
.DENO_DEPLOYMENT_ID
: unique Deno ID of the deployment.
You can also leverage the Context
object to access Netlify-specific read-only information within your edge functions. For example, the Context
object includes a 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 key 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.
Changes to environment variables for functions require a require a build and deploy to take effect.
Note that if your site uses the classic environment variables experience, atomic deploys don’t apply to environment variables used in edge functions. All edge functions on these sites, including those in previous deploys, will automatically use the most recent environment variable key/value pairs.
If you use the Netlify CLI command
netlify dev
to run edge functions, the CLI will only use environment variable values set using the Netlify UI, CLI, or API. By default,netlify dev
will use values set for theLocal development
deploy context.netlify dev
will not use variables declared in a local.env
file for edge functions. As a workaround, you can use the CLI to import environment variables from your.env
file into Netlify before runningnetlify dev
.
# 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.
Want to use build environment variables in your functions?
By default, build environment variables 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. One way to do this is to 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 { 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
Edge Functions are in BETA
.
Netlify provides environment variables to edge functions using the Deno runtime, so you can access environment variables using the Deno.env
API.
get(key)
: get the value of an environment variabletoObject()
: get all environment variables as an object
Note that you can’t use the Deno set
and 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 "https://edge.netlify.com";
export default async (request: Request, context: Context) => {
const value = Deno.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 = Deno.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
Did you find this doc useful?
Your feedback helps us improve our docs.