Code
The Code step runs a small JavaScript script inside a workflow. Use it when no built-in node does exactly what you need: reshaping data, combining the output of several previous steps, doing a calculation, or implementing custom logic.
It’s listed in the Add Step panel as Code (“Write custom code”) under the Core category.
How It Works
- Add a Code step and choose an Execution Mode.
- Write a script that reads the
inputandstepsvariables andreturns a value. - puq.ai runs the script in a sandbox and uses the returned value as the step’s output.
The editor only accepts a safe subset of JavaScript (ES2020) — see What You Can’t Use.
Settings
| Setting | Required | Default | Description |
|---|---|---|---|
| Execution Mode | Yes | Run Once for all items | Run Once for all items or Run once for each item |
| Property Name | Only in “Run once for each item” mode | — | Name of the array property on input to loop over, for example items |
| Script | Yes | return true; | The JavaScript to run |
Execution Modes
Run Once for all items
The script runs once and receives the full input data. Use this when you want to process or transform the entire dataset together.
Run once for each item
The script runs separately for each element in an array property. Use this when you want to process items one by one. Each run has access to an
itemvariable representing the current element.
- Property Name tells the step which property of
inputholds the array — for exampleitemsif your input is{ "items": [...] }. It also accepts a value mapped from a previous step. - If
inputisn’t an object, the property is missing, or it isn’t an array, the step fails. - The output is an array, one entry per item, in the same order as the input array.
- If any single item’s script throws, the whole step fails — it does not skip the failing item and keep going.
Context Variables
| Variable | Available in | Description |
|---|---|---|
input | Both modes | The step’s input data |
steps | Both modes | Outputs of previous steps, keyed by step name (only steps that have produced output) |
item | Run once for each item | The current array element |
Return Value
- Run Once for all items — the script must
returnan Object or Array. Any other type (string, number, boolean…) fails the step with an error such asScript output must be an Object or Array, got string. - Run once for each item — each run can return any value; the step’s output is the array of per-item results.
- An empty script succeeds immediately with a
nulloutput.
What You Can’t Use
The Code step runs in a restricted JavaScript sandbox, not a browser or Node.js:
- No
async/await— code runs synchronously. - No
fetch,XMLHttpRequest,require, orimport— use an HTTP Utilities step for external calls. - No
window,document,localStorage, orsessionStorage— no browser or DOM access. - No
evalor theFunctionconstructor.
The editor underlines these as errors as you type.
A script that runs for about 30 seconds or longer, or that allocates roughly 16 MB or more, fails with an execution timeout or memory limit error.
Output
- Run Once for all items: the returned Object/Array becomes the step’s output directly.
- Run once for each item: an array of the values returned for each item, in order.
Errors
The step fails when:
- The script throws, has a syntax error, or uses a disabled feature.
- Run Once for all items returns something other than an Object or Array.
- Run once for each item is selected and the Property Name is missing from
input, not found, or not an array. - The script exceeds the execution time or memory limit.
The step does not fail just because the script is empty — an empty script succeeds with a null output.
Example
// Execution Mode: Run Once for all items
// input: { "firstName": "Ana", "lastName": "Lopez" }
return {
fullName: `${input.firstName} ${input.lastName}`,
initials: `${input.firstName[0]}${input.lastName[0]}`.toUpperCase(),
};
// Execution Mode: Run once for each item, Property Name: items
// input: { "items": [{ "price": 10 }, { "price": 25 }] }
return {
...item,
priceWithTax: item.price * 1.2,
};
// Output: [{ "price": 10, "priceWithTax": 12 }, { "price": 25, "priceWithTax": 30 }]
Best Practices
- Keep scripts small and focused; use a Router for branching instead of encoding it in the script.
- Prefer Run once for each item over looping manually inside the script — it keeps each item’s result (and failure) separate.
- Avoid building very large strings or arrays; the sandbox has a fixed memory ceiling.
- Use an HTTP Request or a connector node for calls to external services instead of trying to call out from the script.