---
title: Code
description: Run custom python code
---

Run your own Python code on the incoming data when no other node does what you need.

## 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

* 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](/nodes/builtin/datatransformation/editfields/) or [Math](/nodes/builtin/datatransformation/math/) need no code. To call an API, use [Http Request](/nodes/builtin/core/http-request/).

## Inputs and settings

### 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.** `input` is that node's output as it produced it:

  ```python
  # input == {"selectionText": "Hello", "triggerType": "contextmenu"}
  input["selectionText"]  # -> "Hello"
  ```

* **Several nodes connected.** `input` is a dict keyed by each source node's label, so you can tell them apart:

  ```python
  # 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.** `input` is an empty dict, `{}`, so `input.get("selectionText", "")` is always safe.

### 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.

```python
words = input.get("selectionText", "").split()
{
    "words": len(words),
    "longest": max(words, key=len) if words else "",
}
```

## Outputs

Each output item has these fields:

```json title="Shape of an output item"
{
  "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

None. This node needs no connection and has no slots for other nodes.

## Example workflow

Count the words in text you select on a page:

1. [Context Menu](/nodes/builtin/trigger/contextmenu/) trigger, **Menu Contexts** `Selection`.
2. Code with the snippet from **Returning a value** above.
3. [Browser Notification](/nodes/extension/browsernotification/), **Title** `Word count`, **Message** `{{ $input.result.words }} words, longest: {{ $input.result.longest }}`.

## 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](/nodes/builtin/core/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.
* **`result` is `null`.** The last line is an assignment, a `print()` or `None`. End with the value itself, for example `out` on its own line.
* **`ModuleNotFoundError`.** Only the Python standard library is available; packages such as `numpy` or `requests` cannot be installed.
* **`KeyError` on `input[...]`.** The field is not there, or several nodes are connected and `input` is keyed by node label. Use `print(input)` to see its shape in the activity log.

## Related nodes

* [Edit Fields](/nodes/builtin/datatransformation/editfields/): set or rename fields without code.
* [Http Request](/nodes/builtin/core/http-request/): call APIs, which the Code node cannot do.
* [JSON](/nodes/builtin/datatransformation/json/): reshape JSON without code.
* [Debug](/nodes/builtin/core/debug/): inspect the data between steps.