On this page
Sommatic Docs
Open portal

Code nodes

What code.js and code.python can use when they run — input, globals, logs, timeouts and output shape — and how to pass them data from other nodes and the run.

View as Markdown
Open in

When the logic outgrows an expression — loops, parsing, calculations over many records — write it in a code node. code.js runs JavaScript and code.python runs Python. Both receive the node’s input and return a value that becomes the node’s output.

JavaScript
const lines = $json.items.filter((item) => item.qty > 0);
const total = lines.reduce((sum, item) => sum + item.qty * item.price, 0);
console.log(`${lines.length} lines`);
return { total, skus: _.map(lines, 'sku') };
Python
def main(input):
    lines = [item for item in input.get("items", []) if item.get("qty", 0) > 0]
    total = sum(item["qty"] * item["price"] for item in lines)
    print(f"{len(lines)} lines")
    return {"total": total, "skus": [item["sku"] for item in lines]}

Output: result and logs

Both nodes wrap what you return:

JSON
{ "result": { "total": 42, "skus": ["A", "C"] }, "logs": ["2 lines"] }

The next node reads $json.result.total; nodes further down read $('Compute totals').item.json.result.total. In code.js, logs is a list of the lines you logged; in code.python it is the text printed to the console. Errors, syntax errors and timeouts leave through the error port with the message in the output.

code.js

Your code runs as the body of an async function: use return to produce the result and await when you need it. If nothing is returned, result is null.

AvailableNotes
$jsonThe node’s input.
$input$input.item, $input.first() and $input.last() return the input itself (not { json }); $input.all() returns a list with it.
console.log, console.warn, console.errorCaptured in logs.
_lodash.
JSON, Math, Date, String, Number, Boolean, Array, ObjectStandard built-ins.
parseInt, parseFloat, isNaN, isFiniteNumber parsing.
encodeURIComponent, decodeURIComponent, encodeURI, decodeURIURL encoding.
RegExp, Map, Set, PromiseStandard built-ins.

The expression variables $('…'), $node, $execution, $now and the $ helpers are not available inside code.js. The default timeout is 5,000 ms; change it with execution.timeout_ms. This page covers the default execution mode; the node page lists the others.

code.python

Define a main(input) function. input is the node’s input as a dictionary; return a value that can be serialized to JSON (values that cannot, such as dates, are converted to text).

SettingNotes
packagesPyPI packages to install before your code runs, such as ["pandas"]. They go into a temporary environment created for each execution, so installing adds time to every run.
execution.timeout_msDefault 30,000 ms. Raise it for OCR or heavy processing.

print writes to logs, not to the result. A file without main(input) fails.

Pass data from other nodes

Code nodes see only their input. When the code needs the output of earlier nodes or the run ids, put a data.transform before it that builds that input with an expression:

JavaScript
{{ ({ order: $('Fetch order').item.json, customer: $('Find customer').item.json, run: $execution.id, today: $now.setZone('America/Bogota').toISODate() }) }}

The code then reads $json.order, $json.customer, $json.run and $json.today (or input["order"]… in Python).

Next steps

Install agent tools

Give your coding agent the official Sommatic AI docs: skills, the docs MCP server and Markdown pages, so it never has to scrape the site.

Skills only. They do not update by themselves; run it again to refresh.

npx skills add https://docs.sommatic.ai
How agents read these docs