Python Code Nodes
Expert guidance for writing Python code in n8n Code nodes.Quick Start Template
- Consider JavaScript first — use Python only when necessary
- Access data via
_input.all(),_input.first(), or_input.item - Must return
[{"json": {...}}]format — list of dicts with a"json"key - Webhook data is under
_json["body"](not_jsondirectly) - No external libraries — only Python standard library is available
Python Modes
- Python (Beta) — Recommended
- Python (Native) Beta
Uses Use this mode for most Python use cases.
_input, _json, _node helper syntax. Includes built-ins like _now and _jmespath().Data Access Patterns
Critical: Webhook Data Structure
Return Format Requirements
Critical Limitation: No External Libraries
What IS Available (Standard Library Only)
Workarounds for Missing Libraries
| Need | Workaround |
|---|---|
HTTP requests (requests) | Use HTTP Request node before Code node, OR switch to JavaScript with $helpers.httpRequest() |
Data analysis (pandas, numpy) | Use Python statistics module for basic stats, OR switch to JavaScript |
Web scraping (BeautifulSoup) | Use HTTP Request node + HTML Extract node, OR switch to JavaScript with regex |
| Advanced date operations | Switch to JavaScript with Luxon DateTime library |
Common Python Patterns
1. Data transformation with list comprehensions
1. Data transformation with list comprehensions
2. Filtering and aggregation
2. Filtering and aggregation
3. String processing with regex
3. String processing with regex
4. Data validation and cleaning
4. Data validation and cleaning
5. Statistical analysis
5. Statistical analysis
6. Hashing and encoding
6. Hashing and encoding
7. URL building and parsing
7. URL building and parsing
Top 5 Error Patterns
#1: Importing external libraries (Python-specific!)
#1: Importing external libraries (Python-specific!)
#2: Missing return statement
#2: Missing return statement
#3: Incorrect return format
#3: Incorrect return format
#4: KeyError on dictionary access
#4: KeyError on dictionary access
#5: Webhook body nesting
#5: Webhook body nesting
JavaScript vs Python Comparison
| Feature | JavaScript | Python |
|---|---|---|
| HTTP requests | $helpers.httpRequest() | Use HTTP Request node |
| Advanced dates | Luxon DateTime library | datetime + timedelta |
| Data analysis | Manual or JMESPath | statistics module |
| External libraries | N/A (built-ins sufficient) | Not available |
| n8n documentation | Extensive | Limited |
| Community support | Strong | Weaker |
| Recommendation | 95% of use cases | Specific needs only |
Best Practices
Pre-Deploy Checklist
- Considered JavaScript first — using Python only when necessary
- Code is not empty — has meaningful logic
- Return statement exists
- Return format is
[{"json": {...}}] - Data access uses
_input.all(),_input.first(), or_input.item - No external library imports — only standard library
- Dictionary access uses
.get()to avoidKeyError - Webhook data accessed via
["body"]if input is from a Webhook node - Mode is “All Items” unless per-item isolation is required
- All code paths return the same structure