Skip to main content

Python Code Nodes

Expert guidance for writing Python code in n8n Code nodes.
Use JavaScript for 95% of use cases. Python Code nodes have significant limitations compared to JavaScript Code nodes:
  • No external libraries (requests, pandas, numpy are not available)
  • No $helpers.httpRequest() equivalent
  • No Luxon DateTime library for advanced date operations
  • Less n8n documentation and community support
Only use Python when:
  • You need specific Python standard library functions (e.g., statistics, hashlib)
  • You’re significantly more comfortable with Python syntax
  • Your logic maps naturally to Python list comprehensions

Quick Start Template

Essential rules:
  1. Consider JavaScript first — use Python only when necessary
  2. Access data via _input.all(), _input.first(), or _input.item
  3. Must return [{"json": {...}}] format — list of dicts with a "json" key
  4. Webhook data is under _json["body"] (not _json directly)
  5. No external libraries — only Python standard library is available

Python Modes

Data Access Patterns

Critical: Webhook Data Structure

Webhook data is nested under ["body"], not at the root of _json.

Return Format Requirements

Critical Limitation: No External Libraries

The most important Python limitation in n8n: you cannot import external packages.

What IS Available (Standard Library Only)

Workarounds for Missing Libraries

Common Python Patterns

Top 5 Error Patterns

JavaScript vs Python Comparison

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 avoid KeyError
  • 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