Skip to content

How do I run custom code in an automation?

The Code step runs your own code over the items passing through. It’s the escape hatch for anything the visual steps can’t express. It’s faster, cheaper and more predictable than asking an AI to do string or number work.

JavaScript, TypeScript, Python, PHP and Lua.

  • Your code runs once, with the whole list of incoming items available as items.
  • Return the result: JavaScript, TypeScript, PHP and Lua use return. In Python, assign to result (return also works).
  • What you return becomes the step’s output.
// JavaScript: add VAT and a formatted total to every order
return items.map((order) => ({
...order,
vat: Math.round(order.total * 0.16),
totalText: `KES ${order.total.toLocaleString('en-KE')}`,
}));
# Python: keep Kenyan numbers and normalise them to 2547…
import re
result = [
{**row, "phone": "254" + row["phone"][-9:]}
for row in items
if re.match(r"^(\+?254|0)7\d{8}$", row["phone"])
]

items holds the data coming in. To bring in a value from another step, set it as a field with Set Fields just before the Code step.

  • Code has a timeout (5 seconds by default), so a mistake can’t hang your workflow.
  • Code can’t read the server’s settings or secrets.
  • Python, PHP and Lua run in an isolated sandbox with no network or file access.
  • If you need to call an API, use the HTTP Request step and pass its result into Code.

Questions

Which languages does the Code step support?

JavaScript, TypeScript, Python, PHP and Lua.

How do I return a value from Python?

Assign it to `result` (a `return` also works). JavaScript, TypeScript, PHP and Lua use `return`.

Can my code call an API?

Python, PHP and Lua have no network access. Call the API with an HTTP Request step and pass its result into Code.