# Recipes

> Expression patterns that work in production workflows — reading earlier nodes, building typed objects, defensive conditions, dates in a time zone, safe URLs, defaults and lists — ready to copy into a node field.

Source: https://docs.sommatic.ai/en/expressions/recipes/ (updated 2026-11-26)

Each recipe says which field kind it needs: **pure** (the whole field is the expression and keeps its type) or
**mixed** (text around the expression, the result is text). Replace the node labels with the labels of your workflow.

## Read a field from an earlier node

Pure or mixed. The most common expression in production:

```javascript
{{ $('Normalize input').item.json.correlation_id }}
```

## Read the webhook request

Right after a webhook trigger, the request is the input (`$json`). Further down, read it from the trigger by label:

```javascript
{{ $json.body.email }}
{{ $('Webhook Receiver').item.json.body.email }}
{{ $('Webhook Receiver').item.json.query.source }}
```

## Build a typed object

Pure. Wrap object literals in parentheses, otherwise the braces are read as a block of code:

```javascript
{{ ({ id: $json.id, received: $now.toISO(), run: $execution.id }) }}
```

Use it for a request body, the output of a `data.transform` or the payload of a task.

## Write a defensive condition

Pure, for [`flow.if`](https://docs.sommatic.ai/en/nodes/flow.if.md). When the data you read may be missing, wrap the logic in a function that you
call at once, and return `false` if anything fails:

```javascript
{{ (function () { try { var result = $('Classify').item.json || {}; return (result.confidence || 0) >= 0.7; } catch (error) { return false; } })() }}
```

For short checks, optional chaining is enough:

```javascript
{{ ($('Classify').item.json.confidence ?? 0) >= 0.7 }}
```

## Format dates in a time zone

Pure or mixed. `$now` uses the server's zone; set yours first:

```javascript
{{ $now.setZone('America/Bogota').toFormat('yyyy-LL-dd HH:mm') }}
{{ $now.setZone('America/Bogota').plus({ days: 3 }).toISODate() }}
{{ $today.minus({ months: 1 }).toFormat('LLLL yyyy') }}
```

## Build safe URLs

Mixed. Encode every value that goes into a path or query:

```javascript
https://api.example.com/items/{{ encodeURIComponent($json.key) }}
https://api.example.com/search{{ $toQueryString({ q: $json.body.term, page: 1 }) }}
```

## Fall back to a default

Pure or mixed:

```javascript
{{ $ifEmpty($json.body.priority, 'normal') }}
{{ $json.customer?.name ?? 'Customer' }}
```

## Filter, map and sum lists

Pure:

```javascript
{{ $json.items.filter(item => item.qty > 0).map(item => item.sku) }}
{{ _.sumBy($json.items, 'qty') }}
{{ $jmespath($json, 'items[?status == `open`].id') }}
```

## Use an optional branch

Pure. Read a node only if it ran in this execution:

```javascript
{{ $('Enrich customer').isExecuted ? $('Enrich customer').item.json.segment : 'unknown' }}
```

## Put a traceable reference in a message

Mixed. `$execution.id` is shared by every run of a `workflow.chain`, so one reference follows the whole case:

```javascript
We received your request. Reference: {{ $execution.id }}
```

## Read the result of a code node

`code.js` and `code.python` wrap what they return in `result`:

```javascript
{{ $('Compute totals').item.json.result.total }}
```

## Join values into one piece of text

When a field would start and end with an expression, write a single expression that concatenates:

```javascript
{{ $json.first_name + ' ' + $json.last_name }}
{{ `${$json.prefix}-${$json.number}` }}
```

> **Tip: Long logic belongs in a node**
>
> If an expression grows beyond a few lines, move it to a [`data.transform`](https://docs.sommatic.ai/en/nodes/data.transform.md) or a
> [code node](https://docs.sommatic.ai/en/expressions/code-nodes.md). It is easier to test, it shows its output in the run, and later nodes can
> read it with `$('…')`.

## Next steps

- [Gotchas](https://docs.sommatic.ai/en/expressions/gotchas.md): What breaks expressions and how to avoid it.
- [Variables](https://docs.sommatic.ai/en/expressions/variables.md): Every variable these recipes use.
