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

# Code

> Run JavaScript, HTML, Rhino Python, Blender Python, or Revit C# inside a workflow

The Code node runs custom logic inside a Runchat workflow. It is one of the most flexible nodes in the platform, useful for everything from data wrangling to building interactive web apps to driving CAD plugins.

<iframe width="100%" height="400" src="https://www.youtube.com/embed/LAi66wAiC54" title="Code node walkthrough" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen />

## Asking the chat agent to write code

The most common way to use the Code node is to let the [chat agent](/concepts/agents/chat) write and maintain the code for you. Select a Code node and describe what you want it to do. The agent reads your existing code (if any), sees error messages from the last run, and applies changes line by line. See [Agent Capabilities → Code Editing Tools](/concepts/agents/capabilities#code-editing-tools) for the full list of edits it can make.

For complex tasks (multi-step logic, non-trivial data transformations), switch the chat agent to a flagship reasoning model like Claude Opus or Gemini 3 Pro before asking it to write code.

### Reviewing agent changes

When the agent edits code, the changed lines are highlighted in the editor and an **Accept / Undo** toolbar appears in the top-right corner with a count of changed lines. Click the green check to keep the changes or the undo arrow to revert. Manually editing the code clears the highlights and accepts the agent's changes implicitly.

## Editing code yourself

You can also write or tweak code directly in the editor. The editor is intentionally minimal:

* Standard text editing keybindings (cut, copy, paste, undo, redo)
* **Ctrl+F** auto-formats the whole script (works on Mac too, with literal Ctrl rather than Cmd)
* `Tab` opens the credential picker. See [Using credentials](#using-credentials) below.

## Choosing a language mode

<Frame>
  <img src="https://mintcdn.com/runchat/J8Y8DQ43swL1M50L/images/nodes/code-language-selector.webp?fit=max&auto=format&n=J8Y8DQ43swL1M50L&q=85&s=038ba76d4a5d13fb6b252c021527c2e6" alt="A Code node with the language selector dropdown open showing JavaScript, HTML, Rhino Python, Blender Python, and Revit C# options" width="562" height="686" data-path="images/nodes/code-language-selector.webp" />
</Frame>

The settings bar at the bottom of the node has a language selector with five modes:

* **HTML**: renders a live web page in a preview panel. Use for interactive tools, dashboards, 3D viewers, and small apps.
* **Rhino Python**: runs inside the [Rhino plugin](/plugins/rhino) on the active document
* **Blender Python**: runs inside the [Blender plugin](/plugins/blender) on the active document
* **Revit C#**: runs inside the [Revit plugin](/plugins/revit) on the active document
* **JavaScript**: runs server-side in a sandbox. Use for data wrangling, transforming values between nodes, calling APIs.

You can switch modes from the dropdown at any time without losing your code.

## HTML mode

In HTML mode, your code is rendered as a live web page in the node. Use it for:

* Building interactive tools (paint, color pickers, sliders, mask editors)
* 3D viewers using Three.js or similar
* Dashboards or data visualizations
* Quick UI prototypes

In the settings bar, you can toggle between **Editor** view (writing the HTML) and **Preview** view (the live rendered page).

You can reference connected input parameters inside your HTML, Runchat injects the values before rendering.

## CAD plugin modes

When set to Rhino Python, Blender Python, or Revit C#, the code is sent to the matching desktop plugin to run on the active document. The Runchat node shows the script and the run button connects to your local Rhino/Blender/Revit instance via the [bridge plugin](/plugins/rhino).

These modes are great for generating geometry, automating repetitive tasks, or scripting analyses.

## JavaScript mode

JavaScript runs in a sandboxed environment with a few important constraints:

* Your code must `return` a value. The runtime uses the return value as the node's output.
* External `import`s and `require`s are not available, you can only use built-in JavaScript.
* The execution timeout is 60 seconds per item.

If you need a library that isn't built in, write the logic as a cloud function (e.g. on Google Cloud Run, Cloudflare Workers) and call it with `fetch()`.

<Info>
  `fetch()` requests cost 1 credit each in addition to the per-node run cost.
  Networking requests can be used to send your data to unknown third parties; if
  a workflow contains a code node that references your user credentials, you'll
  be warned and asked to proceed only if you trust the source.
</Info>

<Info>
  **Why can't I do *x* with the code node?**

  Most Runchats use the code node in some way, often nested several nodes deep for doing various forms of data formatting and manipulation. It is crucial that this code cannot make any changes to your data or pc without your knowledge. By sandboxing all code and preventing access to modules we can provide a secure environment for running untrusted code without risking breaches.
</Info>

## Inputs and outputs

The Code node has only the `code` input by default. To use other parameters in your script, add them with the **Parameter Manager** in the settings panel.

When the chat agent is helping you write code, it can also suggest and add parameters for you.

The output is rendered with a component that matches the returned data type:

* A returned URL to an image, video, 3D mesh, or PDF renders inline
* HTML strings render in a preview frame
* Markdown renders formatted
* JSON or arrays render in a paginated table

## Parameter Manager

You can add input parameters from the node settings panel. Click the **+** icon to add one and the trash icon to remove it. Each parameter has:

* A **name** you'll reference in your script
* A **data type** that any incoming value will be cast to before your code runs

After adding, the parameter appears as a handle on the left side of the node in alphabetical order.

### Each vs All on a parameter

Each parameter connection has an **Each** or **All** toggle on the edge (see [Data Matching](/concepts/data/data-matching)):

* **Each** (default): the script runs once per item, with that item bound to the parameter name.
* **All**: all incoming items are passed in as a single array. Connect multiple inputs to the parameter and access them through array index notation, e.g. `inputData[1]`.

```js theme={null}
// Each mode on both
// param_1: [1,2,3], param_2: ["a","b"]
return [param_1, param_2];
// run 1: [1, "a"]
// run 2: [2, "b"]
// run 3: [3, "b"]   // last item reused for shorter parameter

// All mode on param_1, Each on param_2
return [param_1, param_2];
// run 1: [[1,2,3], "a"]
// run 2: [[1,2,3], "b"]

return param_1[0];
// run 1: 1
```

## Using credentials

Reference any [credential](/concepts/integrations/credentials) directly in your script with `ENV.<name>` or `ENV["<name>"]`. Credentials are placeholders that only resolve to real values when sent to an allowed origin via `fetch()`.

You can also press `Tab` in the code editor to open the credential picker and insert a credential by name.

```js theme={null}
const response = await fetch("https://api.example.com/data", {
  headers: { Authorization: `Bearer ${ENV.my_api_key}` },
});
return await response.json();
```

## Common errors

The sandbox always runs your code as a function and expects a return value. If your code doesn't return anything, the node will throw:

```text theme={null}
"undefined" is not valid JSON
```

Add a `return` statement to whatever value should be the output.

## Next steps

* [Data Matching](/concepts/data/data-matching): how Each and All interact with multi-input scripts
* [Credentials](/concepts/integrations/credentials): securely store and use API keys
* [Agent Capabilities](/concepts/agents/capabilities): tools the chat agent uses to edit code
* [Generating Rhino scripts](/examples/generating-rhino-scripts): worked example using Rhino Python mode
