# Code nodes

> What code.js and code.python can use when they run — input, globals, logs, timeouts and output shape — and how to pass them data from other nodes and the run.

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

When the logic outgrows an expression — loops, parsing, calculations over many records — write it in a code node.
[`code.js`](https://docs.sommatic.ai/en/nodes/code.js.md) runs JavaScript and [`code.python`](https://docs.sommatic.ai/en/nodes/code.python.md) runs Python. Both receive
the node's input and return a value that becomes the node's output.

**code.js**

```javascript
const lines = $json.items.filter((item) => item.qty > 0);
const total = lines.reduce((sum, item) => sum + item.qty * item.price, 0);
console.log(`${lines.length} lines`);
return { total, skus: _.map(lines, 'sku') };
```

**code.python**

```python
def main(input):
    lines = [item for item in input.get("items", []) if item.get("qty", 0) > 0]
    total = sum(item["qty"] * item["price"] for item in lines)
    print(f"{len(lines)} lines")
    return {"total": total, "skus": [item["sku"] for item in lines]}
```

## Output: result and logs

Both nodes wrap what you return:

```json
{ "result": { "total": 42, "skus": ["A", "C"] }, "logs": ["2 lines"] }
```

The next node reads `$json.result.total`; nodes further down read `$('Compute totals').item.json.result.total`. In
`code.js`, `logs` is a list of the lines you logged; in `code.python` it is the text printed to the console. Errors,
syntax errors and timeouts leave through the `error` port with the message in the output.

## code.js

Your code runs as the body of an `async` function: use `return` to produce the result and `await` when you need it. If
nothing is returned, `result` is `null`.

| Available                                                                | Notes                                                                                                                                |
| ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| `$json`                                                                  | The node's input.                                                                                                                    |
| `$input`                                                                 | `$input.item`, `$input.first()` and `$input.last()` return the input itself (not `{ json }`); `$input.all()` returns a list with it. |
| `console.log`, `console.warn`, `console.error`                           | Captured in `logs`.                                                                                                                  |
| `_`                                                                      | lodash.                                                                                                                              |
| `JSON`, `Math`, `Date`, `String`, `Number`, `Boolean`, `Array`, `Object` | Standard built-ins.                                                                                                                  |
| `parseInt`, `parseFloat`, `isNaN`, `isFinite`                            | Number parsing.                                                                                                                      |
| `encodeURIComponent`, `decodeURIComponent`, `encodeURI`, `decodeURI`     | URL encoding.                                                                                                                        |
| `RegExp`, `Map`, `Set`, `Promise`                                        | Standard built-ins.                                                                                                                  |

The expression variables `$('…')`, `$node`, `$execution`, `$now` and the `$` helpers are **not** available inside
`code.js`. The default timeout is 5,000 ms; change it with `execution.timeout_ms`. This page covers the default
execution mode; the node page lists the others.

## code.python

Define a `main(input)` function. `input` is the node's input as a dictionary; return a value that can be serialized to
JSON (values that cannot, such as dates, are converted to text).

| Setting                | Notes                                                                                                                                                                        |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `packages`             | PyPI packages to install before your code runs, such as `["pandas"]`. They go into a temporary environment created for each execution, so installing adds time to every run. |
| `execution.timeout_ms` | Default 30,000 ms. Raise it for OCR or heavy processing.                                                                                                                     |

`print` writes to `logs`, not to the result. A file without `main(input)` fails.

## Pass data from other nodes

Code nodes see only their input. When the code needs the output of earlier nodes or the run ids, put a
[`data.transform`](https://docs.sommatic.ai/en/nodes/data.transform.md) before it that builds that input with an expression:

```javascript
{{ ({ order: $('Fetch order').item.json, customer: $('Find customer').item.json, run: $execution.id, today: $now.setZone('America/Bogota').toISODate() }) }}
```

The code then reads `$json.order`, `$json.customer`, `$json.run` and `$json.today` (or `input["order"]`… in Python).

> **Warning: Avoid double braces in code**
>
> The `code` field is a node field: two opening braces written together are read as an expression before the code
> runs. In Python, avoid f-string escapes for literal braces and build them with concatenation, such as `"{" + "{"`.
> In JavaScript, add a space between nested braces.

## Next steps

- [code.js](https://docs.sommatic.ai/en/nodes/code.js.md): Configuration, modes and ports of the JavaScript node.
- [code.python](https://docs.sommatic.ai/en/nodes/code.python.md): Configuration and ports of the Python node.
