> For the complete documentation index, see [llms.txt](https://docs.stackai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stackai.com/guides-and-quickstarts/workflow-builder/code-execution.md).

# Code execution

The Code node performs calculations and data transformations with code. Use it when a step must follow explicit rules or return a defined data structure.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSQ1Sgy933krTyK0S4JpP%2Fuploads%2F8Wx7r5oP39KxVSrf1O1a%2FG026.mp4?alt=media&token=69b4ca30-652c-464d-bbff-3e3e2fa3bc43>" %}

## Before you start

This guide requires Python or TypeScript programming knowledge. If your task only needs to pass fields or format an answer, start with [Map variables and expressions](https://docs.stackai.com/guides-and-quickstarts/workflow-builder/map-variables-and-expressions.md) or [Structured outputs and templates](https://docs.stackai.com/guides-and-quickstarts/workflow-builder/structured-outputs-and-templates.md). Have a connected workflow and know the input fields your function must receive before writing code.

## Connect and configure Code

Connect the input, Code, and output nodes.

![Input, Code, and Output connections](https://content.gitbook.com/content/SQ1Sgy933krTyK0S4JpP/blobs/eseg9sdDdjiwqus8SJG4/assets/g026-g026-01-canvas.png)

Decide which values the code must receive and which values later nodes will need.

Open Code settings and select Python or TypeScript.

![Code language settings](https://content.gitbook.com/content/SQ1Sgy933krTyK0S4JpP/blobs/7xtlyHgSvBzH3uFpJskM/assets/g026-g026-02-language.png)

The function must use the syntax and return format appropriate for the selected language.

## Write and run the function

Open the code editor and review the function.

![Code function editor](https://content.gitbook.com/content/SQ1Sgy933krTyK0S4JpP/blobs/JHOz5cCyoSxAwoezO8K8/assets/g026-g026-03-code.png)

Return useful values explicitly. Printed log messages help with diagnosis but are not a substitute for the returned result.

Run the code and inspect its result, exit code, logs, and error field.

![Code result, exit code, logs, and error details](https://content.gitbook.com/content/SQ1Sgy933krTyK0S4JpP/blobs/tvns0i8MZex937qhG8p8/assets/g026-g026-04-code-result.png)

A successful execution should return the expected value without an execution error.

## Use the returned values

Insert the required Code result field into the downstream template.

![Code result field in a downstream template](https://content.gitbook.com/content/SQ1Sgy933krTyK0S4JpP/blobs/z3is3din3dmPbEgeZHz3/assets/g026-g026-05-expression.png)

Match the expression path to the actual returned object.

Run the complete workflow and inspect Output.

![Complete workflow output after code execution](https://content.gitbook.com/content/SQ1Sgy933krTyK0S4JpP/blobs/ac6oMUp4awz1NvSjZCEx/assets/g026-g026-06-final-output.png)

This checks that both code execution and the downstream reference produce the intended result.

## Important behavior

Printed messages appear in logs. Return the values that later nodes need from the function.

If a later node reports a missing value, compare its reference with the fields under Code.result.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stackai.com/guides-and-quickstarts/workflow-builder/code-execution.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
