Skip to main content
A Code node runs JavaScript when a conversation flow enters the node. Use it for calculations, data formatting, and lightweight HTTP lookups that don’t require your own server. The code can read dynamic variables and call metadata, then store fields from its return value for later nodes. For example, a Code node can normalize an appointment timestamp, return an appointment_label, and let the flow route based on whether the timestamp was valid. If you use a single- or multi-prompt agent, see Code Tool.
Conversation flow canvas with a Code node that has an Open button for its code configuration and an outgoing transition.

A Code node on the conversation flow canvas.

When to use a Code node

Use a Code node only for logic that can run without secrets. Dynamic variables and metadata are stored in plaintext with the call or chat record. For authentication, secret management, internal systems, or production writes, use a custom function hosted on your backend.

Configure a Code node

1

Add a Code node

Select Code from the action nodes in the left sidebar.
Conversation flow action-node menu with Code between Extract Variable and MCP.

Code in the conversation flow action-node menu.

2

Open the code editor

Select the node, then select Open under Code Configuration.
3

Write JavaScript

Read values from dv or metadata and return a JSON-serializable value. Objects are the easiest return type to map into response variables.
4

Store fields as variables

Under Store Fields as Variables, map a dynamic-variable name to a path in the returned value. For the example above, map appointment_is_valid to valid and appointment_label to appointment_label.See Store response variables for nested and array paths.
5

Configure execution and transitions

Set the timeout in the code editor. In the node settings, choose whether the agent talks or plays a typing sound while the code runs and whether the flow waits for the result before transitioning.
6

Test the code

Use Dynamic Variables in the editor to add test values, then select Run Code. The result and any console.log() output appear below the editor.Test values stay in the dashboard and don’t change the live agent. The dashboard test doesn’t populate metadata, so metadata-dependent code must handle an empty object during testing.

JavaScript environment

The code runs inside a QuickJS sandbox. Top-level await is supported.

dv: dynamic variables

Read dynamic variables as properties of dv. All values in dv are strings, including values stored by earlier nodes.
Use dv.user_number, not {{user_number}}, inside JavaScript. Runtime values such as call_id, chat_id, session_type, direction, user_number, and agent_number are available when they apply to the current session.
Dynamic variables are available through dv inside JavaScript. {{...}} template substitution isn’t available inside code.

metadata: call or chat metadata

Read metadata passed when you create the phone call or chat.
Metadata values can use JSON types. Unlike values in dv, they aren’t limited to strings.

fetch(url, options): HTTP requests

fetch() supports HTTP and HTTPS URLs, request methods, string-valued headers, and request bodies. The returned response supports status, ok, statusText, url, headers.get(), text(), and json().
The sandbox blocks non-HTTP protocols and selected local, private, and platform addresses. It doesn’t provide the complete browser Fetch API.

console.log(): test output

Use console.log() while testing. Logs appear in Run Code results. They aren’t added to the live code result or shown as Code node logs in call logs.

Runtime limits

The sandbox provides common JavaScript built-ins such as Array, Date, JSON, Map, Math, Promise, RegExp, and Set. Node.js modules, browser DOM APIs, require, import, and Intl aren’t available.

Store response variables

Response variables copy fields from the full return value into dynamic variables. For this return value:
configure these mappings: Stored values are strings. Objects and arrays are stored as JSON strings. If a path doesn’t exist or resolves to null, Retell skips that variable without failing the execution. Response-variable extraction uses the full return value, even when the result sent to the agent is capped at 15,000 characters.

Control transition timing

Wait for Result is on by default.
  • When it is on, Retell waits for the code to finish before evaluating the node’s transition conditions. The return value and stored response variables are available to those conditions and the next node.
  • When it is off, the flow can transition while the code is still running. The result and stored variables might not be available to the next node.
If Talk While Waiting is on, the agent delivers the configured prompt or static sentence before transitioning. If the caller speaks while the code is running, the agent can still respond to that turn. Turning Talk While Waiting off suppresses the node’s entry message; it doesn’t block responses to caller interruptions. Use transition conditions that check stored result fields before the agent confirms an action succeeded.

Node settings

Use Code node safely

  • Don’t put API keys, credentials, or sensitive tokens in code, dv, or metadata.
  • Use fetch() for low-risk HTTP reads. Put authenticated requests and state-changing operations behind a custom function on your backend.
  • For critical routing, provide a safe else path. A timeout, thrown error, or sandbox failure doesn’t retry automatically and doesn’t extract response variables.

FAQ

No. The sandbox doesn’t provide require, import, Node.js modules, browser DOM APIs, or Intl. Use the available JavaScript built-ins and fetch().
Runtime values are available as properties of dv, including dv.call_id, dv.user_number, and dv.agent_number when they apply. Use dv.name syntax to access them. {{...}} template substitution isn’t available inside JavaScript.
Retell marks the execution as failed, doesn’t extract response variables, and doesn’t retry automatically. Use try/catch for expected errors and return a structured fallback such as { "ok": false, "error": "lookup failed" }. For critical routing, configure a safe else path.
The setting suppresses the node’s entry message. If the caller speaks while the code is running, that user turn can still trigger an agent response before the code result is available. Ground any success confirmation in a stored result field instead of conversation context alone.
Yes. Top-level await is supported, including for fetch() calls.
Retell sends up to 15,000 characters to the agent by default. Response-variable extraction uses the full return value before that result is capped.