When code is the right tool
Section titled “When code is the right tool”Most changes need no code: drag a field, write an expression, or use Edit Fields. Use the Code node when you need to:
- regroup or count values, for example orders per customer;
- parse an unusual format with Python’s standard library (
re,json,csv,datetime); - combine several fields with logic that doesn’t fit in one expression.
Read the input, return a value
Section titled “Read the input, return a value”words = input.get("text", "").split(){ "words": len(words), "longest": max(words, key=len) if words else "",}inputis a normal Python value: a dict, a list or a single value.- The last line, on its own, is what the node passes on. An assignment on the last line returns nothing.
- The next step reads it as
{{ $input.result.words }}. print()writes to the run’s activity log, which helps when you debug.
With several steps connected to the Code node, input is a dict keyed by each step’s label, such as input["Get Selected Text"]["selection"].
One item, or the whole list?
Section titled “One item, or the whole list?”When the previous step outputs several items, the Code node runs once per item and input is one item. To work on the whole list at once (to total it or group it), put Aggregate in front of the Code node. It then receives one item that holds the list.
totals = {}for order in input["orders"]: totals[order["customer"]] = totals.get(order["customer"], 0) + order["amount"]totalsLimits
Section titled “Limits”Each run starts clean in an isolated sandbox: no network, no access to the page or your stored data, and a 30-second time limit. The first run after a pause takes a couple of seconds while Python starts. To call an API, use Http Request before or after the Code node.
You’ll notice this when…
Section titled “You’ll notice this when…”The output is null
The last line is an assignment (total = 3) or a statement, so nothing is returned. Put the value on its own as the last line (total).
The Code node ran once per row but you wanted one total
It received a list of items, so it ran for each one. Add Aggregate before it.
input[“title”] raises a KeyError
Several steps are connected, so input is keyed by step label, or the field is missing on this item. Use input.get("title", ""), and check the shape in the previous step’s Output tab.