n8n "access to env vars denied"? Fix $env on Self-Hosted n8n 2.x, and What to Use on n8n Cloud

You put {{ $env.API_BASE_URL }} in an HTTP Request node, or $env.MY_KEY in a Code node, and n8n answers with access to env vars denied. On a fresh self-hosted install that's now the default behaviour, not a misconfiguration. On n8n Cloud there's nothing to switch off at all. This checklist covers why it happens, how to allow $env on your own server, what that exposes, and what to use instead when you can't, or shouldn't, read environment variables from a workflow.

Quick reference

Situation Fix Check
Self-hosted n8n 2.x, $env denied everywhere Set N8N_BLOCK_ENV_ACCESS_IN_NODE=false on the n8n process and restart it 1, 2
Setting changed, still denied Value isn't exactly false, or the container wasn't recreated 2
No error, but the value is empty The variable isn't set in the n8n process itself 3
n8n Cloud Can't change it; use a Config node, $vars or credentials 5, 6
API keys in $env Move them to credentials 4
Python Code node, os.environ empty Separate setting: N8N_BLOCK_RUNNER_ENV_ACCESS 7

☐ 1. Know why the error appears

n8n's 2.0 breaking-changes page: "The default value for N8N_BLOCK_ENV_ACCESS_IN_NODE is now set to true."

Note: the environment variable reference table still lists the default as false, which contradicts the 2.0 breaking-changes page and the current source. The behaviour on 2.x follows the source: unset means blocked.

So if your workflow worked on n8n 1.x and broke after an upgrade to 2.x, this is the most likely reason.

☐ 2. Self-hosted: allow $env (if you decide to)

Set the variable on the n8n process, then recreate it so the new environment is picked up.

Docker Compose:

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    environment:
      - N8N_BLOCK_ENV_ACCESS_IN_NODE=false
      - API_BASE_URL=https://api.example.com

Then run docker compose up -d. Compose recreates the container when its environment changes. A plain docker restart keeps the old environment.

docker run: add -e N8N_BLOCK_ENV_ACCESS_IN_NODE=false and start a new container. You can't add environment variables to an existing container.

☐ 3. Make sure the variable exists where n8n runs

Allowing access doesn't create the variables. In the source, $env.NAME simply returns whatever the n8n process has under NAME, so a missing variable gives an empty value, not an error.

☐ 4. Know what you expose when you turn it off

With access allowed, $env reads from the n8n process's full environment. There's no per-variable allowlist in n8n core: an optional allowlist was proposed in pull request #39831, which was still open as of October 2026.

A reasonable split: non-secret settings (base URLs, model names, limits) in $env or a Config node, and secrets in credentials only.

☐ 5. n8n Cloud: there's no switch

You can't set environment variables on n8n Cloud. In forum answers from 2021 and 2023, n8n team members say this directly: it's "currently not possible to … set environment variables" on n8n Cloud, and "You wouldn't be able to change that setting on n8n cloud, only if you were self-hosting".

Note: those answers are a few years old. Check your Cloud plan's settings, but plan for $env not being available.

The plan-independent option is a Config node.

☐ 6. Custom variables ($vars) on paid plans

n8n's custom variables are an alternative to $env that you manage in the UI.

☐ 7. Python Code node: a separate setting

The task runner docs list N8N_BLOCK_RUNNER_ENV_ACCESS (default true): "Whether to block access to the runner's environment from within Python code tasks." N8N_BLOCK_ENV_ACCESS_IN_NODE doesn't change this.

Symptom table

Symptom Likely cause Check
access to env vars denied after upgrading to 2.x New default blocks $env 1, 2
Still denied after setting the variable Value not exactly false, container not recreated, or set on main but not workers 2
Expression is empty, no error Variable not passed into the container 3
Works when executed, error in the editor preview Preview limitation reported on the forum 3
Template from the internet fails on n8n Cloud It reads $env 5
$vars not available Plan without custom variables 6

Workflows that ship both ways

The Store Support Agent Pack for n8n is a pack of 8 n8n workflow JSON files for online stores: order status replies, ticket triage, FAQ answers, review replies and refund triage. You import them into your own n8n and run them with your own model API key. According to its product page, it comes as two sets. The self-hosted workflows read their settings from environment variables. A separate n8n Cloud set uses credentials and a Config node instead, and was workflow-tested with env access blocked. Its docs cover install, n8n Cloud setup, and the env vars and store API contract. Note: the pack's own env var doc says the self-hosted set needs N8N_BLOCK_ENV_ACCESS_IN_NODE=false on n8n 2.x (check 2). If you'd rather keep $env blocked, use the Cloud set on your own server too. A single scenario is $39 and the full pack is $79. Its listed limits: it was workflow-tested on n8n 1.123.84, and the Shopify adapter is unit-tested against simulated Shopify responses, not yet a live store.

Sources