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."
- In n8n's source (the
$envprovider in theworkflowpackage), access is blocked unless the variable is exactly the stringfalse. If it isn't set at all,$envis blocked. - The error n8n throws is
access to env vars denied, with the detail "If you need access please contact the administrator to remove the environment variable ‘N8N_BLOCK_ENV_ACCESS_IN_NODE‘". - The environment variable reference describes the setting as "Whether to allow users to access environment variables in expressions and the Code node", so it covers both node parameter expressions and the Code node, not just code.
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.
- The value must be lowercase
false. In the source check,False,0ornoall leave access blocked. - To confirm what the process actually sees, run
docker compose exec n8n envand look for the variable. - Note: in queue mode, workflows run on the worker processes. n8n's queue-mode docs already require workers to share the main instance's encryption key. Set this variable, and any variables your workflows read, on the workers as well.
☐ 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.
- Variables in a
.envfile next todocker-compose.ymlare used for substitution in the Compose file. They only reach the container if the service passes them in, throughenvironment:orenv_file:. - Names are case-sensitive:
$env.api_base_urland$env.API_BASE_URLare different. - Note: forum answers report that the expression preview in the editor can show an error even when the variable is configured. Execute the node and check its output before changing anything else.
☐ 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.
- Anyone who can edit a workflow can read any variable the n8n process has, including database passwords or
N8N_ENCRYPTION_KEYif you set them as environment variables. - The 2.0 breaking-changes page says: "For sensitive data, use credentials or other secure methods instead of environment variables."
- n8n encrypts stored credentials with
N8N_ENCRYPTION_KEY("Provide a custom key used to encrypt credentials in the n8n database"). For an API key, a credential such as Header Auth is the safer place.
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.
- Add an Edit Fields (Set) node right after the trigger. Name it
Config, and give it fields such asapi_base_urlandmodel. - Reference it anywhere later in the workflow with
{{ $('Config').item.json.api_base_url }}. - Keep secrets out of it. Workflow JSON travels with exports and copy-paste, so put keys in credentials.
- When you import a workflow built for self-hosting, replace every
$env.Xwith the matching Config field. Search the workflow JSON for$envbefore you import, so you don't miss one inside a Code node.
☐ 6. Custom variables ($vars) on paid plans
n8n's custom variables are an alternative to $env that you manage in the UI.
- Availability, per the docs: n8n Cloud Pro and Enterprise, self-hosted Business and Enterprise. "Only instance owners and admins can create variables."
- Use them in expressions and the Code node as
$vars.<variable-name>. "All variables are strings." A missing variable isundefined, and the workflow doesn't fail on its own. - Keys and values may only use letters, numbers and underscores. Keys are limited to 50 characters, values to 1,000.
- From n8n 1.118.0, variables can be project-scoped. A project variable with the same key overrides the global one inside that project.
- They're read-only in workflows: you change them 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.
- If you run task runners in external mode, the docs say several runner settings belong in the runners config file (
/etc/n8n-task-runners.json) as anenv-override, not as container environment variables. - Note: the docs don't say whether
N8N_BLOCK_RUNNER_ENV_ACCESSfollows that rule. If setting it on the runner container doesn't work, try the config file.
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
- n8n docs, n8n 2.0 breaking changes (Block environment variable access from Code Node by default): https://docs.n8n.io/2-0-breaking-changes/
- n8n docs, Security environment variables (
N8N_BLOCK_ENV_ACCESS_IN_NODE): https://docs.n8n.io/hosting/configuration/environment-variables/security/ - n8n source,
$envprovider (createEnvProviderState, error text): https://github.com/n8n-io/n8n/blob/master/packages/workflow/src/workflow-data-proxy-env-provider.ts - n8n docs, Custom variables (
$vars, availability, limits, scope): https://docs.n8n.io/code/variables/ - n8n docs, Deployment environment variables (
N8N_ENCRYPTION_KEY): https://docs.n8n.io/hosting/configuration/environment-variables/deployment/ - n8n docs, Queue mode (shared encryption key for workers): https://docs.n8n.io/hosting/scaling/queue-mode/
- n8n docs, Task runner environment variables (
N8N_BLOCK_RUNNER_ENV_ACCESS): https://docs.n8n.io/hosting/configuration/environment-variables/task-runners/ - n8n pull request #39831, scoped
$envallowlist (open): https://github.com/n8n-io/n8n/pull/39831 - n8n community, No access to $env: https://community.n8n.io/t/no-access-to-env/20665
- n8n community, How N8N_BLOCK_ENV_ACCESS_IN_NODE should work?: https://community.n8n.io/t/how-n8n-block-env-access-in-node-should-work/30474
- n8n community, How do I set ENV Variables in n8n cloud?: https://community.n8n.io/t/how-do-i-set-env-variables-in-n8n-cloud/9292
- n8n community, How can I remove the n8n_block_env_access_in_node in cloud?: https://community.n8n.io/t/how-can-i-remove-the-n8n-block-env-access-in-node-in-cloud/28827
- n8n community, How to allow env vars access in set fields or credentials via JS expressions?: https://community.n8n.io/t/how-to-allow-env-vars-access-in-set-fields-or-credentials-via-js-expressions/273453