nodes / builtin / core
Code
Run custom python code
Run your own Python code on the incoming data when no other node does what you need.
What it does
Section titled “What it does”Code runs the Python in Code to Execute and passes on the value of its last line. The language is Python only (Python 3 with its standard library, running in the browser). There is no JavaScript mode.
- The incoming data is available as a variable named
input. - The value of the final expression becomes the output, under
result. - Anything you
print()appears in the run’s activity log, which helps with debugging. - Each run starts from a clean slate 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.
When to use it
Section titled “When to use it”- A calculation or text transformation that would take many nodes otherwise.
- Reshape a complex structure: regroup a list, merge fields, count values.
- Parse an unusual format with Python’s standard library (
re,json,csv,datetime,statistics).
For simple field changes, Edit Fields or Math need no code. To call an API, use Http Request.
Inputs and settings
Section titled “Inputs and settings”| Setting | Type | What it does |
|---|---|---|
| Code to Execute (required) | Code | The code will be executed in the context of the node. |
Reading input
Section titled “Reading input”input is a normal Python value (a dict, a list or a single value), the same data that {{ $input }} holds in other nodes. Its shape depends on how many nodes are connected:
-
One node connected.
inputis that node’s output as it produced it:# input == {"selectionText": "Hello", "triggerType": "contextmenu"}input["selectionText"] # -> "Hello" -
Several nodes connected.
inputis a dict keyed by each source node’s label, so you can tell them apart:# input == {# "Get Selected Text": {"selection": "Hello"},# "Http Request": {"data": {...}},# }input["Get Selected Text"]["selection"] # -> "Hello"A node with a blank label is keyed by its id. If two nodes share a label, both keys get a short suffix, such as
"Http Request [a1b2]". -
Nothing connected.
inputis an empty dict,{}, soinput.get("selectionText", "")is always safe.
Returning a value
Section titled “Returning a value”Put the value you want to pass on as the last line, on its own. An assignment on the last line returns nothing.
words = input.get("selectionText", "").split(){ "words": len(words), "longest": max(words, key=len) if words else "",}Outputs
Section titled “Outputs”-
Output
Each output item has these fields:
| Field | Type | What it holds |
|---|---|---|
| result | Any | What the code returned; null when it returned nothing |
{ "result": null}Read the returned value downstream with {{ $input.result }}, or a field of it with {{ $input.result.words }}. The value travels as JSON: dicts become objects, sets become lists, integers too large for JSON become text, and values with no JSON form (such as class instances) are returned as their text form.
Dependencies and credentials
Section titled “Dependencies and credentials”None. This node needs no connection and has no slots for other nodes.
Example workflow
Section titled “Example workflow”Count the words in text you select on a page:
- Context Menu trigger, Menu Contexts
Selection. - Code with the snippet from Returning a value above.
- Browser Notification, Title
Word count, Message{{ $input.result.words }} words, longest: {{ $input.result.longest }}.
Common issues
Section titled “Common issues”- “Network access is disabled in the Code node sandbox. Use an HTTP Request node to call APIs.” The code tried to fetch something. Fetch it with Http Request before the Code node and read the response from
input. - “Code execution timed out after 30000ms”. The code ran longer than 30 seconds (start-up included), often an endless loop. Simplify it or split the work across items.
resultisnull. The last line is an assignment, aprint()orNone. End with the value itself, for exampleouton its own line.ModuleNotFoundError. Only the Python standard library is available; packages such asnumpyorrequestscannot be installed.KeyErroroninput[...]. The field is not there, or several nodes are connected andinputis keyed by node label. Useprint(input)to see its shape in the activity log.
Related nodes
Section titled “Related nodes”- Edit Fields: set or rename fields without code.
- Http Request: call APIs, which the Code node cannot do.
- JSON: reshape JSON without code.
- Debug: inspect the data between steps.