Skip to content

Expressions#

In version 1.9.0 n8n changed the templating language for expressions

If you have issues with expressions in 1.9.0:

  • Please report the issue on the forums.
  • Self-hosted users can switch back to RiotTmpl: set N8N_EXPRESSION_EVALUATOR to tmpl. Refer to Environment variables for more information on configuring self-hosted n8n.

Use expressions to set node parameters dynamically based on data from:

  • Previous nodes
  • The workflow
  • Your n8n environment

You can execute JavaScript within an expression.

n8n created and uses a templating language called Tournament, and extends it with custom methods and variables and data transformation functions that help with common tasks, such as retrieving data from other nodes, or accessing metadata.

n8n supports two libraries:

  • Luxon, for working with data and time.
  • JMESPath, for querying JSON.

No Python support

Expressions must use JavaScript.

Data in n8n

When writing expressions, it's helpful to understand data structure and behavior in n8n. Refer to Data for more information on working with data in your workflows.

Writing expressions#

To use an expression to set a parameter value:

  1. Hover over the parameter where you want to use an expression.
  2. Select Expressions in the Fixed/Expression toggle.
  3. Write your expression in the parameter, or select Open expression editor Open expressions editor icon to open the expressions editor. If you use the expressions editor, you can browse 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
[
  {
    "headers": {
      "host": "n8n.instance.address",
      ...
    },
    "params": {},
    "query": {},
    "body": {
      "name": "Jim",
      "age": 30,
      "city": "New York"
    }
  }
]

In the next node in the workflow, you want to get just the value of city. You can use the following expression:

1
{{$json.body.city}}

This expression:

  1. Accesses the incoming JSON-formatted data using n8n's custom $json variable.
  2. 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 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
// This example is split over multiple lines for readability
// It's still invalid when formatted as a single line
{{
  function example() {
    let end = DateTime.fromISO('2017-03-13');
    let start = DateTime.fromISO('2017-02-13');
    let diffInMonths = end.diff(start, 'months');
    return diffInMonths.toObject();
  }
  example();
}}

While the second example is valid:

1
{{DateTime.fromISO('2017-03-13').diff(DateTime.fromISO('2017-02-13'), 'months').toObject()}}