Function step
A Function step runs JavaScript you write. Use it when an expression isn’t enough: to add up a list, reshape data or apply a rule with several parts.
Add a Function step
Section titled “Add a Function step”-
Add a step from Logic › Function. The Function panel opens with sample code.
-
Leave Language set to JAVASCRIPT.
-
Replace the sample under Code with your own. Format tidies it (Shift+Alt+F).
-
Select Add Step, then Save the automation.
What your code can use
Section titled “What your code can use”Your code runs as the body of an async function. You can return from the top level and use await.
These names are ready to use:
| Name | What it holds | Same as the expression |
|---|---|---|
input |
The run’s input | {{input.x}} is input.x |
step |
Earlier steps’ results | {{step.CONNECTOR_1.output.x}} is step.CONNECTOR_1.output.x |
vars |
Values set by Variable steps | {{var.x}} is vars.x |
config |
Configuration values | {{config.x}} is config.x |
loop |
Inside a loop: current, index, size |
{{loop.current}} is loop.current |
Variables are vars, with an s, because var is a reserved word in JavaScript. The Insert row’s var list inserts vars.name into your code for you.
There is no secret: an automation can’t read secrets, and code that reads secret.x fails the step with “secret.x has no value”. Keep credentials in a connection and call it with sdk.connector(...); the connection’s credential is applied to the request and never appears in the run. sdk.connector(...) calls public addresses only: a call to localhost, a private network or an internal name fails with “This address isn’t allowed: … Automations can call public addresses only.” (see Troubleshooting).
These are copies of the run’s data. Changing input or vars in your code doesn’t change them for other steps. To pass something on, return it.
Numbers in your code are JavaScript numbers. Everywhere else in an automation a number stays exactly as it arrived, but in a Function step 1.10 reads as 1.1, and a whole number longer than 15 digits, such as an order ID, can change in its last digits. Keep long IDs as text: send them in quotes, or read them with String(...) before doing anything else with them.
A step ID with spaces or other characters needs brackets: step['Get orders'].output.
Return a result
Section titled “Return a result”Return an object. Its fields become the step’s output, so later steps read them as {{step.<stepId>.output.<field>}}.
- Return text, a number, true or false, or a list on its own, and it becomes
result:return ['a', 'b'];is read as{{step.<stepId>.output.result}}. - Return nothing, and the step succeeds with an empty output.
Example: total an order
Section titled “Example: total an order”With a webhook body of {"items": [{"name": "Desk", "price": 120}, {"name": "Lamp", "price": 35.5}]}:
const items = input.items || [];
let total = 0;for (const item of items) { total += Number(item.price) || 0;}
return { count: items.length, total: total, summary: `${items.length} items, $${total.toFixed(2)}`,};If this step’s ID is FUNCTION_1, a later step reads {{step.FUNCTION_1.output.total}} (155.5) and {{step.FUNCTION_1.output.summary}} (“2 items, $155.50”).
Example: filter an earlier step’s results
Section titled “Example: filter an earlier step’s results”const tickets = step.CONNECTOR_1.output.tickets || [];const urgent = tickets.filter((t) => t.priority === 'urgent');
return { urgent: urgent, urgentCount: urgent.length, team: vars.team,};A Loop can then go through {{step.FUNCTION_2.output.urgent}}.
Errors
Section titled “Errors”If your code throws, or a promise it awaits is rejected, the step fails and the run stops. The error appears in the test panel and in Logs.
An await on something that never finishes stops the step with “awaited something that never settled”. Only promises from sdk resolve inside a Function step.
Function or Smart Function
Section titled “Function or Smart Function”Smart Function, also in Logic, is “AI-powered business logic with SDK access”: you describe what it should do and it can write the code for you. Use Function when you want to write and control the code yourself. See the step reference. A Smart Function’s code is async function execute(sdk) { … }, and there is no vars in it: it reads a variable with sdk.getContext().getVariable('x') and a run input with sdk.getInput('x'). The expressions reference has the full list.