Code

Run arbitrary Python logic inside a workflow node. The Code node gives you a full Python function body with access to the upstream input value and workflow context.

When to Use

Use Code when:

  • You need logic that cannot be expressed with Transform, If, or Switch (loops, calculations, string manipulation, data processing)
  • You want to use a Python library that is not available as a built-in plugin
  • You need to transform arrays, perform arithmetic, or build complex conditional logic

Inputs

HandleTypeDescription
inputanyThe upstream value passed into your function as the input variable.

Output

HandleTypeDescription
resultobjectThe value returned by your function. Must be a Python dict or JSON-serializable value.

The result is accessed in downstream nodes as {{ code.* }}.

Settings

SettingTypeDescription
languageenumAlways python for now.
codecode editorThe Python function body. The input and ctx variables are pre-injected.

Writing Code

Your code runs inside an async Python function. The following variables are available:

VariableTypeDescription
inputanyThe value connected to the input handle
ctx.workflow_idstringThe current workflow's ID
ctx.run_idstringThe current execution run's ID

Return a dictionary or any JSON-serializable value:

# input is the upstream value
return {
  "result": input["amount"] * 1.1,
  "formatted": f"${input['amount'] / 100:.2f}"
}

Example

Extract the most recent commit from a GitHub push event and format a Slack message:

commits = input.get("commits", [])
if not commits:
    return {"message": "No commits in this push."}

latest = commits[0]
return {
    "message": f"New commit by {latest['author']['name']}: {latest['message'][:80]}",
    "url": latest["url"],
    "author": latest["author"]["name"]
}

Installing Python Packages

Packages listed in the workflow's Python dependencies are installed automatically at runtime. Add packages through the workflow settings, not inside the code node itself.

Standard library packages (like json, re, datetime, os) are always available.

Accessing Environment Variables

import os
api_key = os.environ.get("MY_API_KEY", "")

Set environment variables in the Env Vars page.

Common Gotchas

  • Return type: Your code must return a value. If you return None or forget the return statement, {{ code.result }} will be null downstream.
  • Async: The function context is async. You can use await for async operations, but standard synchronous code works without await.
  • Input is optional: If nothing is connected to the input handle, input will be None.
  • Errors: An unhandled exception in your code node will fail the entire execution path. Use try/except to handle expected failures gracefully.

See Also

On this page