Availability
Custom code must be included in your plan. Check Settings → Plan or ask your workspace admin. It is not available in reseller customer workspaces. Without it, the Code tool card on the Tools page opens the plan upgrade dialog, and saving or testing a code tool is refused with “Custom code is not included in this workspace’s plan.” Plain code in a Run Code step keeps working on every plan. Only code that exportsrun (a module) needs custom code.
A code tool works on every channel an assistant supports: voice calls, web chat, messaging channels and email. Simulations never run your code.
Try the Airtable example
A new code tool opens with an Airtable status lookup. In Test, select Run with the prefilled record IDdemo. The result marks the data as a sample and makes no requests.
To read one of your records, set base_id and table_id in Configure. Add a bearer token in Credentials, name it airtable, and give it read access to that base. In Test, replace demo with a record ID. The example reads a text field named Status. Change the field name in the code if your table uses a different one.
The editor checks syntax as you type and highlights the error line. Run the test again after editing the code or its inputs to verify the current version. Test with your sample records before assigning the tool to an assistant.
Build a code tool
An online shop wants the phone assistant to tell callers when their parcel arrives. The tool looks up the order, then asks the carrier for the status of the shipment.1
Add the tool
Open Agents → Tools, select Add and choose Code tool. Name it in snake case, for example
get_delivery_status. In Description, tell the assistant when to use it: “Looks up the delivery status of an order. Use it when the customer asks where their package is.”2
Write the code
Export an asynchronous function named
run. It receives the values you configure and returns the answer for the assistant:3
Add inputs and credentials
On the Configure tab, add the input
order_id and describe it for the assistant: “Order number as printed on the confirmation, for example SO-12345.” Under Credentials, add the connection with the shop’s token and name it shop.4
Set the waiting behavior
Under Message while running, enter what callers hear, for example “One moment, I’m checking that for you.” With a message, the timeout can go up to 30 seconds. Without one it is 1 to 10 seconds, 5 by default.
5
Test and assign
Open the Test tab, fill in an order number and select Run. Then assign the tool to an assistant under Tools → Add tool in the assistant editor.
Write the code
The code is one JavaScript module that exports a function namedrun:
inputsholds the values you configured, read asinputs.order_id.credentialsholds your saved connections, read ascredentials.shop.timeoutMsis the time left in milliseconds whenrunstarts.- Return value: any JSON value. A string reaches the assistant as it is, everything else as compact JSON, cut after 16,000 characters.
undefinedbecomesnull.
import statement, no import() and no npm packages. Each run starts fresh, so variables at the top of the file do not carry over between runs. The clock is UTC; use Intl.DateTimeFormat with a timeZone for local time.
Return expected outcomes, throw real failures
“No order with this number” is a normal answer. Return it, and the assistant can word it for the caller. Throw anError only when something is really broken, for example when the other system answers with a server error. The assistant then receives “The code failed while running:” with your message and the line, cut to 1,000 characters. Write the message for the assistant.
Available globals
setTimeout, setInterval, Buffer, process, require, eval, WebSocket and streams are not available.
Give the code its inputs
Each input has a name, a type (text, number, whole number or yes/no) and a source:
An input marked Required that has no value stops the run before your code starts. The assistant receives
Missing required value "order_id". If a contact may lack the value, clear Required and handle it in code. A tool has up to 50 inputs, 20 of them secrets.
Use saved credentials
Credentials let your code call a system without a key in the code. Add them under Agents → Tools → Apps, then pick them in the tool or node:- Custom credential: a bearer token, an API key, a username and password, or an HMAC secret. The code reads
credentials.shop.token,credentials.shop.key,credentials.shop.usernameandcredentials.shop.password, orcredentials.shop.secret. - HubSpot: the code reads
credentials.crm.accessTokenandcredentials.crm.apiBaseUrl. - Salesforce: the code reads
credentials.crm.accessToken,credentials.crm.instanceUrlandcredentials.crm.apiVersion. Salesforce tokens expire. If a test fails with an authorization error, reconnect the connection under Agents → Tools → Apps.
[redacted] in results, errors and the test view when they appear unchanged. A secret that your code shortens, encodes or changes stays readable, so never return a key.
Limits
Waiting for a response does not count towards the processing time. Requests to private or internal addresses are blocked, also after a redirect. A workspace can start up to 300 runs per minute; a single tool up to 120, and a single conversation up to 30.
What happens when a run fails
The assistant hears the reason only when your code throws or an input is missing. For limits and system problems it says that the action is not possible right now. The exact reason is in the test result and the run history.Test before you use it
The Test tab runs the code in the editor, saved or not. Fill in the inputs the assistant would pass, and select Run. The result shows:- What the agent receives: the exact text the assistant gets.
- The result, an error with Go to line, the console output, every request with its status and time, and the duration.
Use the code in an automation
The Run Code step in Automations has two modes, chosen by the code:- Plain code is the code your automations already run. It reads
context,data,stepsandlast, has no network, and mustreturna value. It is unchanged and works on every plan. - Module code exports
runand getsinputs,credentials,timeoutMsandworkflow(data,stepsandlastas read-only copies). It can usefetchand credentials. It must return an object, and later steps read its fields assteps.<id>.<field>. The whole object is also available asresult.
order_id to {{data.order_id}}. A value that is only a template keeps its type. If the code fails, the step fails, the run ends as failed and nothing is retried.
Only workspace owners and admins can run or save a code step that uses credentials, because the code can read them. A member can still use an automation an owner or admin has set up.
Before you activate the automation, the check lists a missing plan, a connection that is unknown or belongs to another workspace, and syntax errors with their line. Exporting or sharing an automation keeps the credential names and clears the connections, so the person who imports it picks their own.
To try a module, select Run test in the step settings and confirm. It sends real requests. Connect Test only resolves the inputs and runs no code.
Let Milian write and test it
Describe what the tool should do, and Milian writes the code, sets up inputs and credentials, and assigns the tool. Milian never asks for keys in the chat. If you have no connection yet, it sends you to Agents → Tools → Apps to add a custom credential. Before every test run, Milian describes the requests it will send and asks you to choose Run or Don’t run. A real test of a code node asks the same way. Milian fixes errors by line and asks again before each new run. The run appears in the chat with what the agent receives and a button to open the tool. See Milian.API, MCP, CLI and SDK
Create, update, test and restore code tools withtype: "code" through the API, MCP tools, CLI and SDK.
Next steps
Tools
Find, assign and manage all tools of your workspace
Automation nodes
Use the Run Code step in a workflow