Expression Engine
Relevant source files
The following files were used as context for generating this wiki page:
- packages/workflow/src/expression.ts
- packages/workflow/src/extensions/index.ts
- packages/@n8n/codemirror-lang/src/expressions/grammar.ts
- packages/workflow/src/workflow-data-proxy.ts
- packages/frontend/editor-ui/src/constants.ts
- packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json
- packages/nodes-base/nodes/Set/Set.node.json
- packages/nodes-base/nodes/If/If.node.json
- packages/nodes-base/nodes/Function/Function.node.json
- packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts
The n8n Expression Engine is a powerful core feature that allows for dynamic data handling and manipulation within workflows. It enables users to reference data from various sources—such as previous nodes, execution metadata, and environment variables—directly within a node's parameters. By embedding JavaScript-based logic inside {{ ... }} delimiters, users can perform complex data transformations, implement conditional logic, and dynamically configure nodes, making workflows highly flexible and powerful.
The engine is integral to most nodes, particularly those involved in data transformation like the Set node, conditional logic like the IF node, or custom operations like the HTTP Request node. It provides a consistent interface for accessing and processing data as it flows through a workflow.
Sources: packages/nodes-base/nodes/Set/Set.node.json, packages/nodes-base/nodes/If/If.node.json, packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json:91-96
Core Concepts
Expression Syntax
Expressions are identified by double curly braces {{ ... }}. The content within the braces is evaluated as JavaScript at execution time. A common use case is to access incoming JSON data and use it to populate a parameter.
For example, in a test workflow for the HTML Extract node, a Set node is configured to extract the value of an HTML string from the incoming JSON body.
{
"parameters": {
"keepOnlySet": true,
"values": {
"string": [
{
"name": "data",
"value": "={{ $json[\"body\"][\"html\"] }}"
}
]
},
"options": {}
},
"name": "Set",
"type": "n8n-nodes-base.set"
}
This expression {{ $json["body"]["html"] }} tells the engine to look at the incoming item's JSON data ($json), access the body object, then the html key, and use its value for the "data" field.
Sources: packages/nodes-base/nodes/HtmlExtract/test/workflow.dotNotation.json:91-96
Global Variables
Within an expression, several global variables are available to access different parts of the workflow's context. These variables provide a structured way to retrieve data without needing to write complex code.
The following diagram illustrates the data sources available to an expression.
The primary variables are summarized in the table below:
| Variable | Description |
|---|---|
$binary | Access binary data of an item. |
$data | Access all data of the current node execution. |
$env | Access environment variables. |
$evaluateExpression | Evaluate another expression from within an expression. |
$execution | Access information about the current workflow execution (e.g., ID, mode). |
$ifEmpty | Helper to provide a fallback value if a variable is empty. |
$input | Access data from a specific input of the current node. |
$item | An alias for the current item being processed. |
$jmespath | Query JSON data using JMESPath syntax. |
$fromAI | Access data from an AI agent. |
$json | Access the JSON data of the current item. |
$node | Access data from any other node in the workflow. |
$now | A Luxon DateTime object representing the current date and time. |
$parameter | Access other parameters of the same node. |
$position | The index of the current item being processed (0-based). |
$prevNode | A shortcut to access data from the node that directly precedes the current one. |
$runIndex | The index of the current execution of the node in a loop. |
$today | A Luxon DateTime object representing the start of the current day. |
$vars | Access workflow static variables. |
$workflow | Access information about the workflow itself (e.g., ID, name). |
$nodeVersion | The version of the current node. |
Sources: packages/frontend/editor-ui/src/constants.ts:510-534
Editor and Language Grammar
The n8n UI uses a CodeMirror-based editor for writing expressions. This provides features like syntax highlighting and autocompletion, which are powered by a defined language grammar. This grammar specifies the valid tokens and structure of an expression.
The parser is configured with a timeout to prevent long-running parsing from degrading UI performance.
export const EXPRESSION_EDITOR_PARSER_TIMEOUT = 15_000; // ms
Sources: packages/frontend/editor-ui/src/constants.ts:646
The grammar defines the building blocks of the expression language, including:
- Identifiers: Variable names like
$jsonor function names. - Accessors: Dot notation (
.) and bracket notation ([]) for accessing object properties. - Literals: Strings, numbers, booleans,
null, andundefined. - Operators: Arithmetic, comparison, and logical operators.
- Function Calls: Invoking built-in helper functions.
This structured grammar ensures that expressions are parsed consistently and allows the editor to provide helpful feedback to the user.
Sources: packages/@n8n/codemirror-lang/src/expressions/grammar.ts
Use in Core Nodes
The Expression Engine is fundamental to the operation of many core n8n nodes.
Data Transformation: Set Node
The Set node is a primary example of expression usage. It allows users to add, update, or transform fields in the data stream. Its alias list includes "JS", "JSON", "Filter", "Transform", and "Map", highlighting its role in data manipulation, which is powered by expressions.
Sources: packages/nodes-base/nodes/Set/Set.node.json:1-3, packages/nodes-base/nodes/Set/Set.node.json:81-84
Conditional Logic: IF Node
The IF node uses expressions to evaluate conditions and route items down a true or false path. Each condition compares a value (often derived from an expression) against another value. This allows for complex, data-driven branching within a workflow. The node's aliases include "Router", "Filter", "Condition", "Logic", and "Branch".
Sources: packages/nodes-base/nodes/If/If.node.json:1-3, packages/nodes-base/nodes/If/If.node.json:76-79
Custom Logic: Function Node
While the Function node allows for writing larger blocks of JavaScript, it still operates within the same data context as expressions. It can access the same global variables ($json, $item, etc.) to read incoming data and must return an array of items in the expected format. Its aliases include "Code", "Javascript", and "Custom Code".
Sources: packages/nodes-base/nodes/Function/Function.node.json:1-3, packages/nodes-base/nodes/Function/Function.node.json:80-83
API Calls: HTTP Request Node
In the HTTP Request node, expressions are used to dynamically set the URL, headers, query parameters, and body of the request. This enables creating requests based on data from previous steps, for example, fetching user details from an API using an ID from a database query.
Sources: packages/nodes-base/nodes/HttpRequest/HttpRequest.node.json:1-3
Summary
The Expression Engine is a cornerstone of n8n's functionality, providing the critical link between static workflow design and dynamic, data-driven execution. Through a simple {{ ... }} syntax, it exposes a rich context of variables and functions, enabling users to build sophisticated and adaptable automations. Its integration into the editor with features like syntax highlighting and its central role in core nodes for transformation, logic, and API interactions make it an indispensable tool for any n8n user.