Skip to main content
Execute custom JavaScript code within a Workflow. Use this for data transformations, calculations, API calls, or custom logic that can’t be handled by other actions. Your code runs in a secure, sandboxed environment.
1

Add the action

In the Workflow builder, click + between blocks and select Run Code from the action library.
Action library with Run Code selected
Adding the Run Code action to the Workflow
2

Configure NPM packages

Click Advanced Settings to access additional configuration. In the NPM Packages section, search and select any JavaScript libraries you need for your code. Dayjs (date formatting) and axios (HTTP requests) are included by default.
NPM packages search and selection in Advanced Settings
3

Write your code

In the code editor, write your custom JavaScript code. Reference variables from previous steps or the trigger using # to use data from earlier in the Workflow. For managing secret API keys or other secure information, see Secret Keys.
Code editor with variable references from previous steps
When using NPM packages, include the import statement at the top of your code.
Code editor showing NPM package import statement
4

Generate code with AI

Optionally, click Generate with AI above the code editor to have AI write code for you.
Generate with AI button above the code editor
Enter a prompt describing what you want the code to do. Press # to insert a variable reference in your prompt.
AI prompt field with variable reference for code generation
Click Generate to create the code. Review the result and adjust as needed.
Generated JavaScript code from the AI prompt
5

Configure output fields

The return value of your JavaScript code becomes the output of the action. In the Output Fields section, you have two options:
  • Auto-detect from code (recommended): When enabled, Cassidy analyzes the return statement in your code to automatically determine available outputs. These detected fields appear in the Detected Fields section and can be referenced in later Workflow steps.
Auto-detect from code toggle enabled with detected output fields
  • Define output fields manually: Toggle off Auto-detect from code to manually add fields. Click + Add Field to specify the field name and data type for each output value. For more details, see Structured Output Fields.
Manually defined output fields with field name and data type

Working with files in Run Code

The Run Code action supports reading, creating, transforming, and outputting files using the built-in CassidyFile class and the cassidy.files helper methods. This lets you process file data, generate new files, download files from URLs, and pass files to subsequent Workflow steps.

Inputting files

To work with a file variable from a previous Workflow step, wrap the variable in the CassidyFile constructor:
CassidyFile constructor wrapping a file variable from a previous step

Working with files

The CassidyFile class provides several convenience methods to get the content in different formats, including as a string, in base64, or as a buffer:
CassidyFile class methods for reading file content as string, base64, or buffer
The cassidy.files object also provides convenience methods for constructing and working with files:
cassidy.files helper methods for creating and working with files

Outputting files

To output files from the action, return them in the output object. These will be available to use in later actions of the Workflow:
Returning a file in the output object of a Run Code action
File output available as a variable in subsequent Workflow steps
The returned file can then be referenced as a variable in subsequent Workflow steps, such as Upload Knowledge Base File or any other action that accepts file inputs.
For more information on creating files across Cassidy, see How to create files in Cassidy.