n8n's `$env` variable (available when `N8N_BLOCK_ENV_ACCESS_IN_NODE` is not set to `true`) lets expressions read environment variables from the host system.
n8n's $env variable (available when N8N_BLOCK_ENV_ACCESS_IN_NODE is not set to true) lets expressions read environment variables from the host system. This allows you to define API endpoints, feature flags, and configuration values as environment variables and switch between test and production by changing the environment, not the workflow.
Real-world example: Your workflow calls a payment processing API that has separate sandbox and production URLs. You want the same workflow to target sandbox in development and production in live.
services:
n8n:
environment:
PAYMENT_API_BASE_URL: "https://api.sandbox.paymentprovider.com/v1"
PAYMENT_API_MODE: "test"
NOTIFICATION_EMAIL: "dev-team@example.com"
```text
```yaml title="docker-compose.yml - Production environment"
services:
n8n:
environment:
PAYMENT_API_BASE_URL: "https://api.paymentprovider.com/v1"
PAYMENT_API_MODE: "live"
NOTIFICATION_EMAIL: "billing-alerts@example.com"
```text
```text title="HTTP Request Node Configuration"
URL: {{ $env.PAYMENT_API_BASE_URL }}/charges
Method: POST
```text
```text title="IF Node - Conditional Logic Based on Environment"
Condition:
{{ $env.PAYMENT_API_MODE }} equals "test"
True branch: Log the request (do not actually charge)
False branch: Process the real payment
```text
> **Warning: Security Consideration**
>
> By default, `$env` exposes all host environment variables to expressions, including secrets like `POSTGRES_PASSWORD`. In production, set `N8N_BLOCK_ENV_ACCESS_IN_NODE=true` and use n8n credentials for sensitive values. Use `$env` only for non-secret configuration like URLs, feature flags, and mode switches.
This approach keeps your workflow definition identical across environments. Promotion from staging to production requires zero workflow changes.
**Related:** [Always Set an Error Workflow on Every Production Workflow](../error-handling-and-reliability/01-always-set-an-error-workflow-on-every-production-workflow.md) | [Break Large Workflows into Sub-Workflows](../workflow-architecture/01-break-large-workflows-into-sub-workflows.md)
I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.