# Helpers

> The helper functions an expression can call — JMESPath queries, defaults for empty values, slugs, query strings, Base64 and random ids — plus lodash, Luxon and the JavaScript built-ins.

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

Helpers are functions available in every expression. They cover the small jobs that come up between nodes: choosing a
default, building a URL, querying a nested response, encoding a value.

| Helper                      | Returns                                      |
| --------------------------- | -------------------------------------------- |
| `$jmespath(object, query)`  | The result of a JMESPath query               |
| `$ifEmpty(value, fallback)` | `value`, or `fallback` when `value` is empty |
| `$isEmpty(object)`          | `true` when the object has no keys           |
| `$slugify(text)`            | A lowercase, hyphenated slug                 |
| `$toQueryString(object)`    | A query string that starts with `?`          |
| `$base64Encode(value)`      | URL-safe Base64 text                         |
| `$base64Decode(text)`       | The decoded text                             |
| `$randomId(options)`        | A random id with a prefix                    |

## $jmespath(object, query)

Runs a [JMESPath](https://docs.sommatic.ai/en/expressions/jmespath.md) query over any object and returns what it selects. It shines with deep
API responses, where a query is shorter than a chain of JavaScript.

```javascript
{{ $jmespath($json, 'items[?qty > `0`].sku') }}
{{ $jmespath($('Fetch order').item.json, 'lines[0].product.name') }}
{{ $jmespath($json, 'length(items)') }}
```

JMESPath literals go between backticks inside the query, as in `` `0` `` above.

## $ifEmpty(value, fallback)

Returns `fallback` when `value` is `undefined`, `null`, an empty string, an empty list or an object without keys;
otherwise returns `value`. `0` and `false` are kept.

```javascript
{{ $ifEmpty($json.body.priority, 'normal') }}
{{ $ifEmpty($('Find customer').item.json.segment, 'unassigned') }}
```

## $isEmpty(object)

Returns `true` when the object has no own keys. It only looks at the first level: `{ a: {} }` is not empty.

```javascript
{{ $isEmpty($json.body.attachments_meta) }}
```

For strings, `null` or `undefined`, prefer `$ifEmpty`.

## $slugify(text)

Turns any text into a lowercase slug with hyphens, without accents.

```javascript
{{ $slugify('Hola Mundo Ñandú 2026') }}
// → hola-mundo-nandu-2026
```

## $toQueryString(object)

Builds a query string from an object, encoding keys and values. Nested objects use brackets (`filter[status]=open`,
encoded). The result starts with `?`, so you can append it to a URL.

```javascript
https://api.example.com/orders{{ $toQueryString({ status: 'open', page: 2 }) }}
// → https://api.example.com/orders?status=open&page=2
```

> **Warning: Falsy values become null**
>
> `false`, `0` and empty strings are sent as the text `null`: `$toQueryString({ active: false })` gives
> `?active=null`. Send them as strings (`'false'`, `'0'`) or build that part of the URL yourself.

## $base64Encode(value) and $base64Decode(text)

`$base64Encode` encodes a string, or an object serialized as JSON, with the URL-safe alphabet (`-` and `_` instead of
`+` and `/`) and without `=` padding. `$base64Decode` returns the decoded text. Both return an empty string for an
empty value.

```javascript
{{ $base64Encode('Sommatic') }}
// → U29tbWF0aWM
{{ $base64Encode({ id: $json.id }) }}
// → eyJpZCI6N30 when id is 7
{{ $base64Decode($json.body.state) }}
```

When a service requires standard Base64 with padding, convert the value in a [code node](https://docs.sommatic.ai/en/expressions/code-nodes.md).

## $randomId(options)

Returns a prefix followed by random hexadecimal characters. Always pass the options object:

| Option   | Default | Meaning                     |
| -------- | ------- | --------------------------- |
| `length` | `15`    | Number of random characters |
| `prefix` | `'id-'` | Text placed before them     |

```javascript
{{ $randomId({ length: 8, prefix: 'ord-' }) }}
// → ord-3f9a01c2
{{ $randomId({}) }}
// → id- followed by 15 characters
```

For a standard UUID, use `$uuid`.

## Libraries

| Library  | How you call it                                                               | Reference                                                                         |
| -------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| lodash   | `_`, for example `_.sumBy($json.items, 'qty')` or `_.get($json, 'a.b', 0)`    | [lodash](https://docs.sommatic.ai/en/expressions/lodash.md)                       |
| Luxon    | Through `$now` and `$today`, for example `$now.plus({ days: 1 }).toISODate()` | [Luxon](https://docs.sommatic.ai/en/expressions/luxon.md)                         |
| JMESPath | Through `$jmespath(object, query)`                                            | [JMESPath](https://docs.sommatic.ai/en/expressions/jmespath.md)                   |
| JSONata  | In JSONata fields, not inside braces                                          | [JSONata functions](https://docs.sommatic.ai/en/expressions/jsonata-functions.md) |

```javascript
{{ _.pick($json, ['id', 'status']) }}
{{ _.uniq($json.tags) }}
{{ _.groupBy($json.items, 'category') }}
```

## JavaScript built-ins

Expressions can use `Math`, `JSON`, `Date`, `String`, `Number`, `Boolean`, `Array` and `Object`, and every method of
strings, numbers, lists and objects (`split`, `toUpperCase`, `padStart`, `map`, `filter`, `reduce`, `Object.keys`…).

```javascript
{{ Number($json.amount) * 1.19 }}
{{ String($json.id).padStart(6, '0') }}
{{ encodeURIComponent($json.key) }}
```

To convert text to a number, use `Number(…)`. Avoid I/O: an expression cannot call services or read files; use
[`io.http`](https://docs.sommatic.ai/en/nodes/io.http.md) or a [code node](https://docs.sommatic.ai/en/expressions/code-nodes.md).

## Next steps

- [API reference](https://docs.sommatic.ai/en/expressions/reference.md): Every variable and helper with its signature, parameters, return value, errors and examples run against the engine.
- [Recipes](https://docs.sommatic.ai/en/expressions/recipes.md): Helpers and variables in patterns that work in production.
- [Gotchas](https://docs.sommatic.ai/en/expressions/gotchas.md): What breaks expressions and how to avoid it.
