Functions
Write JavaScript, host it in Regal, and manage it with the editor or Copilot.
Overview
Functions let you write JavaScript and run it directly in Regal. Use a function to clean up data, reshape a payload, call an API, or apply a business rule, without any code living in another system.
Every function declares what it takes in and what it returns. Regal hosts and runs the code, keeps a version history, and lets you test before anything goes live. You can build and manage functions in the editor or ask Copilot to do it for you.
Once your function is active, see Run Function Action to call it from an AI Agent, or use it in a journey as described below.
Key Use Cases
| Use Case | Why It's Powerful |
|---|---|
| Data cleanup | Normalize phone numbers, emails, and transcribed input before your agent uses them |
| Date and time logic | Handle time zones, days of the week, and timestamp formats in code instead of in the prompt |
| System lookups | Call your CRM or another API and hand your agent only the fields it needs |
| Business rules | Apply eligibility or routing logic the same way every time |
Build Functions with Copilot
Copilot can handle the full lifecycle of a function. Describe what you need in plain language and Copilot can:
- Create a function and write the code
- Update an existing function when your requirements change
- Activate a draft so it goes live
- List and review the functions in your account
- Test a function by running it with sample inputs
- Attach a function to your AI Agent as a custom action
- Delete a function you no longer need
For example: "Create a function that validates an email address and returns an empty value if it's invalid, then add it to my Intake agent."

Functions created by Copilot appear in the Functions list like any other. You can edit, test, and version them in the editor.
Create a Function in the Editor
If you'd rather write the code yourself, here is how.
-
Go to Orchestration and open Functions.

-
Click Create function and give it a name and a short description. New functions open with a working starter template.
-
Write your handler. Here is a function that cleans up an email address:
exports.handler = async (event, context) => { const email = event.params.email.toLowerCase().replace(/\s+/g, ""); const isValid = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email); return { validated_email: isValid ? email : "" }; }; -
Define the input schema and output schema. Add one field per input and per output, each with a name, a type (string, number, integer, or boolean), and a description.

-
Save, then activate the function.
Use Functions in Journeys
Journeys can run the same functions your AI Agents use. Add a function to a journey node, map its inputs, and use its outputs in any node that comes after it.
Add a function to a journey
- Open your journey and add a Function node.
- Select an active function. Its inputs appear for mapping.
- Choose a version. Leave it on Always use latest, or pin a specific active version.
- Map each input (see below).
- Save the node.

Map your inputs
Each input on the function needs a source. Choose one per input:
| Source | Use it when |
|---|---|
| Contact Attribute | The value already lives on the contact's profile |
| Event Attribute | The value comes from the event that triggered the journey |
| Static Value | The value is the same every time the node runs |
| Previous Node Response | The value came from an earlier function or webhook node in the journey. |
Use the output in later nodes
A function's output fields are available to every node after it in the journey.
- In a webhook node: Reference an output field wherever you'd use any other value, such as the URL or request body.
- In another function node: Map an input and choose Previous Function or Webhook Response as the source, then pick the output field.
For example, a function that returns validated_email can feed a webhook that creates a lead, with no extra code in between.
Watch for Input Schema ChangesJourneys aren't able to resolve new input variables using AI like Functions in AI Agents are. If you're using a Function node and the input schema changes in a new live version, it will break. Pin versions to prevent this from happening.
Use Secrets in Your Function
Functions often call an API, and those calls usually need a credential such as an auth header. Don't paste credentials into your code, since anyone who can open the function can read them. Store them as secrets and reference them from the function instead.
Secrets are managed on your brand's Secrets page. You associate the secrets a function needs with that function, and the function reads them at run time. The value never appears in your code.
-
Go to the Secrets page and add a secret with a name and value.
-
Open your function and associate the secret with it.

-
Reference the secret in your handler with
context.secrets.<name>. Here is a function that calls an API with a secret in the header:JavaScript
exports.handler = async function (event, context) { const axios = require("axios"); const secretValue = context.secrets.FUNCTIONS_API_KEY; const response = await axios.post( "url", { headers: { "X-Functions-Api-Key": secretValue, }, } ); return { status: response.status }; };
How Your Code Works
- Inputs arrive in
event.params. Return your result as an object that matches your output schema. - There are no HTTP status codes or headers. Return the outcome as a normal field, such as
successorerror_code, so your agent can act on it. - If your code throws an error, the agent receives a generic function error. Use output fields for anything you want the agent to handle.
- Functions can use a curated set of packages:
axios,got,luxon,moment-timezone,google-libphonenumber,qs, and Node's built-incrypto. You can't add your own packages.
Drafts and Versions
Edits save as a draft. Activating a draft makes it a live version. You can compare versions to see what changed, and reactivate an earlier version to roll back.
Pin a Function Version
When you add a function to an AI Agent action or a journey node, you choose which version it runs:
- Always use latest (default): The action or node runs the latest active version, so a change takes effect on the next call.
- A specific version: The action or node stays on that version, even after you activate newer ones, until you manually select a different version.
Pinning lets you keep iterating on a function without breaking what already depends on it. For example, if you change a function's input schema, any journey node mapped to the old inputs breaks until you update its mappings. Pin the node to the stable version, make your change, and move the node to the new version once its inputs are mapped correctly.
Only active versions can be pinned. Existing actions and nodes were set to Always use latest, so nothing changes until you pin a version.

New Live Versions Affect Production TrafficActivating a version affects live traffic right away for every action and node set to Always use latest. Pinned actions and nodes are not affected.
Test Your Function
- Open the function and click Test function.
- Enter sample values for each input.
- Run the test and review the output or error.

Test Your FunctionTests run your code for real. If your function writes to a CRM, the test writes to it too.
Troubleshooting & FAQ
Can I edit a function that Copilot created?
Yes. Open it from the Functions list and edit it like any other function.
My function returns an empty or unexpected value.
Run it from the Test function drawer with the same inputs and compare the output to your output schema.
My change isn't showing up.
Confirm you activated the new version. Drafts don't run on live calls.
Can I import a package that isn't on the list?
Not today. Reach out to your Regal team to ask about adding one.
My function can't read a secret.
Confirm the secret exists on the Brand Secrets page, is associated with this function, and that the name in context.secrets.<name> matches exactly.
I activated a new version, but my agent or journey still behaves the old way.
Check the version setting on the action or node. If it's pinned to a specific version, it stays there until you select a new one.
Can I pin a draft?
No. Only active versions can be pinned.
Updated 17 days ago

