Tips > Reliability & Performance

Use the `$env` Variable to Switch Between Test and Production Endpoints

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 lets expressions read host environment variables, so you can define API endpoints, feature flags, and configuration as environment variables and switch between test and production by changing the environment rather than the workflow. The workflow definition stays identical across environments, and promotion from staging to production requires zero workflow changes.

What does the $env variable do?

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.

How do you switch endpoints by environment?

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"
services:
  n8n:
    environment:
      PAYMENT_API_BASE_URL: "https://api.paymentprovider.com/v1"
      PAYMENT_API_MODE: "live"
      NOTIFICATION_EMAIL: "billing-alerts@example.com"
URL:    {{ $env.PAYMENT_API_BASE_URL }}/charges
Method: POST
Condition:
  {{ $env.PAYMENT_API_MODE }} equals "test"

True branch:  Log the request (do not actually charge)
False branch: Process the real payment

This approach keeps your workflow definition identical across environments. Promotion from staging to production requires zero workflow changes.

What are the security considerations?

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.

Related: Always Set an Error Workflow on Every Production Workflow · Break Large Workflows into Sub-Workflows

Showcase builds

19 complete workflows from my own projects, each with its n8n workflow JSON to import. Showcase entries link the file at the end of the article.

See the showcase builds

Keep reading

190 entries grouped by topic, from first workflow to queue mode. Free, no signup.

Browse the encyclopedia

Need it built?

I design, build and run n8n systems for clients. Every engagement starts with a $1,500 diagnostic audit, credited toward the build.

Book an introductory call