Skip to main content

Overview

Use the Code node to process workflow data directly with custom JavaScript or Python code. Choose it when standard nodes are not enough, for example for calculations, formatting, validation, file processing, or custom business logic. Code

When to Use Code Node

Code nodes are perfect for:
  • Data transformations and formatting
  • Mathematical calculations
  • Custom business logic
  • JSON parsing and manipulation
  • Data validation and cleaning
  • Date/time operations
  • File processing with JavaScript and Python
What code nodes are not ideal for:
  • AI analysis (use Agent node)
  • External API calls (use HTTP Request node)
  • Simple conditions (use Condition node)
  • Long-running or interactive analysis (use Data Analysis)

Configuration

Language: Choose whether to write your code in JavaScript or Python. Code Editor: Write your transformation logic in the selected language in the code editor that opens when you select the Code node. Access Previous Nodes: Outputs from previous nodes are available as variables in the Code node. The available variable names are shown at the top of the code editor. Only upstream nodes whose slug is referenced in your code are loaded; reading from the data map instead loads every upstream output. To use the Code node output later, see Accessing Code Output.

Examples

Calculate Statistics

Validate and Clean Data

Transform and Filter Arrays

Date Operations

JSON Processing

Aggregate and Summarize

Create a File with JavaScript

JavaScript can produce workflow files by returning a files array. Each entry must include fileName, mimeType, and either text (for text/CSV/JSON content) or base64 (for binary content). The runtime uploads the files and attaches them to the node output under _files.
JavaScript

Read Files from Previous Nodes

Files passed from previous nodes, such as form uploads or action outputs, are available in the Python working directory. Each file object carries a path you can pass directly to open() and the original filename under _metadata.name.
Python

Create a File with Python

Files created by Python in the working directory are attached to the node output under _files.
Python

Accessing Code Output

Use the Code node name to access returned values from JavaScript or Python in subsequent nodes:
Files returned by JavaScript or created by Python in the working directory are available under _files:

Language Capabilities

Code node capabilities depend on the selected language.

JavaScript

JavaScript runs in a secure sandbox environment with built-in utility functions:
  • ld.request(): Make HTTP requests
  • ld.log(): Output debugging information
  • Data conversions: CSV, Parquet, Arrow format conversions
  • File creation: Return a files array to expose generated files under _files
  • Standard JavaScript: JSON, Date, Math, Array, Object methods

Complete Utilities Reference

View all available sandbox utilities including data conversions, SQL validation, cryptography, and more.

Python

Python runs in a sandboxed environment without internet access.
  • Use top-level return to set the node output
  • Use print() to write logs
  • Use preinstalled data and document libraries such as pandas, numpy, openpyxl, and pypdf
  • Access previous node outputs as variables when their slugs are valid Python identifiers
  • Read files from previous nodes by passing file["path"] to open()
  • Save files in the working directory to expose them under _files
  • Run without internet access
The JavaScript ld.* utilities are not available in Python.

Best Practices

Return data as objects for easy access in later nodes. This makes it simple to reference specific values in subsequent nodes using dot notation.
Use ||, optional chaining (?.), or Python .get() to provide default values and prevent errors when data is undefined or null.
Wrap risky operations in try/catch for JavaScript or try/except for Python. This helps prevent workflow failures and provides meaningful error messages.
Complex logic might be better in an Agent node. Use code nodes for straightforward transformations and calculations, not for tasks requiring intelligence or context understanding.
Code nodes fail if the combined output of previous nodes exceeds 5 MB. Reduce upstream output size, process fewer loop items, or disable loop output collection before the code node.
Document what your code does for future reference. Clear comments help you and your team understand the logic when revisiting the workflow later.

Limits

Each Code node has a 5 MiB limit on the combined input it receives from previous nodes. Before your code runs, Langdock measures the size of the upstream outputs your code references by slug. If they exceed 5 MiB, the node fails with an error and your code is never executed. Reading from the data map instead loads every upstream output, so the full combined size is measured against the limit. Common causes:
  • A Loop node that collects outputs from many iterations into a single array.
  • A previous node that returns a very large API response, file content, or dataset.
  • Deeply nested objects that cannot be safely measured.
If you hit this limit, do one of the following:
  • Reduce the size of the upstream node output by filtering, summarizing, or selecting only the fields you need.
  • Process fewer items per iteration in any preceding Loop node.
  • Disable output collection on the preceding Loop node when you do not need to read its aggregated results.

Next Steps

Agent

Use AI for intelligent processing

HTTP Request

Fetch external data

Data Analysis

Analyze data with an Agent