Set global variables for workflow (self-hosted)

Describe the problem/error/question

I want to set a variable like $id that I can use anywhere in the workflow. A few different nodes might set it, so I can’t reference the node. Ideally, I would like to:
Set it like: var $id = "123"
And use it in a node like: $id.

Thank you for the help!

Please share your workflow

Here’s a simplified version of what I’m thinking.

Information on your n8n setup

  • n8n version: 1.75.2
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via: Docker

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @sbruner You can use Crypto node for this

Thank you for the reply. Can you explain how I would reference “$id” in another node?

In any node you can reach prevues nodes by it name {{ $("Node name").json }}. Look at this example

In my example, I don’t know the Node Name. That’s my issue.

How do I get data from a node when I don’t know the name, and it’s not the previous node?

start with an expression, type {{ }}, then type a $, you should then see options for all your previous nodes that are accessible

On the left side panel you should see all available variables that you can drag and drop. If you don’t see anything that might be because to forgot to run the previous node.

Note: If there is an error in previous node you would also have to resolve it before you see the information in the left side panel.

Sorry I a little bit confused. In the beginning of the workflow you would create $id in Crypto node and then reference to it by this node name. Do I miss something?
Take look at this course created by N8N team.
Level One

1 Like

I appreciate all the responses. Maybe I wasn’t clear. Please look at my initial workflow. Depending on the response of an IF Node, two different nodes will return an “id”. I don’t know which one, so I don’t know the name of the node. How can I get the data if I don’t know the name of the node?

You can use OR operator, so your expression would be {{ $("Node name_1").json || $("Node name_2").json }}

I appreciate your responses. My example is a very simplified version of what I’m thinking.

As the subject says, I’m really looking for a way to set Global Variables for the workflow in N8N. Is that possible?

Please look at Create custom variables | n8n Docs
As well to this post Global variables on self-hosted

It is a fairly new feature, so… (Edit: Apparently that’s not true per one of the n8n guys, but still…). be sure to test thoroughly… so see if $getWorkflowStaticData does what you need.

Edit: It is not exactly clear in the documentation whether this data is scoped to a specific execution of a workflow, or to the instance of n8n, crossing multiple executions of the workflow. The variable name “lastExecution” in the docs subtly suggests it is the latter, so I tested a bit to see how it works across multiple executions. I confirmed that a value set in one execution does persist to be read back in during later executions.

However, I also ran multiple executions of a workflow concurrently, where a “static data” variable gets set at the beginning, and checked at the end, to see if workflow executions were “stepping on” each other’s data, but… I couldn’t get that to fail, so maybe it is “thread safe” (so to speak). I would probably do some more testing before depending on this behavior in a production workflow.

Please mark this as the solution if it’s what you needed to get things working. Thanks.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.