/n8n-tutorials

How to set environment variables in n8n?

Learn how to set environment variables in n8n with simple steps to improve workflow control, security, and automation.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to set environment variables in n8n?

To set environment variables in n8n, you add them to the environment where n8n is actually running (for example: Docker container, server shell, or n8n Cloud settings). n8n then automatically picks them up on startup. After they are set, you can use them inside your workflows with expressions like {{$env.MY_VARIABLE}}. n8n does not let you create environment variables from inside the UI — they must be added at the OS/Docker level or in n8n Cloud settings.

 

What “environment variables” mean in n8n

 

Environment variables are simple key‑value settings that n8n reads when it starts. They control things like credentials, webhook URLs, execution limits, and any custom values you want to inject into your workflows.

They are not n8n-specific — they come from the underlying system. n8n just exposes them through $env so a workflow can read them.

  • n8n Cloud: Add them in the Environment Variables panel.
  • Docker: Add them in your docker-compose.yml or docker run command.
  • Bare-metal / local install: Export them in the shell before starting n8n.

 

How to set environment variables in real deployments

 

Below are the real, production-safe ways to set environment variables depending on where n8n is running.

  • 1. Docker Compose
    Add your variables under the environment: section:

    version: "3"
    services:
    n8n:
    image: n8nio/n8n
    environment:
    - N8N\_HOST=n8n.example.com
    - MY_SECRET_TOKEN=abc123
    - NODE\_ENV=production
    ports:
    - "5678:5678"
  • 2. Docker run

    Useful for quick dev setups:

    docker run -it --rm
    -e MY\_VARIABLE=hello-world
    -p 5678:5678
    n8nio/n8n
  • 3. Linux server / local install

    You export the variable in your shell before starting n8n:

    export MY_API_KEY="123456" // set the variable
    n8n start // launch n8n, it now has access

    These variables disappear when the shell session ends unless you put them in ~/.bashrc or a systemd service file.

  • 4. n8n Cloud

    Go to Settings → Environment Variables → Add Variable. n8n Cloud restarts your workspace automatically so the new values take effect.

 

How to use environment variables in your workflows

 

Once the variable exists in the environment and n8n restarted, you read it like any other expression.

{{$env.MY_VARIABLE}}  // returns value you set in Docker/server/Cloud

You can use this in:

  • HTTP Request node URLs or headers
  • Credentials fields (recommended for secrets)
  • Code nodes
  • IF conditions

Example using it inside a Code node:

return [
  {
    apiKey: $env.MY_API_KEY, // safely loaded from environment
  }
];

 

Important production tips

 

  • n8n must restart to load new environment variables.
  • Environment variables override config files if both exist.
  • Keep secrets out of the UI — use environment variables or credentials.
  • Don’t use huge values (MBs of data). Environment variables are for small configuration, not payloads.

 

In short: add the variable to the environment your n8n instance runs on, restart n8n, then reference it with {{$env.YOUR\_VAR}} inside your workflow. This is the correct, production-safe method.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022