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.
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 Python
- 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. 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 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:
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 requestsld.log(): Output debugging information- Data conversions: CSV, Parquet, Arrow format conversions
- 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
returnto 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 workflow attachments from the working directory
- Save files in the working directory to expose them under
_files - Run without internet access
ld.* utilities are not available in Python.
Best Practices
Return Structured Objects
Return Structured Objects
Return data as objects for easy access in later nodes. This makes it simple to reference specific values in subsequent nodes using dot notation.
Handle Missing Data
Handle Missing Data
Use
||, optional chaining (?.), or Python .get() to provide default values and prevent errors when data is undefined or null.Use Error Handling
Use Error Handling
Wrap risky operations in
try/catch for JavaScript or try/except for Python. This helps prevent workflow failures and provides meaningful error messages.Keep It Simple
Keep It Simple
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.
Keep Inputs Small
Keep Inputs Small
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.
Add Comments
Add Comments
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 all upstream outputs that the node can access. If they exceed 5 MiB, the node fails with an error and your code is never executed. 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.
- 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