<!-- BusyBot node reference — https://busybot.net/tools/code/ -->

> Node: Code (`code`) · Action · v1
> Category: Core Nodes · Credentials: none
> Updated: 2026-08-16

# Code

> Run custom JavaScript or Python code in a sandbox

## Overview

The Code tool executes user-written JavaScript or Python code in a sandboxed environment. It supports two execution modes: "Run Once for All Items" processes the entire input array in a single invocation (the code receives all items via `$input.all()` in JavaScript or a pre-loaded `items` list in Python), and "Run Once for Each Item" runs the code once per input item (accessing `$input.item` in JavaScript or a pre-loaded `item` dict in Python). Code runs isolated from the workflow process and is terminated when it exceeds the configured timeout or memory limit. The code must return (JavaScript) or print JSON to stdout (Python) with the expected item structure. Timeout and memory limits are configurable.

**Category:** Core Nodes  
**Tool Name:** `code`  
**Version:** 1

**Appearance:** Icon: `lucide-Code` | Color: `#2D2D2D`

## Node Type

**Action** — processes input items and produces output

## Input / Output

| Direction | Port(s) |
|-----------|--------|
| Input | `Input` |
| Output | `Output`, `Error` |

## Credentials

This tool does not require any credentials.

### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| Mode | `options` | No | `runOnceForAllItems` | Whether to run the code once for all items or once per item. |
| | | | | Options: `runOnceForAllItems`, `runOnceForEachItem` |
| Language | `options` | No | `javaScript` | The programming language to write code in. |
| | | | | Options: `javaScript`, `python` |
| JavaScript Code (All Items) | `code` | No | _starter script_ | JavaScript code that receives all items via $input.all() and must return an array of items. Also available: $input.first(), $input.last() and $input.length. _(shown when Language is `javaScript` and Mode is `runOnceForAllItems`)_ |
| JavaScript Code (Each Item) | `code` | No | _starter script_ | JavaScript code that receives the current item via $input.item and must return a single item. _(shown when Language is `javaScript` and Mode is `runOnceForEachItem`)_ |
| Python Code (All Items) | `code` | No | _starter script_ | Python code that receives all items as a pre-loaded `items` list. Must print JSON array to stdout. _(shown when Language is `python` and Mode is `runOnceForAllItems`)_ |
| Python Code (Each Item) | `code` | No | _starter script_ | Python code that receives the current item as a pre-loaded `item` dict. Must print JSON to stdout. _(shown when Language is `python` and Mode is `runOnceForEachItem`)_ |
| Timeout (ms) | `number` | No | `30000` | Maximum execution time for the code in milliseconds. |
| Memory Limit (MB) | `number` | No | `128` | Maximum memory the code may use in megabytes. |
| Max Concurrency | `number` | No | `1` | Maximum number of items to process concurrently (each-item mode only). _(shown when Mode is `runOnceForEachItem`)_ |

Each code field opens pre-filled with a short runnable script for its language and mode, which you replace with your own. Only the field matching the current Language and Mode combination is used; the other three are ignored.

## Output Data

Output items are whatever your code hands back. The input item JSON is not passed through on its own — if you want a field kept, your code has to return it — and binary data is not forwarded: your code receives each item's JSON only, and each returned object becomes the JSON of one output item.

| Language | Mode | Contract |
|----------|------|----------|
| JavaScript | Run Once for All Items | `return` an array — each element becomes one output item |
| JavaScript | Run Once for Each Item | `return` a single item object, not an array |
| Python | Run Once for All Items | `print(json.dumps([...]))` — a JSON array, one element per output item |
| Python | Run Once for Each Item | `print(json.dumps({...}))` — a single JSON object |

- Each returned element may be a full item (`{ "json": { "id": 1 } }`) or a bare object (`{ "id": 1 }`), which is wrapped as the item's JSON for you. Anything that is not an object is rejected as an error.
- Returning nothing at all produces no output items.
- In Run Once for All Items mode the number of output items is whatever your code returns — it is not tied to the number of input items, which is what makes this node usable for filtering items out and for generating new ones.
- Items that arrive already carrying an upstream error are not handed to your code; they are reported as errors according to the node's error mode.
- A run that exceeds Timeout (ms) or Memory Limit (MB) fails as an item error, as does code that throws, or Python that prints something other than valid JSON.

Reference the result downstream like any other item, e.g. `{{ $json.total }}`.

## Usage Examples

- Add a computed field to every item using JavaScript
- Filter items with custom Python logic
- Transform all items by restructuring their JSON
- Generate new items from scratch using code
- Deduplicate items using a custom comparison function

## Example Configuration

JavaScript, all items — double a numeric field:

```json
{
  "type": "code",
  "parameters": {
    "language": "javaScript",
    "mode": "runOnceForAllItems",
    "jsCodeAllItems": "const items = $input.all();\nreturn items.map(item => ({ json: { ...item.json, price: item.json.price * 2 } }));",
    "timeout": 30000,
    "memoryLimit": 128
  }
}
```

JavaScript, each item, with a concurrency limit:

```json
{
  "type": "code",
  "parameters": {
    "language": "javaScript",
    "mode": "runOnceForEachItem",
    "jsCodeEachItem": "const item = $input.item;\nreturn { json: { id: item.json.id, slug: item.json.title.toLowerCase().replace(/ /g, '-') } };",
    "maxConcurrency": 4,
    "timeout": 10000,
    "memoryLimit": 128
  }
}
```

Python, all items — filter and transform:

```json
{
  "type": "code",
  "parameters": {
    "language": "python",
    "mode": "runOnceForAllItems",
    "pythonCodeAllItems": "import json\nactive = [i for i in items if i['json'].get('active')]\nresult = [{'json': {'id': i['json']['id'], 'email': i['json']['email'].lower()}} for i in active]\nprint(json.dumps(result))",
    "timeout": 30000,
    "memoryLimit": 256
  }
}
```

Python, each item, with a concurrency limit:

```json
{
  "type": "code",
  "parameters": {
    "language": "python",
    "mode": "runOnceForEachItem",
    "pythonCodeEachItem": "import json\nresult = {'json': {'name': item['json']['name'].strip(), 'score': round(item['json']['score'], 2)}}\nprint(json.dumps(result))",
    "maxConcurrency": 8,
    "timeout": 5000,
    "memoryLimit": 128
  }
}
```

Use `runOnceForAllItems` when you need to reduce the item set before passing downstream:

```json
{
  "type": "code",
  "parameters": {
    "language": "javaScript",
    "mode": "runOnceForAllItems",
    "jsCodeAllItems": "return $input.all().filter(item => item.json.status === 'active');",
    "timeout": 30000,
    "memoryLimit": 128
  }
}
```

Use `runOnceForEachItem` when each item is processed independently with no cross-item logic:

```json
{
  "type": "code",
  "parameters": {
    "language": "javaScript",
    "mode": "runOnceForEachItem",
    "jsCodeEachItem": "const item = $input.item;\nconst fullName = `${item.json.firstName} ${item.json.lastName}`;\nreturn { json: { ...item.json, fullName } };",
    "maxConcurrency": 10,
    "timeout": 30000,
    "memoryLimit": 128
  }
}
```

Use Python's `runOnceForAllItems` for data aggregation across the full item set:

```json
{
  "type": "code",
  "parameters": {
    "language": "python",
    "mode": "runOnceForAllItems",
    "pythonCodeAllItems": "import json\ntotal = sum(i['json']['amount'] for i in items)\nprint(json.dumps([{'json': {'total': total, 'count': len(items)}}]))",
    "timeout": 30000,
    "memoryLimit": 128
  }
}
```

Use Python's `runOnceForEachItem` for per-item transformations that use Python-native libraries:

```json
{
  "type": "code",
  "parameters": {
    "language": "python",
    "mode": "runOnceForEachItem",
    "pythonCodeEachItem": "import json, re\nclean = re.sub(r'[^a-zA-Z0-9 ]', '', item['json']['text'])\nprint(json.dumps({'json': {'text': clean}}))",
    "maxConcurrency": 5,
    "timeout": 15000,
    "memoryLimit": 128
  }
}
```

### Error Handling

| Mode | Behavior |
|------|----------|
| **stop** | Halts workflow on first error |
| **continue** | Skips failed items, passes successful ones through |
| **errorPort** | Routes failed items to Error output port |

## Tips

Executes custom JavaScript or Python code in a secure sandbox to transform, filter, or generate workflow data. Use when you need programmatic logic, calculations, or complex transformations that can't be achieved with other built-in tools. Produces one or more items as returned by the user code, depending on the execution mode.