To effectively set a variable in n8n, you must first understand that there isn’t just one way; there are three primary methods, each designed for a different scope and purpose. For modifying data on a per-item basis as it flows through your workflow, the Edit Fields (Set) node is your go-to tool. When you need a variable that persists and updates throughout a single workflow execution, especially within loops, you’ll use Workflow Static Data inside a Code node. Finally, for storing read-only, instance-wide values like constants, n8n Enterprise and Pro plans offer Custom Variables.
Understanding Variables in n8n: More Than One Way to Store Data
When I first started building complex automations in n8n, the concept of a “variable” seemed simple. But I quickly realized that, unlike traditional programming, n8n treats variables contextually. A variable isn’t just a variable; its behavior depends entirely on how and where you create it. This distinction is the key to moving from basic, linear workflows to powerful, dynamic automations.
So, which method should you use? The answer, as is often the case in automation, is: it depends on your goal. Let’s break down the three main ways you can n8n set variable
and when to use each one.
H2: The Everyday Workhorse: The ‘Edit Fields’ (Set) Node
This is the method you’ll probably use 90% of the time. The ‘Edit Fields’ node (which was simply called the ‘Set’ node in older versions) allows you to add, modify, or remove fields on the JSON object of each item that passes through it.
Think of it like an assembly line. Each item coming down the line is a separate product. The ‘Edit Fields’ node is a station where a worker adds a new part or a label to that specific product. It doesn’t affect the products that came before it or the ones that will come after.
H4: When to Use the Edit Fields Node
- Creating new fields from existing data: Combining a
firstName
andlastName
field to create afullName
. - Setting default or static values: Adding a
status
field with the value “New” to every incoming lead. - Simplifying complex data: Extracting a nested value like
{{ $json.customer.address.city }}
and putting it into a top-level field calledcity
for easier access later.
It’s simple, visual, and incredibly effective for most data manipulation tasks. But what happens when you need to remember something across multiple items in the same run?
H2: For Loops and Counters: Workflow Static Data
Now, here’s where it gets interesting. Have you ever needed to process a list of 100 items and keep a running total of a certain value? Or count how many times a loop has run? If you try to use an ‘Edit Fields’ node for this, you’ll find that it resets for each item. This is where workflow.workflowStaticData
comes to the rescue.
This is a special object that exists only for the duration of a single workflow execution. It’s like having a shared whiteboard for your entire automation run. Any node in the workflow can write to it or read from it, and the data persists until the workflow finishes.
Let’s be honest about this: you have to use the Code node to manage it, which can feel a bit intimidating at first, but it’s surprisingly straightforward.
H4: Real-World Example: Tallying Processed Orders
Imagine you have a workflow that fetches new orders from Shopify. You want to loop through each order, check if it’s high-value (over $100), and at the very end, send a single Slack message summarizing how many high-value orders were found.
-
Initialize the Counter: Add a Code node right after your trigger. In it, you’ll initialize your counter:
workflow.workflowStaticData.highValueOrderCount = 0;
return items;
-
Increment Inside the Loop: After your ‘Split in Batches’ or ‘Loop Over Items’ node, you’ll have an ‘If’ node to check if
order.total_price > 100
. On the ‘true’ branch, add another Code node to increment the counter:
workflow.workflowStaticData.highValueOrderCount++;
return items;
-
Use the Final Count: At the end of your workflow, after the loop is complete, you can use the final value in your Slack node’s text field with an expression:
Total high-value orders found: {{ workflow.workflowStaticData.highValueOrderCount }}
.
Without workflowStaticData
, this kind of summary would be incredibly difficult to achieve. It’s the secret sauce for stateful operations within a single run.
H2: For Global Constants: Custom Variables
Finally, we have Custom Variables. (A quick heads-up: this is an Enterprise and Pro Cloud feature). These are read-only variables that an instance owner can define in the n8n admin panel. Once set, they are available in every single workflow on that n8n instance.
Think of these not as variables you set in a workflow, but as constants you use. They are perfect for storing values that don’t change often and need to be referenced across your automations.
H4: When to Use Custom Variables
- A company-wide support email address.
- The User ID for a specific admin who should receive error notifications.
- A standard greeting message for chatbots.
You access them with a simple expression: {{ $vars.yourVariableName }}
. The key benefit here is maintainability. If that support email changes, you update it in one place (the Variables page), and it’s instantly corrected in all 50 workflows that use it. No more find-and-replace nightmares!
H3: Quick Comparison: Which Variable Type is Right for You?
To make it crystal clear, here’s a simple breakdown:
Feature | Scope | How to Set | How to Use | Best For… |
---|---|---|---|---|
Edit Fields (Set) Node | Per Item | In the node’s UI | {{ $json.fieldName }} |
Modifying data as it flows, creating new fields. |
Workflow Static Data | Per Execution | Code Node (workflow.workflowStaticData ) |
Code Node (workflow.workflowStaticData ) |
Counters, accumulators, and sharing data in loops. |
Custom Variables | Instance-wide | n8n Admin UI (Variables page) | {{ $vars.variableName }} |
Reusable, non-secret, global constants. |
Choosing the right way to n8n set variable
is fundamental to building scalable and maintainable workflows. By understanding the scope of each method—item, execution, or instance—you’re no longer just connecting nodes; you’re architecting intelligent automation solutions.