Skip to main content

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​

NameRequiredDefaultDescription
Mode (mode)YesRun once for all itemsRun 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)NoOutput of the previous stepThe value exposed to the script as $input. Supports expressions, e.g. {{ $nodes['HTTP Request'].responseBody }}.
JavaScript (code)YesA starter scriptThe 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)No10Wall-clock limit for the script. Values are clamped to 1–60.

Ports​

PortDescription
main (input)Connect the step(s) whose data the script needs.
output{ result, logs } — see below.

What the script can use​

GlobalWhat it is
$inputThe 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.
$nodesOutputs of earlier steps by name, e.g. $nodes['Fetch Orders'].items.
$varsWorkflow variables.
$inputsWorkflow input parameters. Connection parameters are never passed into the sandbox.
$nowThe current time as an ISO 8601 string (UTC).
item, $item, indexPer-item mode only: the current element and its zero-based position.
console.log / info / debug / warn / errorCaptured 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 — result is your return value (null if you return nothing).
  • Run once for each item — result is an array with one entry per item; an item whose run returns nothing contributes null.
{
"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​

LimitValue
Time10 s by default, up to 60 s (timeoutSeconds)
Memory64 MB
Statements20,000,000 per run (guards against infinite loops)
Recursion depth256
Script size200,000 characters
Console output500 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.