| name | n8n-code-python |
| description | Write Python code in n8n Code nodes. Use when writing Python for n8n, using Python libraries, pandas data processing, or preferring Python over JavaScript in workflows. Use when this capability is needed. |
| metadata | {"author":"fazer-ai"} |
n8n Code Python
Expert guidance for writing Python in n8n Code nodes.
Based on n8n-skills by Romuald Członkowski
Python Code Node Basics
n8n supports Python in Code nodes with some differences from JavaScript.
Run Once for All Items
items = _input.all()
return [{"json": {**item.json, "processed": True}} for item in items]
Run Once for Each Item
data = _input.item.json
return {"json": {**data, "processed": True}}
Accessing Data
Current Node Input
items = _input.all()
first = _input.first()
current = _input.item
data = _input.first().json
Execution Context
Common Patterns
Transform All Items
items = _input.all()
return [
{
"json": {
"id": item.json["id"],
"name": item.json["name"].upper(),
"processed": True
}
}
for item in items
]
Filter Items
items = _input.all()
return [
{"json": item.json}
for item in items
if item.json.get("status") == "active"
]
Aggregate Data
items = _input.all()
total = sum(item.json.get("amount", 0) for item in items)
count = len(items)
return [{
"json": {
"total": total,
"count": count,
"average": total / count if count > 0 else 0
}
}]
Group By
from collections import defaultdict
items = _input.all()
grouped = defaultdict(list)
for item in items:
key = item.json.get("category", "unknown")
grouped[key].append(item.json)
return [
{"json": {"category": k, "items": v, "count": len(v)}}
for k, v in grouped.items()
]
JSON Parsing
import json
items = _input.all()
return [
{
"json": {
"parsed": json.loads(item.json.get("rawData", "{}"))
}
}
for item in items
]
Error Handling
import json
items = _input.all()
results = []
for item in items:
try:
parsed = json.loads(item.json.get("rawData", "{}"))
results.append({"json": {"success": True, "data": parsed}})
except Exception as e:
results.append({"json": {"success": False, "error": str(e)}})
return results
Date/Time Handling
from datetime import datetime, timedelta
now = datetime.now()
date = datetime.fromisoformat("2024-01-15T10:30:00")
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
next_week = now + timedelta(days=7)
yesterday = now - timedelta(days=1)
return [{
"json": {
"now": now.isoformat(),
"formatted": formatted,
"nextWeek": next_week.isoformat()
}
}]
Working with Pandas
⚠️ Note: pandas may not be available in all n8n installations. Check your environment.
import pandas as pd
items = _input.all()
data = [item.json for item in items]
df = pd.DataFrame(data)
df["total"] = df["price"] * df["quantity"]
df_grouped = df.groupby("category").agg({"total": "sum"}).reset_index()
return [{"json": row} for row in df_grouped.to_dict(orient="records")]
Webhook Data Access
CRITICAL: Webhook data is under .body:
message = _input.first().json.get("message")
body = _input.first().json.get("body", {})
message = body.get("message")
headers = _input.first().json.get("headers", {})
query = _input.first().json.get("query", {})
Available Libraries
Standard Python libraries typically available:
json - JSON parsing
datetime - Date/time handling
re - Regular expressions
collections - Data structures
math - Mathematical functions
base64 - Encoding
hashlib - Hashing
import json
import re
import hashlib
from datetime import datetime
from collections import defaultdict
hash_value = hashlib.sha256(data.encode()).hexdigest()
matches = re.findall(r"\d+", text)
Common Mistakes
1. Wrong Return Format
return {"name": "John"}
return [{"json": {"name": "John"}}]
2. Using dot notation for dict access
name = item.json.name
name = item.json["name"]
name = item.json.get("name", "default")
3. Not Handling Missing Keys
value = item.json["optional_field"]
value = item.json.get("optional_field", "default")
4. Returning dict instead of list
return {"json": {"result": "data"}}
return [{"json": {"result": "data"}}]
Python vs JavaScript in n8n
| Feature | Python | JavaScript |
|---|
| Input access | _input | $input |
| Node access | Limited | $node["Name"] |
| Env vars | Limited | $env.VAR |
| Async/await | No | Yes |
| Libraries | Standard + limited | Luxon, Lodash |
Best Practices
- Always return list of
{"json": {...}} dicts
- Use
.get() for safe dict access
- Handle exceptions with try/except
- Import at top of code block
- Test with simple data first
- Access webhook data via
.body
Quick Reference
| Need | Code |
|---|
| All items | _input.all() |
| First item | _input.first() |
| Current item | _input.item (each mode) |
| Item JSON | item.json |
| Dict value | item.json.get("key", default) |
| Webhook body | item.json.get("body", {}) |
| Return item | {"json": {...}} |
| Return many | [{"json": {...}}, ...] |
Based on n8n-skills by Romuald Członkowski • Adapted for Moltbot by fazer.ai
Source: fazer-ai/moltbot-skill-n8n — distributed by TomeVault.