Code
Runs your own JavaScript to reshape, calculate or clean up data when the built-in steps aren't enough. The script runs in an isolated sandbox with no network, file system or host access — it can only read the workflow data handed to it and return a value.
Parameters
| Name | Required | Default | Description |
|---|---|---|---|
Mode (mode) | Yes | Run once for all items | Run once for all items (runOnce) runs the script a single time. Run once for each item (runForEach) runs it once per element of items and collects the return values into an array. |
Input (input) | No | Output of the previous step | The value exposed to the script as $input. Supports expressions, e.g. {{ $nodes['HTTP Request'].responseBody }}. |
JavaScript (code) | Yes | A starter script | The script. It is the body of a function, so finish with return. Stored verbatim — {{ }} inside the code is not treated as an expression. Max 200,000 characters. |
Timeout (seconds, max 60) (timeoutSeconds) | No | 10 | Wall-clock limit for the script. Values are clamped to 1–60. |
Ports
| Port | Description |
|---|---|
main (input) | Connect the step(s) whose data the script needs. |
output | { result, logs } — see below. |
What the script can use
| Global | What it is |
|---|---|
$input | The Input value, or the previous step's output when Input is empty. |
items | $input as an array: the array itself, [$input] for a single value, or [] when there is no input. |
$nodes | Outputs of earlier steps by name, e.g. $nodes['Fetch Orders'].items. |
$vars | Workflow variables. |
$inputs | Workflow input parameters. Connection parameters are never passed into the sandbox. |
$now | The current time as an ISO 8601 string (UTC). |
item, $item, index | Per-item mode only: the current element and its zero-based position. |
console.log / info / debug / warn / error | Captured into the logs output (up to 500 entries, 4,000 characters each). |
Data enters the sandbox as plain JSON: dates arrive as ISO strings, and files appear as descriptor objects (an in-memory file as { fileName, contentType, size, base64 }, a stored file as { providerId, fileId, fileName, contentType, rowCount }). A dataset works like a list of its first rows: rows[0], rows.length (all rows) and rows.preview work, and reading a row past the preview or calling a list method such as rows.map() throws an error that says to use a Loop. A file's base64 content is only passed in when your script mentions base64 (for example $input.file.base64); otherwise files arrive without it, so steps that only look at names or sizes don't copy every file into the sandbox and its memory limit.
There is no fetch, require/import, file system, eval or new Function(...). The script runs in strict mode. Use the HTTP Request step for network calls and pass the result in. $secrets, $trigger, $execution and $workflow are not available inside the script — resolve what you need in the Input field with an expression instead.
Returning data
Whatever you return becomes result in the output. Return any JSON-serialisable value: an object, an array, a string, a number.
- Run once for all items —
resultis your return value (nullif you return nothing). - Run once for each item —
resultis an array with one entry per item; an item whose run returns nothing contributesnull.
{
"result": [
{ "id": 1, "total": 59.97 },
{ "id": 2, "total": 12.5 }
],
"logs": [
{ "level": "info", "message": "processed 2 orders" }
]
}
Downstream, reference it as {{ $nodes['Code'].result }}.
Example
Calculate an order total from the line items returned by a previous Fetch Orders step.
- Mode: Run once for all items
- Input:
{{ $nodes['Fetch Orders'].items }} - JavaScript:
const out = items.map(order => ({
id: order.id,
total: order.lines.reduce((sum, l) => sum + l.price * l.qty, 0)
}));
console.log(`processed ${out.length} orders`);
return out;
The same thing in per-item mode:
return {
id: item.id,
total: item.lines.reduce((sum, l) => sum + l.price * l.qty, 0)
};
Generate code with AI
The JavaScript editor has a Generate code with AI button (wand icon, top right). Describe what you want — for example Sum the amount of every order from "Fetch Orders" — click Generate, review the snippet, then Insert it. Inserting replaces the current script. The generator is told about the sandbox globals listed above and sees your current code. See AI assistant.
Limits
| Limit | Value |
|---|---|
| Time | 10 s by default, up to 60 s (timeoutSeconds) |
| Memory | 64 MB |
| Statements | 20,000,000 per run (guards against infinite loops) |
| Recursion depth | 256 |
| Script size | 200,000 characters |
| Console output | 500 entries, 4,000 characters each |
Errors
A thrown error, a syntax error or an exceeded limit fails the step with a message starting Code: — for a script error it includes the line in your script, e.g. (line 2); for limits it reads like Code: the script exceeded the 10s time limit. Anything logged before the failure is still kept in logs. Failures are handled like any other step error — see Error handling.