Skip to content
DocsOpen Hub
Node reference

Function node

On this page

Use a Function node to validate input, transform JSON, calculate a value, or prepare the final API response. Cloudgate executes the node's Python script and stores its returned result under the node name.

Input and output#

Reference request and workflow data directly with selectors. For a JSON order in the request body, use ${body}:

python
import json

order = ${body}
if not isinstance(order, dict):
    raise ValueError("Send an order object")
items = order.get("items", [])
if not isinstance(items, list):
    raise ValueError("items must be a list")

return json.dumps({
    "orderId": order.get("id"),
    "itemCount": len(items)
})

If this node is named OrderSummary, later nodes can reference ${OrderSummary}. A top-level return is supported by the Cloudgate function wrapper. Use json.dumps for a JSON response rather than Python's string representation of a dictionary.

Choose a selector for the value you need#

Use order = ${FetchOrder} to read a JSON result from a node named FetchOrder. Use a quoted selector such as order_id = "${orderId}" when you want its text representation. Cloudgate's selector binding keeps resolved values as data, including quotes and newlines.

Prior node names are selectors, not variables automatically imported into Python. FetchOrder by itself is not the upstream result; ${FetchOrder} is. An unquoted JSON selector supplies its parsed value, so you do not need json.loads just to read a JSON request or node result.

Assign a value for later nodes#

Use a session selector assignment when later nodes need a separately named value:

python
order = ${body}
${session:OrderId} = order["id"]
return "Order selected"

Later nodes in this run can reference ${OrderId}. The assignment follows normal Python control flow: code in an unselected branch does not run. Session values belong to this execution; use a database for durable records and a stored Key Selector for shared configuration.

Design reusable logic#

Keep the function focused and deterministic where possible. Validate types before arithmetic or indexing. Return one documented shape for success and let validation failures produce a useful error without including private input.

Reusable code snippets can help share a known pattern, but inspect the inserted code and required selectors for each workflow. A snippet is not proof that its data contract matches the current action.

Debug a failure#

Inspect the node error, resolved input, and preceding node's output in a sandbox run. Check malformed JSON, missing keys, null values, and empty collections. Available Python packages and execution limits depend on the deployed runtime; do not assume a local machine's modules are installed in Cloudgate.

Need to investigate a problem? Start with troubleshooting