Expressions#
Expressions allow you to set node parameters dynamically based on data from:
- Previous nodes
- The workflow
- Your n8n environment
n8n uses the riot-tmpl templating language, and extends it with custom methods and variables.
You can execute JavaScript within an expression.
n8n supports two libraries that make common tasks easier:
Writing expressions#
To use an expression to set a parameter value:
- Select Parameter options for the parameter where you want to use an expression.
- Select Add expression.
- Write your expression in the expression editor. You can browse some of the available data in the Variable selector. All expressions have the format
{{ your expression here }}
.
Example: Get data from webhook body#
Consider the following scenario: you have a webhook trigger that receives data through the webhook body. You want to extract some of that data for use in the workflow.
Your webhook data looks similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
In the next node in the workflow, you want to get just the value of city
. You can use the following expression:
1 |
|
This expression:
- Accesses the incoming JSON-formatted data using n8n's custom
$json
variable. - Finds the value of
city
(in this example, "New York"). Note that this example uses JMESPath syntax to query the JSON data. You can also write this expression as{{$json['body']['city']}}
.
Example: Writing longer JavaScript#
An expression contains one line of JavaScript. This means you can'd do things like variable assignments or multiple standalone operations.
To understand the limitations of JavaScript in expressions, and start thinking about workarounds, look at the following two pieces of code. Both code examples use the Luxon date and time library to find the time between two dates in months, and encloses the code in handlebar brackets, like an expression.
However, the first example isn't a valid n8n expression:
1 2 3 4 5 6 7 8 9 10 11 |
|
While the second example is valid:
1 |
|