Write Python code in n8n Code nodes. Use when writing Python in n8n, using _input/_json/_node syntax, working with standard library, or need to understand Python limitations in n8n Code nodes.
Instrucciones de origen · Vista previa de solo lectura
name
n8n-code-python
description
Write Python code in n8n Code nodes. Use when writing Python in n8n, using _input/_json/_node syntax, working with standard library, or need to understand Python limitations in n8n Code nodes.
Expert guidance for writing Python code in n8n Code nodes.
⚠️ Important: JavaScript First
Recommendation: Use JavaScript for 95% of use cases. Only use Python when:
You need specific Python standard library functions
You're significantly more comfortable with Python syntax
You're doing data transformations better suited to Python
Why JavaScript is preferred:
Full n8n helper functions ($helpers.httpRequest, etc.)
Luxon DateTime library for advanced date/time operations
No external library limitations
Better n8n documentation and community support
Quick Start
# Basic template for Python Code nodes
items = _input.all()
# Process data
processed = []
for item in items:
processed.append({
"json": {
**item["json"],
"processed": True,
"timestamp": datetime.now().isoformat()
}
})
return processed
Essential Rules
Consider JavaScript first - Use Python only when necessary
Access data: _input.all(), _input.first(), or _input.item
CRITICAL: Must return [{"json": {...}}] format
CRITICAL: Webhook data is under _json["body"] (not _json directly)
CRITICAL LIMITATION: No external libraries (no requests, pandas, numpy)
Standard library only: json, datetime, re, base64, hashlib, urllib.parse, math, random, statistics
Mode Selection Guide
Same as JavaScript - choose based on your use case:
Run Once for All Items (Recommended - Default)
Use this mode for: 95% of use cases
How it works: Code executes once regardless of input count
Data access: _input.all() or _items array (Native mode)
Best for: Aggregation, filtering, batch processing, transformations
Performance: Faster for multiple items (single execution)
# Example: Calculate total from all items
all_items = _input.all()
total = sum(item["json"].get("amount", 0) for item in all_items)
return [{
"json": {
"total": total,
"count": len(all_items),
"average": total / len(all_items) if all_items else0
}
}]
Run Once for Each Item
Use this mode for: Specialized cases only
How it works: Code executes separately for each input item
Data access: _input.item or _item (Native mode)
Best for: Item-specific logic, independent operations, per-item validation
Performance: Slower for large datasets (multiple executions)
# Python (Native) example
processed = []
for item in _items:
processed.append({
"json": {
"id": item["json"].get("id"),
"processed": True
}
})
return processed
Recommendation: Use Python (Beta) for better n8n integration.
Data Access Patterns
Pattern 1: _input.all() - Most Common
Use when: Processing arrays, batch operations, aggregations
# Get all items from previous node
all_items = _input.all()
# Filter, transform as needed
valid = [item for item in all_items if item["json"].get("status") == "active"]
processed = []
for item in valid:
processed.append({
"json": {
"id": item["json"]["id"],
"name": item["json"]["name"]
}
})
return processed
Pattern 2: _input.first() - Very Common
Use when: Working with single objects, API responses
# Get first item only
first_item = _input.first()
data = first_item["json"]
return [{
"json": {
"result": process_data(data),
"processed_at": datetime.now().isoformat()
}
}]