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

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
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.
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.
docker-compose.yml or docker run command.
Below are the real, production-safe ways to set environment variables depending on where n8n is running.
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"
Useful for quick dev setups:
docker run -it --rm
-e MY\_VARIABLE=hello-world
-p 5678:5678
n8nio/n8n
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.
Go to Settings → Environment Variables → Add Variable. n8n Cloud restarts your workspace automatically so the new values take effect.
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:
Example using it inside a Code node:
return [
{
apiKey: $env.MY_API_KEY, // safely loaded from environment
}
];
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.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.