# Encoders

> Base64 and URL query strings.

Source: https://docs.sommatic.ai/en/expressions/reference/encoders/

Each example was run against the deployed expression engine over the [example context](#example-context) at the end of the page.

## `$base64Encode`

Function · `string` · Source: `Link Loom SDK · utilities.encoder.base64.encode`

```javascript
$base64Encode(value)
```

Encodes `value` with the URL-safe Base64 alphabet (`-` and `_` instead of `+` and `/`) and without `=` padding. A string is encoded as UTF-8; any other value is serialized as JSON first. Empty values (`''`, `null`, `undefined`, `0`, `false`) give an empty string.

### Parameters

- `value` (string | object, required): The text or value to encode.

### Returns

`string`: URL-safe Base64 text without padding.

### Examples

```javascript
{{ $base64Encode('Sommatic') }}
```

Result (string):

```json
"U29tbWF0aWM"
```

```javascript
{{ $base64Encode({ order: $json.body.order_id }) }}
```

Result (string):

```json
"eyJvcmRlciI6Im9yZF8xMDQyIn0"
```

```javascript
{{ $base64Encode('') }}
```

Result (string):

```json
""
```

### Notes

- Some services require standard Base64 with padding (for example a `Basic` authorization header). Convert the result: `{{ (function(){ var b = $base64Encode(text).replace(/-/g, '+').replace(/_/g, '/'); return b + '==='.slice((b.length + 3) % 4); })() }}`, or bind a credential to the node instead of building the header.

In JSONata: `$$base64Encode(body.order_id)`

## `$base64Decode`

Function · `string` · Source: `Link Loom SDK · utilities.encoder.base64.decode`

```javascript
$base64Decode(text)
```

Decodes standard or URL-safe Base64, with or without padding, and returns the result as UTF-8 text. It never fails: characters outside the alphabet are skipped. To get an object back from encoded JSON, parse the text with `JSON.parse`.

### Parameters

- `text` (string, required): Base64 text.

### Returns

`string`: The decoded text; an empty string for an empty value.

### Examples

```javascript
{{ $base64Decode('U29tbWF0aWM') }}
```

Result (string):

```json
"Sommatic"
```

Back to an object.

```javascript
{{ JSON.parse($base64Decode('eyJvcmRlciI6Im9yZF8xMDQyIn0')) }}
```

Result (object):

```json
{
  "order": "ord_1042"
}
```

### Notes

- Binary content (a PDF, an image) does not survive the conversion to text: decode it in a code node or let the node that handles the file do it.

In JSONata: `$$base64Decode(body.state)`

## `$toQueryString`

Function · `string` · Source: `Link Loom SDK · utilities.encoder.object.toQueryString`

```javascript
$toQueryString(object)
```

Builds a query string from an object, percent-encoding keys and values. Nested objects and lists use brackets (`filter[status]`, `ids[0]`), also encoded. The result starts with `?`, ready to append to a URL; anything that is not an object gives an empty string.

### Parameters

- `object` (object, required): The parameters, one key per parameter.

### Returns

`string`: `?key=value&…`, or `''` when the argument is not an object.

### Examples

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

Result (string):

```json
"https://api.example.com/orders?status=open&page=2"
```

```javascript
{{ $toQueryString({ q: $json.body.customer.name, filter: { segment: 'gold' } }) }}
```

Result (string):

```json
"?q=Ana%20P%C3%A9rez&filter%5Bsegment%5D=gold"
```

```javascript
{{ $toQueryString({ ids: ['A-100', 'C-300'] }) }}
```

Result (string):

```json
"?ids%5B0%5D=A-100&ids%5B1%5D=C-300"
```

false, 0 and empty strings are sent as null.

```javascript
{{ $toQueryString({ active: false, retries: 0 }) }}
```

Result (string):

```json
"?active=null&retries=null"
```

### Notes

- `false`, `0` and `''` become the text `null`. Send them as strings (`'false'`, `'0'`) or write that part of the URL yourself.

In JSONata: `$$toQueryString({ "status": "open" })`

## Example context

The examples run over this data: the input of the current node (`$json`), the nodes that already ran (`$(…)`), the ids of the run and the current node. The clock is fixed at `2026-11-26T14:30:00.000Z` and the server zone is UTC, so `$now` and `$today` give the same values on every page.

```json
{
  "$json": {
    "body": {
      "order_id": "ord_1042",
      "email": "ana.perez@example.com",
      "customer": {
        "name": "Ana Pérez",
        "segment": "gold"
      },
      "priority": "",
      "items": [
        {
          "sku": "A-100",
          "qty": 2,
          "price": 10.5
        },
        {
          "sku": "B-200",
          "qty": 0,
          "price": 4
        },
        {
          "sku": "C-300",
          "qty": 1,
          "price": 25
        }
      ],
      "tags": [
        "new",
        "web",
        "new"
      ],
      "created_at": "2026-11-25T09:15:00Z"
    },
    "headers": {
      "content-type": "application/json"
    },
    "query": {},
    "method": "POST"
  },
  "nodes": {
    "Webhook Receiver": "= $json",
    "Normalize input": {
      "correlation_id": "c-7f3a",
      "channel": "web"
    },
    "Classify": {
      "label": "billing",
      "confidence": 0.92
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-xxxx",
    "$flow_instance_id": "fi-xxxx",
    "$flow_definition_id": "fd-xxxx",
    "$flow_version_id": "fv-xxxx",
    "$flow_slug": "order-intake"
  },
  "node": {
    "node_id": "node-notify",
    "label": "Notify customer"
  }
}
```

The examples of a chained workflow run over a second context: a workflow started by `workflow.chain`, whose `workflow.trigger` node is labelled `Chain Entry Point`.

```json
{
  "$json": {
    "order_id": "ord_1042",
    "customer_email": "ana.perez@example.com",
    "amount": 46,
    "chain_context": {
      "caller_flow_definition_id": "fd-caller-xxxx",
      "caller_flow_instance_id": "fi-caller-xxxx",
      "caller_flow_slug": "order-intake",
      "caller_node_id": "node-chain-billing",
      "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
      "root_flow_instance_result_id": "fir-root-xxxx"
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-hop-xxxx",
    "$flow_instance_id": "fi-hop-xxxx",
    "$flow_definition_id": "fd-billing-xxxx",
    "$flow_version_id": "fv-billing-xxxx",
    "$flow_slug": "billing-review",
    "$input": {
      "data": {
        "order_id": "ord_1042",
        "customer_email": "ana.perez@example.com",
        "amount": 46
      },
      "chain_context": {
        "caller_flow_definition_id": "fd-caller-xxxx",
        "caller_flow_instance_id": "fi-caller-xxxx",
        "caller_flow_slug": "order-intake",
        "caller_node_id": "node-chain-billing",
        "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
        "root_flow_instance_result_id": "fir-root-xxxx"
      }
    }
  },
  "node": {
    "node_id": "node-create-task",
    "label": "Create review task"
  }
}
```

Examples run against the orchestrator at commit 31e874b.
