Skip to main content
Code Execution runs a piece of JavaScript (or TypeScript) on the data of the run. It is the step for everything a model should not be asked to do: arithmetic, formatting, merging the results of a Map, calling an external system, deciding something deterministic. You do not need to be a developer to use it for simple things, and the editor has an AI Code Assistant (⌘ L on macOS, Ctrl L elsewhere) that writes or changes the code from a description. This page explains what the code receives and what it must return, so you can read and check what the assistant produces. In the running example, Normalize Values sits after Extract Data and turns the extracted number into a formatted amount, builds an approval code and flags contracts above the threshold.
Code Execution step with the code editor, the Available Libraries bar and the Terminal

The execute function

The code must declare a function named execute that receives input and returns the step’s output. The step is created with this template:
Whatever execute returns becomes the output of the step: a text, a number, an object or a list. Return an object with named fields when later steps need to pick values from it; those fields show up in the @ menu of the next steps.
Start the code with a short comment that says what the step does, which inputs it uses and what it returns. Whoever opens the Workflow later, including the AI assistant, reads it before the code. A function that is missing or not named execute fails the step with the message: The execute(input) function was not found in the code and is required.

What input contains

input is an object with one key per Input of the Workflow and one key per step that ran before this one, using their names exactly as written in the editor: Names with spaces need the bracket form, input["Extract Data"]; names without spaces can use input.requester. The editor knows the shape of what each previous step returns and offers completion while you type, so input["Extract Data"]. lists the attributes of its Output Format.
Add a console.log(JSON.stringify(input, null, 2)) on the first run. The Terminal below the editor shows everything the code writes to stdout and stderr, with the exit code, so you can see the real shape of the data before writing the logic.

Returning values

  • Return an object to expose named fields: { total_value, is_high_value }. Each field is selectable by later steps and by a Condition.
  • Return a list to feed a Map.
  • Return a text or number for a single value.
  • Returning nothing (undefined) produces an empty output; the step still succeeds.
  • Throw an error (throw new Error("Missing total")) to fail the step on purpose. The run fails at that step and the message appears in the result pane and in Debug.
A Code Execution step has no Output Format to fill in: the fields of the object you return are its output format. The editor infers them from the code so the next steps can offer them in the @ menu.

Working with files

File values arrive as metadata, not as bytes. To read a file’s content, use the libraries available in the step:
  • @meistrari/document-sdk to parse documents;
  • @meistrari/vault-sdk to read and write files in the Vault;
  • @meistrari/tela-sdk-js to call the Tela API (for example run a Canvas) from the code.
These three are installed by default and listed under Available Libraries. To add another package (for example xlsx to read a spreadsheet or pdf-lib to assemble a PDF), type its name in Add a library and click Install. Installation takes a few seconds and the package is then available with import or require.
How to produce a file as the output of a Code Execution step (for example upload a generated spreadsheet to the Vault and return its reference) depends on the Vault SDK and is not documented here. For generating documents from a template, use the Document Templater instead.

Using credentials

Never paste an API key in the code. Store it as a Credential and read it at run time:
credential("NAME") returns the value of a Workspace or Workflow-scoped credential. A missing credential fails the step with missing_credential. Values are redacted from logs, but anything you console.log explicitly may still show, so do not print them. Admins can restrict which credentials a Workflow may use from the Credentials button in the header; see Credentials.

Limits

A Code Execution step is never cached: it runs on every execution of the Workflow. A run that makes no progress for 3 hours is marked as timed out, so keep long waits out of the code; a wait of hours belongs to a schedule, not to a step.

Debugging

After a run, the step’s section in the result pane shows the returned value, and Debug adds the Terminal (stdout, stderr and exit code) and the error message when the code threw. When a Code Execution step fails, fix the code and use Run Step N on it to re-run from the start up to that step without touching the steps after it. See Running, testing and publishing.
For developers: the step accepts code, language (javascript default; typescript is accepted) and libraries (npm package specifiers). The sandbox exposes the run state as globals, so input is built from the Inputs (text unwrapped to its content, files as { files: [...] }) and the outputs of previous steps keyed by step name. {{step://…}} references are not expanded inside code.