Skip to main content
A Custom Code gambit runs a short piece of JavaScript in a sandbox, in the middle of a conversation. It reads the flow’s variables, and writes new ones back.

When to use it

Use it for the small transformations no other gambit does. Reformat a date, derive a total, normalize a phone number, pick a field out of an earlier API response. For fetching data, use API Call. For anything reusable across agents, write it as a CodeKit action instead.

The gambit and its editor

Every page in this section shows the same demo flow: Start, a Welcome message, the gambit this page covers, then End. The gambit sits third, selected, with its editor open on the right. The rest of the screen is dimmed. The editor holds Configuration and the Conditions switch. It has no Messages section.

Configuration

What the code can reach

The editor seeds a new gambit with a comment naming what is in scope:
  • variables is a read-only snapshot of the conversation, grouped into namespaces: response, api, post, flow, agent, system, utm, url, enduser, and trigger. An input gambit’s capture lives at variables.response.<name>, keyed by the name in its VARIABLE field.
  • setVariable(name, value) writes into the flow namespace. Read it back as variables.flow[name] in code, or as {{flow.name}} in messages and conditions.
  • The returned value is stored in this gambit’s own variable. Return null when the code only writes through setVariable.
An error does not stop the conversation. The flow moves on, and this gambit’s variable holds an object with an error field instead of a result. Guard against missing variables, and branch on that error field where a failure matters.

Branching

The gambit has one Default branch, and a Conditions section that can route on anything the code wrote. See Flow control and branching.