> ## 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, Rhino C#, Blender Python, Revit C# or ArchiCAD Python 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.

## Asking the agent to write code

The most common way to use the Code node is to let the [agent](https://docs.runchat.com/concepts/agents/chat.md) write and maintain the code for you. Select a Code node and describe what you want it to do in the composer. 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 File Tools](https://docs.runchat.com/concepts/agents/capabilities.md#code-file-tools) for the full list of edits it can make.

For complex tasks (multi-step logic, non-trivial data transformations), switch the agent to a flagship reasoning model like Claude Opus or Gemini 3 Pro before asking it to write code. Open the agent menu (the chat title) and choose **Model: {name}**.

From inside the code editor, click **Ask agent** to close the editor and jump to the agent composer.

### Reviewing agent changes

When the agent edits code, the changed lines are highlighted in the code editor. Click **Accept changes** to keep them or **Undo changes** to revert. Manually editing the code clears the highlights and accepts the agent's changes implicitly.

## The code editor

Select a Code node and click **Edit code** on its toolbar, or press **E**, to open the code editor. It has:

* A file list. JavaScript nodes can hold several files; click **New file** to add one.
* **Run** (or **Stop** while running). Press **Ctrl/Cmd+Enter** to run from the keyboard.
* An **Inputs** panel for the node's input parameters. See [Inputs and outputs](#inputs-and-outputs).
* A result panel with **Output** (the node's result), **Console** (`console.log` output and errors from the last run) and **Outputs** (each output and its type).

Press **Esc** to close the editor. Edits save as you type.

![A Code node with the language menu open](https://mintcdn.com/runchat/rUDQhtaRtUR5pk6i/images/nodes/code-language-selector.webp?fit=max\&auto=format\&n=rUDQhtaRtUR5pk6i\&q=85\&s=a0d211fbb58ea56ba92d6461f08a0017)

## Choosing a language

The language menu on the node toolbar (and in the code editor header) has six modes:

* **JavaScript**: runs server-side in a sandbox. Use for data wrangling, transforming values between nodes, calling APIs. Supports multiple files and npm packages.
* **HTML**: renders a live web page in the node. Use for interactive tools, dashboards, 3D viewers, and small apps.
* **Rhino Python**: runs inside the [Rhino plugin](https://docs.runchat.com/plugins/rhino.md) on the active document
* **Rhino C#**: runs inside the [Rhino plugin](https://docs.runchat.com/plugins/rhino.md) on the active document (Rhino 8+, plugin 0.3.77+)
* **Blender Python**: runs inside the [Blender plugin](https://docs.runchat.com/plugins/blender.md) on the active document
* **Revit C#**: runs inside the [Revit plugin](https://docs.runchat.com/plugins/revit.md) on the active document
* **ArchiCAD Python**: runs inside the [Archicad plugin](https://docs.runchat.com/plugins/archicad.md) on the active project

You can switch modes 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

A single-file HTML node shows the live page directly. When a node has several files, a **Result** / **Preview** toggle on the node switches between the returned value and the live page.

You can reference connected input parameters inside your HTML; they are available as variables when the page runs.

## CAD plugin modes

When set to Rhino Python, Rhino C#, Blender Python, Revit C# or ArchiCAD Python, the code is sent to the matching desktop plugin to run on the active document. Inputs arrive as variables; print to send results back. When Runchat is open inside that app, the node toolbar shows **Run in {host}** (for example **Run in Rhino**), which runs the script in your local instance via the [bridge plugin](https://docs.runchat.com/plugins/rhino.md).

In Rhino Python and Rhino C#, set a `runchat_result` variable to return data: it becomes the node's result as JSON. Rhino C# scripts already import `System`, `System.Linq`, `System.Collections.Generic`, `Rhino` and `Rhino.Geometry`, and print with `Console.WriteLine`.

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:

* Inputs arrive in the entry file as variables. The entry file must `return` a value, which becomes the node's output.
* To use npm packages, add a `package.json` (and any helper modules) with **New file**. The agent can do this for you.
* The execution timeout is 60 seconds per item.

> [!NOTE]
>
> `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.

> [!NOTE]
>
> **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 we can provide a secure environment for running untrusted code without risking breaches.

## Inputs and outputs

The Code node has no inputs besides its code by default. To use parameters in your script, open the code editor and add them in the **Inputs** panel: click **Add input**, then set each input's **Name** (what you reference in your script) and type (any incoming value is cast to this type before your code runs). Remove an input with its remove button.

Each input appears as a handle on the left side of the node. When the agent is helping you write code, it can also add inputs 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 and arrays render as data, with **Previous item** / **Next item** paging for lists

In the editor's **Outputs** tab:

* **One output per key**: return an object to get a separate output handle for each key. See [Spread output](https://docs.runchat.com/concepts/nodes/parameters.md).
* **Publish all**: automatically publish all generated outputs.

### Each, All and Flatten on an input

When a list is connected to an input, a list-handling chip on the input row sets how the script receives it (see [Data Matching](https://docs.runchat.com/concepts/data/data-matching.md)). The chip's menu shows how many items come in and how many runs that produces.

* **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]`.
* **Flatten**: nested lists are flattened into a single list first.

```js theme={null}
// Each 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 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](https://docs.runchat.com/concepts/integrations/credentials.md) 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()`.

Type `ENV.` in the code editor to open the **Insert credential** list 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](https://docs.runchat.com/concepts/data/data-matching.md): how Each and All interact with multi-input scripts
* [Credentials](https://docs.runchat.com/concepts/integrations/credentials.md): securely store and use API keys
* [Agent Capabilities](https://docs.runchat.com/concepts/agents/capabilities.md): tools the agent uses to edit code
* [Generating Rhino scripts](https://docs.runchat.com/examples/generating-rhino-scripts.md): worked example using Rhino Python mode


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.