> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cassidyai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Run code

> Execute custom JavaScript code within a Workflow to transform data, perform calculations, or implement custom logic.

Execute custom JavaScript code within a [Workflow](/workflows/overview). 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.

<Steps>
  <Step title="Add the action">
    In the Workflow builder, click **+** between blocks and select **Run Code** from the action library.

    <Frame>
      <img src="https://mintcdn.com/cassidy/3d6b3JQqVZ3Bg1_p/images/reference/run-code-img-0.png?fit=max&auto=format&n=3d6b3JQqVZ3Bg1_p&q=85&s=50e1740074dbe617cedd8109fd25d78e" alt="Action library with Run Code selected" width="1052" height="290" data-path="images/reference/run-code-img-0.png" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/cassidy/3d6b3JQqVZ3Bg1_p/images/reference/run-code-img-1.png?fit=max&auto=format&n=3d6b3JQqVZ3Bg1_p&q=85&s=3300c24bf9aa4207e361388e34f4fafa" alt="Adding the Run Code action to the Workflow" width="1020" height="1246" data-path="images/reference/run-code-img-1.png" />
    </Frame>
  </Step>

  <Step title="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.

    <Frame>
      <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-3.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=88d98152db7d46001c509174ad8da80a" alt="NPM packages search and selection in Advanced Settings" width="1008" height="384" data-path="images/reference/run-code-img-3.png" />
    </Frame>
  </Step>

  <Step title="Write your code">
    In the code editor, write your custom JavaScript code. [Reference variables](/guides/prompt-engineering#workflow-prompts) 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](/workflows/advanced/secret-keys).

    <Frame>
      <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-4.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=dbff4db1b7bf60ffcac284e624af7f3a" alt="Code editor with variable references from previous steps" width="988" height="480" data-path="images/reference/run-code-img-4.png" />
    </Frame>

    When using NPM packages, include the import statement at the top of your code.

    <Frame>
      <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-5.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=1597f0f952be5fb32a2251f28fa539e6" alt="Code editor showing NPM package import statement" width="682" height="56" data-path="images/reference/run-code-img-5.png" />
    </Frame>
  </Step>

  <Step title="Generate code with Copilot">
    Optionally, click **Edit with Copilot** above the code editor to have AI write or modify code for you.

    <Frame>
      <img src="https://mintcdn.com/cassidy/Kfn4ClNgZxHtTNl2/images/reference/run-code-img-6.png?fit=max&auto=format&n=Kfn4ClNgZxHtTNl2&q=85&s=ed935557779d0c76959035c1598585cc" alt="Edit with Copilot button above the code editor" width="994" height="460" data-path="images/reference/run-code-img-6.png" />
    </Frame>

    Enter a prompt describing what you want the code to do.

    <Frame>
      <img src="https://mintcdn.com/cassidy/Kfn4ClNgZxHtTNl2/images/reference/run-code-img-7.png?fit=max&auto=format&n=Kfn4ClNgZxHtTNl2&q=85&s=2c86e6a976b2e4e6ee1d72f3fc9e22bd" alt="Copilot prompt describing the desired code change" width="1010" height="514" data-path="images/reference/run-code-img-7.png" />
    </Frame>

    Submit your prompt and Copilot generates the code. Review the result and adjust as needed.

    <Frame>
      <img src="https://mintcdn.com/cassidy/Kfn4ClNgZxHtTNl2/images/reference/run-code-img-8.png?fit=max&auto=format&n=Kfn4ClNgZxHtTNl2&q=85&s=63cff53258fecdd17ecfa62f20fe8cc4" alt="Generated JavaScript code from the Copilot prompt" width="1016" height="1274" data-path="images/reference/run-code-img-8.png" />
    </Frame>
  </Step>

  <Step title="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.

    <Frame>
      <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-9.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=7dd5d43190aadc9c4ee6149895806de9" alt="Auto-detect from code toggle enabled with detected output fields" width="1010" height="602" data-path="images/reference/run-code-img-9.png" />
    </Frame>

    * **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](/workflows/advanced/structured-output-fields).

    <Frame>
      <img src="https://mintcdn.com/cassidy/3d6b3JQqVZ3Bg1_p/images/reference/run-code-img-10.png?fit=max&auto=format&n=3d6b3JQqVZ3Bg1_p&q=85&s=99044c551b71f36785da49be1a6d3566" alt="Manually defined output fields with field name and data type" width="1008" height="524" data-path="images/reference/run-code-img-10.png" />
    </Frame>
  </Step>
</Steps>

## 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:

<Frame>
  <img src="https://mintcdn.com/cassidy/3d6b3JQqVZ3Bg1_p/images/reference/run-code-img-13.png?fit=max&auto=format&n=3d6b3JQqVZ3Bg1_p&q=85&s=8372e3e886dc45dfae75bf11b4413673" alt="CassidyFile constructor wrapping a file variable from a previous step" width="994" height="192" data-path="images/reference/run-code-img-13.png" />
</Frame>

### 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:

<Frame>
  <img src="https://mintcdn.com/cassidy/3d6b3JQqVZ3Bg1_p/images/reference/run-code-img-14.png?fit=max&auto=format&n=3d6b3JQqVZ3Bg1_p&q=85&s=cf278d327b81223a8404b3ca59048fd1" alt="CassidyFile class methods for reading file content as string, base64, or buffer" width="958" height="448" data-path="images/reference/run-code-img-14.png" />
</Frame>

The `cassidy.files` object also provides convenience methods for constructing and working with files:

<Frame>
  <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-15.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=89ad77bdb147028cd9f353b306b45246" alt="cassidy.files helper methods for creating and working with files" width="930" height="844" data-path="images/reference/run-code-img-15.png" />
</Frame>

### 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:

<Frame>
  <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-16.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=e3d1b8c96565c3b28c02128b2ff94ba9" alt="Returning a file in the output object of a Run Code action" width="796" height="392" data-path="images/reference/run-code-img-16.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/cassidy/Won7bFEKpcJoIUEp/images/reference/run-code-img-17.png?fit=max&auto=format&n=Won7bFEKpcJoIUEp&q=85&s=4763db30fc0fc8c8ff91d6e58c2a1ec3" alt="File output available as a variable in subsequent Workflow steps" width="1030" height="298" data-path="images/reference/run-code-img-17.png" />
</Frame>

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.

<Tip>For more information on creating files across Cassidy, see [How to create files in Cassidy](/guides/create-files).</Tip>

## Related

* [Build and configure Workflows](/workflows/building)
* [Secret Keys](/workflows/advanced/secret-keys)
* [Structured Output Fields](/workflows/advanced/structured-output-fields)
* [How to create files in Cassidy](/guides/create-files)
