How to define custom environment variables available in the n8n workflow


I’m searching a way to define variables that could vary through environments (dev, staging, prod°, …)

I don’t know how to do this, and didn’t see anything in the documentation or in the forum.

Maybe I’m not searching with the good words, but, is there a way to do this ?

My need would be to define a base URL, on my PC would be a “192.168.xx.xx”, but on a server, could be in a docker stack, so that could be a string like “api-server”.

This need could be extended to other variables, like paths or files to write, …

Thank for your help

Maybe I’ve found a way to do this with the combination of Read Binary File + Spreadsheet + Function nodes.

I write my settings in a setting.csv file with a setting;value; header.
Each line will contain a key/value pair of setting.

The Read Binary will load the file, the spreadsheet transform in a JSON way, and the Function will “squash” all the lines in a single JSON Object.

What do you think, is it a good way ?

You can just use environment variables. So if you set the environment variable:

export ASDF=test

and you then use the following expression anywhere inside of n8n {{$env['ASDF']}} then it will output test.

1 Like

Is there a place in the documentation where it was written ?

I’m sorry if I missed it.

And if it’s missing, I could make a PR to add some documentation on it, if you guide me where to insert this part.

1 Like

Currently it is only listed very shortly as part of available variables in Expressions here:

1 Like

Is it only support in Function and FunctionItem nodes?
I tried in other nodes, it seems not support {{$env[‘ASDF’]}}

Welcome back @Ho_Tommy

That should work. Perhaps the link below helps to clarify how to use it.

Hi @RicardoE105 ,

I am using docker and there are only Function and FunctionItem nodes can use $env.my_var, other nodes only can access {{$env.NODE_ENV}}.
Please see the image below.

Function Item node:

Other node:

Hi @RicardoE105,
I found it is only the frontend problem. Actually, It can work properly in backend.
Execute Command node front-end showing [not found]:

Execute Command node result showing correctly:

Yes, that is correct. It will only display them correctly if you execute the node. To make it also work without executing the node, we have to always send all environment variables to the UI which we want to avoid.

Maybe a silly question but is it possible to define an environment variable during the workflow?

Also, is it possible to update it without restarting n8n or reloading the workflow (example: store an access token as an environment variable and refresh it)?

Thank you

Hello again, I haven’t found a way to set the environment variables during a workflow run. I have tried execute command, function node and the regular set node and sadly none of them achieve it right as far as I can tell.

I stumbled on this today, just wondering if it could be used in parallel to set the environment variables via the workflow?

Apparently this does not work anymore in the latest n8n (installed as npm)


Hey @sscarduzio,

It looks like using $env["env_name"] still works if that helps.

1 Like

OK wow, it does! But…How is this possible? What data structure is this? “typeof” says it’s a function, so how is it possible that we can access it with square brackets?

Heye @sscarduzio,

When I check typeof it shows an object, The function you see in the browser console is just the title of the node running.

I am not actually show how $env works internally but it looks like you can’t dump all of them easily, At some point I really should dig into the expressions.

1 Like

OK Thanks for your prompt response @Jon :+1:

1 Like

Why not just remove their contents and keep their keys?