Fix · Environment variables missing or empty in production: fixes

Netlify environment variables undefined in functions

In production, a Netlify function reads process.env.MY_KEY as undefined, or gets a masked value, although the same code works locally. Common causes: a scope that does not include Functions, a value set after the deploy, or a variable declared in netlify.toml.

Likely causes, most common first

Cause 1

The variable's scope does not include Functions

On Pro and Enterprise plans, a variable can be limited to scopes such as Builds, Functions or Runtime. A variable scoped to Builds is visible to the build but not to the function at runtime.

How to tell: The variable is listed for the Builds scope but not for the Functions scope.

Cause 2

The value was added or changed after the deploy that is running

Each deploy, including Deploy Previews and branch deploys, uses the variable values that were set when it was deployed. Adding a variable in the UI does not change a deploy that already exists.

How to tell: The variable appears in the UI, but the running deploy is older than the change.

Cause 3

The variable lives in netlify.toml

Variables declared in netlify.toml are not available to serverless functions. They can look correct, because the value is in the file and in the build. The CLI's env:list is documented as including netlify.toml values, so it can show the key too.

How to tell: The function logs the key as unset, and the key is declared in netlify.toml with no matching site variable that includes the Functions scope in the UI.

Cause 4

A secret is masked outside Netlify

Secret values are write-only, and code running outside Netlify's hosted systems gets masked versions. Secret values must also be set to explicit deploy contexts and scopes.

How to tell: The value is a masked placeholder instead of your real key, while non-secret variables resolve.

Check and fix it, step by step

  1. Log whether the function sees the variable

    Log a boolean, not the value, and read it with process.env. Trigger the function on the deploy that fails, then check the function logs.

    export default async () => {
      console.log("MY_KEY set:", Boolean(process.env.MY_KEY))
      return new Response("ok")
    }

    Docs: docs.netlify.com →

  2. List the resolved variables and check where each one comes from

    env:list is documented as including netlify.toml values, so a key shown here may come from the file rather than from a site variable with the Functions scope. Compare the output with the variable's entry in the UI. The log from step 1 shows what the function actually receives.

    netlify env:list --scope functions --context production

    Docs: cli.netlify.com →

  3. Check the scope includes Functions

    On Pro and above, open the variable under Project configuration > Environment variables and confirm its scopes include Functions. A variable limited to Builds does not reach the function at runtime.

    Docs: docs.netlify.com →

  4. Move netlify.toml values into the UI or CLI

    If the key is declared in netlify.toml, set it as a site variable with the Functions scope instead. The command below uses a placeholder. Keep real values out of the repository.

    netlify env:set MY_KEY <value> --scope functions --context production

    Docs: docs.netlify.com →

  5. Check secrets run on Netlify and are scoped explicitly

    Set secret variables with explicit contexts and scopes, and test them from a deploy hosted on Netlify. A local netlify build does not include the raw values, and the secret flag cannot be removed once set.

    Docs: docs.netlify.com →

  6. Create a new deploy after any change

    Changes to variables take effect on the next build and deploy. Retest the function on the new deploy, not the old one.

    Docs: docs.netlify.com →

Quick check: grep -n MY_KEY netlify.toml; check the function log for Boolean(process.env.MY_KEY) on the failing deploy

How often this shows up in our data

4 of the 937 verified cases from the last 12 months in our data match this symptom (0.4%). How we collect and verify cases.

Questions

Do variables in netlify.toml reach my Netlify functions?

No. The Netlify docs state that variables declared in netlify.toml are not available to serverless functions, so set them in the UI, CLI or API with the Functions scope.


Do I need to redeploy after adding an environment variable?

Yes. Each deploy uses the values that were set when it was deployed, so create a new deploy to apply updated values.


Why does my secret variable look masked?

Secret values are write-only, and code running outside Netlify's hosted systems gets masked versions. Check that the function runs on a Netlify deploy and that the secret is set to explicit deploy contexts and scopes.


Is there a size limit on environment variables in functions?

In Lambda compatibility mode, AWS's limit applies: the total size of all environment variables cannot exceed 4 KB.


Sources

Every cause and step above was checked against these pages on 27 Oct 2026.

The broader pattern

This is one symptom of a wider failure pattern: Environment variables missing or empty in production: fixes. The guide covers every cause we see for it, on any stack.

Get a heads-up when Netlify breaks something

Netlify

Leave your email and we'll let you know when something big changes for Netlify. Unsubscribe any time by replying. Gemmein Limited. Research terms · Privacy

← All fixes