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

# Code Node

> Write custom JavaScript code to transform data, perform calculations, or implement custom logic

## Overview

The **Code Node** lets you write custom JavaScript code that runs during your workflow. Use it when you need logic that goes beyond what other nodes offer: custom transformations, calculations, string manipulation, or any data processing that requires full programmatic control.

## Required Configuration

* **JavaScript Code:** The code to execute. Must include a `return` statement that defines the node's output.

## Optional Configuration

* **Label:** Display name for the node (defaults to "Code")
* **Requires Auth:** Whether this node needs user authentication (default: false)

## Accessing Data

Inside your code, use the `$()` function to reference data from other nodes:

* **`$("Node Label")`:** Access any previous node's output by its label
* **`$("startNode")`:** Access the original input from the frontend
* **`__user_id__`:** The authenticated user's ID (available when Requires Auth is enabled)

Your code must include a `return` statement. The returned value becomes the node's output.

```javascript theme={null}
// Example: transform data from a previous node
const data = $("Query Users");
const result = data.map(item => ({
  ...item,
  fullName: item.firstName + " " + item.lastName,
  processed: true
}));

return result;
```

## Connections

Code nodes fit anywhere in your workflow:

**Input from:** Any node type that produces data

**Output to:** Any node type that consumes data:

* Mutation nodes (save processed results)
* Query nodes (use computed values as parameters)
* HTTP Request nodes (build custom request payloads)
* Agent nodes (prepare structured input for AI)
* If-Else nodes (route based on computed values)
* Edit Fields nodes (further reshape output)
* Return nodes (end workflow with computed output)
* Other Code nodes (chain complex logic across steps)

## Common Use Cases

**Custom Data Transformation.** Reshape, merge, or filter data in ways that Edit Fields cannot express.

**Calculations and Aggregation.** Sum totals, compute averages, or derive new values from input data.

**String Processing.** Parse, format, or validate strings with full regex and string method support.

**Conditional Logic.** Apply complex branching or business rules that go beyond simple if-else checks.

### Example Workflows

* **Data Pipeline**: Query Node → Code (aggregate results) → Mutation (save summary)
* **API Integration**: HTTP Request → Code (parse and validate response) → Edit Fields (format output)
* **User Processing**: Form Input → Code (validate and transform) → If-Else (route by result)

## Related Nodes

<CardGroup cols={2}>
  <Card title="Edit Fields Node" icon="pen-to-square" href="/nodes/edit-fields-node">
    Transform data with a visual field mapper
  </Card>

  <Card title="If-Else Node" icon="code-branch" href="/nodes/if-else-node">
    Route workflows based on conditions
  </Card>

  <Card title="For Loop Node" icon="rotate" href="/nodes/for-loop-node">
    Iterate over arrays of data
  </Card>

  <Card title="Return Node" icon="right-from-bracket" href="/nodes/return-node">
    End workflows with computed output
  </Card>
</CardGroup>
