
Code Node vs Custom Function
| Code Node | Custom Function | |
|---|---|---|
| Runs | JavaScript in Retell’s sandbox | HTTP request to your server |
| Requires | Nothing — runs directly | Your own API endpoint |
| Best for | Data transformation, simple API calls, logic & calculations | Complex integrations, accessing internal systems |
| Max code size | 5,000 characters | N/A (runs on your server) |
Write Your Code
Write JavaScript
Write your JavaScript code in the editor. You have access to dynamic variables, call metadata, and the 
fetch function for HTTP requests. See JavaScript Environment below for details.
Set response variables (optional)
Use Store Fields as Variables to extract values from your code’s return value and save them as dynamic variables. Specify a variable name and the JSON path to the value.For example, if your code returns
These variables can then be referenced as
{ "status": "shipped", "estimated_delivery": "March 25" }:| Variable Name | JSON Path | Extracted Value |
|---|---|---|
order_status | status | "shipped" |
delivery_date | estimated_delivery | "March 25" |
{{order_status}} and {{delivery_date}} in other nodes.Test your code
Click Run Code at the bottom of the editor to test. Use the Dynamic Variables dropdown in the editor to set test values for your variables (e.g., give 
customer_name a value of “John Doe”) — these values are only used during testing and won’t affect your live agent. The output panel will show the result and any console.log() output.
JavaScript Environment
Your code runs in a JavaScript sandbox with the following globals available. The code editor provides autocomplete — as you type, it will suggest available globals, dynamic variable names, and built-in functions.dv — Dynamic Variables
Access your agent’s dynamic variables as properties on the dv object. All values are strings.
metadata — Call Metadata
Access metadata passed when the call was created via the API. This is the same object you pass in the metadata field of the Create Call API.
fetch(url) — HTTP Requests
Make HTTP requests to external APIs. Works like the standard Fetch API.
console.log() — Debugging
Log output for debugging. Logs appear in the test output panel when using Run Code, and are also available in call logs.
- Your code can return any value (object, string, number) or return nothing at all.
- Standard JavaScript built-ins are available:
Math,JSON,Date,Array,Object,Stringmethods, etc. - External packages (
require,import) are not available. Usefetch()for external integrations. - Code is limited to 5,000 characters.
Examples
Format data from dynamic variables
Fetch data from an external API
Conditional logic with API call
Response Variables
Response variables let you extract specific values from your code’s return value and store them as dynamic variables for use in other nodes. Specify each variable as a name and a JSON path using dot notation:| Path Syntax | Example | Extracts |
|---|---|---|
| Top-level field | status | result.status |
| Nested field | data.order.id | result.data.order.id |
| Array element | items[0].name | First item’s name |
When Can Transition Happen
- If Wait for Result is turned off:
- If Speak During Execution is on, the agent transitions once done talking
- If Speak During Execution is off, the agent transitions immediately after code starts running
- If the user interrupts the agent, transition can happen once the user is done speaking
- If Wait for Result is turned on:
- If Speak During Execution is on, the agent transitions once code finishes and agent is done talking
- If Speak During Execution is off, the agent transitions once code finishes
- If the user interrupts the agent, transition can happen once code finishes and user is done speaking
Node Settings
- Speak During Execution: When enabled, the agent says something while the code runs (e.g., “Let me check that for you.”). Choose between Prompt (LLM generates the message) or Static Text (exact text you provide).
- Wait for Result: When enabled, the agent waits for the code to finish before transitioning. This guarantees that when you reach the next node, the result and extracted variables are ready.
- Timeout: How long the code can run before timing out. Range: 5–60 seconds. Default: 30 seconds.
- Global Node: Read more at Global Node.
- Block Interruptions: When enabled, the agent will not be interrupted by the user when speaking.
- LLM: Choose a different model for this node. Used for speak during execution message generation if set to Prompt.
- Fine-tuning Examples: Can finetune transition. Read more at Finetune Examples.
FAQ
Can I use npm packages or external libraries?
Can I use npm packages or external libraries?
No. The code runs in a lightweight JavaScript sandbox without access to
require or import. You can use all standard JavaScript built-ins (Math, JSON, Date, Array methods, etc.) and the fetch() function for external API calls.What happens if my code times out?
What happens if my code times out?
If your code exceeds the configured timeout (default 30 seconds), it will be stopped and treated as a failed execution. The result will contain a timeout error message. You can adjust the timeout in Node Settings (5–60 seconds).
What happens if my code throws an error?
What happens if my code throws an error?
If your code throws an error or crashes, the execution is marked as failed and the error message is returned as the result. Response variables will not be extracted. You can use
try/catch in your code to handle errors gracefully.Can I use async/await?
Can I use async/await?
Yes. The
fetch() function is async, so you can use await to wait for HTTP responses. Top-level await is supported.Is there a limit on the result size?
Is there a limit on the result size?
Yes, the result is capped at 15,000 characters to prevent overloading the LLM context.
