Skip to main content
The Code node executes custom Python or JavaScript to handle complex data transformations, calculations, and logic within your workflow. Use it when preset nodes aren’t sufficient for your specific processing needs.
Code node interface

Code node configuration interface

Configuration

Define Input Variables to access data from other nodes in your workflow, then reference these variables in your code. Your function must return a dictionary containing the Output Variables you’ve declared.
def main(input_variable: str) -> dict:
    # Process the input
    result = input_variable.upper()
    return {
        'output_variable': result
    }

Language Support

Choose between Python and JavaScript based on your needs and familiarity. Both languages run in secure sandboxes with access to common libraries for data processing.
  • Python
  • JavaScript
Python includes standard libraries like json, math, datetime, and re. Ideal for data analysis, mathematical operations, and text processing.
def main(data: list) -> dict:
    import json
    import math
    
    average = sum(data) / len(data)
    return {'result': math.ceil(average)}

Error Handling and Retries

Configure automatic retry behavior for failed code executions and define fallback strategies when code encounters errors.
Code Error handling

Error handling configuration options

Retry Settings allow up to 10 automatic retries with configurable intervals (maximum 5000ms). Enable this for handling temporary processing issues. Error Handling lets you define fallback paths when code execution fails, allowing your workflow to continue running even when the code encounters problems.
Retry settings

Retry configuration interface

Output Validation and Limits

Code outputs are automatically validated with strict limits:
  • Strings: Maximum length of 80,000 characters, null bytes are removed
  • Numbers: Range from -999999999 to 999999999, floats limited to 10 decimal places
  • Objects/Arrays: Maximum depth of 5 levels to prevent complex nested structures
These limits ensure performance and prevent memory issues in workflows.

Security Considerations

Code executes in a strict sandbox that prevents file system access, network requests, and system commands. This maintains security while providing programming flexibility. Some operations are automatically blocked for security reasons. Avoid attempting to access system files or execute potentially dangerous operations:
Cloudflare WAF blocking

Security filtering by Cloudflare WAF

If your code isn’t saving, check your browser’s Network tab - security filters may be blocking potentially dangerous operations.

Dependencies Support

Code nodes support external dependencies for both Python and JavaScript:
# Python: Import numpy, pandas, requests, etc.
import numpy as np
import pandas as pd

def main(data: list) -> dict:
    df = pd.DataFrame(data)
    return {'mean': float(np.mean(df['values']))}
// JavaScript: Import lodash, moment, etc.
const _ = require('lodash');

function main(data) {
    return { unique: _.uniq(data) };
}
Dependencies are pre-installed in the sandbox environment. Check the available packages list in your Dify installation.

Self-Hosted Setup

For self-hosted Dify installations, start the sandbox service for secure code execution:
docker-compose -f docker-compose.middleware.yaml up -d
The sandbox service requires Docker and isolates code execution from your main system for security.