> ## Documentation Index
> Fetch the complete documentation index at: https://docs.b3os.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Run JavaScript

Write custom JavaScript to transform data any way you need — format numbers, sort arrays, calculate values, merge objects, and more. Define a transform(input) function; the result is available as {{nodeId.result.output}}. Runs in a secure sandbox with ES6+ support. Reads and mutates workflow variables via getVar('name') / setVar('name', value) — both are scoped to variables declared on the workflow.

Built-in action Built In

Write custom JavaScript to transform data any way you need — format numbers, sort arrays, calculate values, merge objects, and more. Define a transform(input) function; the result is available as &#123;&#123;nodeId.result.output&#125;&#125;. Runs in a secure sandbox with ES6+ support. Reads and mutates workflow variables via getVar('name') / setVar('name', value) — both are scoped to variables declared on the workflow.


## At a Glance

| Field | Value |
| --- | --- |
| Action ID | `code-transform` |
| Category | Built In |
| Connector | Not required |
| Requires gas | No |
| Funds movement | None declared |
| Tags | `code`, `transform`, `javascript`, `custom`, `data-processing`, `compute` |

## Payload Schema

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `code` | `string` | Yes | Write a 'transform\(input\)' function. 'input' is whatever you pass in the Input field below. Full ES6+ support, sandboxed \(no network/filesystem\). Read workflow variables with 'getVar\('name'\)' \(throws if the name is not declared on the workflow\). Write them with 'setVar\('name', value\)' — value type must match the declared type, max 60 KiB per value. Writes commit atomically with node completion. |
| `input` | `object` | Yes | The data passed to your transform\(input\) function. Whatever you reference here becomes 'input' in your code. For example, if you set this to &#123;&#123;node.result.items&#125;&#125;, then 'input' is that array directly. |
| `timeout` | `number` | No | Execution timeout in milliseconds \(default: 5000ms, max: 10000ms\) |
| `inputType` | `object` | No | Declares the expected input type for validation \(optional\). Example: &#123;"type": "array"&#125; |
| `outputType` | `object` | No | Declares the output type for validation and variable suggestions \(optional\). Example: &#123;"type": "array", "items": &#123;"type": "object", "properties": &#123;"name": &#123;"type": "string"&#125;&#125;&#125;&#125; |

## Result Schema

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `output` | `string \| number \| boolean \| null \| array \| object` | Yes | The result returned by your transform function. Can be any JSON-serializable value \(primitive, object, array, nested structures\). The output is available to subsequent workflow actions via &#123;&#123;nodeId.result.output&#125;&#125; or &#123;&#123;nodeId.result.output.field&#125;&#125; for object results. |
| `executionTimeMs` | `number` | Yes | Code execution time in milliseconds |
| `language` | `string` | Yes | Language used for execution |

## Examples

**Workflow node**

```json
{
  "type": "code-transform",
  "payload": {
    "code": "// When input='{{node.result.exchanges}}' (array), input IS the array:",
    "input": "{{prevNode.result.items}}"
  },
  "children": []
}
```
  **Test with API**

```bash
curl -X POST "https://api.b3os.org/v1/actions/code-transform/test" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "inputs": {
    "code": "// When input='{{node.result.exchanges}}' (array), input IS the array:",
    "input": "{{prevNode.result.items}}"
  }
}'
```

**Use expressions for dynamic values**

Payload fields can use workflow expressions such as `{{$trigger.body.amount}}`, `{{$nodes.fetch.result.price}}`, and `{{$props.asset}}` when the value should come from a trigger, prior node, or reusable workflow prop.