$npx -y skills add czlonkowski/n8n-skills --skill n8n-code-pythonWrite 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. Use this skill when the user specifically requests Python for an n8n Code node. Note —
| 1 | # Python Code Node (Beta) |
| 2 | |
| 3 | Expert guidance for writing Python code in n8n Code nodes. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## ⚠️ Important: JavaScript First |
| 8 | |
| 9 | **Recommendation**: Use **JavaScript for 95% of use cases**. Only use Python when: |
| 10 | - You need specific Python standard library functions |
| 11 | - You're significantly more comfortable with Python syntax |
| 12 | - You're doing data transformations better suited to Python |
| 13 | |
| 14 | **Why JavaScript is preferred:** |
| 15 | - Full n8n helper functions (`this.helpers.httpRequest`, etc.) |
| 16 | - Luxon DateTime library for advanced date/time operations |
| 17 | - No external library limitations |
| 18 | - Better n8n documentation and community support |
| 19 | |
| 20 | --- |
| 21 | |
| 22 | ## Quick Start |
| 23 | |
| 24 | ```python |
| 25 | # Basic template for Python Code nodes |
| 26 | items = _input.all() |
| 27 | |
| 28 | # Process data |
| 29 | processed = [] |
| 30 | for item in items: |
| 31 | processed.append({ |
| 32 | "json": { |
| 33 | **item["json"], |
| 34 | "processed": True, |
| 35 | "timestamp": datetime.now().isoformat() |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | return processed |
| 40 | ``` |
| 41 | |
| 42 | ### Essential Rules |
| 43 | |
| 44 | 1. **Consider JavaScript first** - Use Python only when necessary |
| 45 | 2. **Access data**: `_input.all()`, `_input.first()`, or `_input.item` |
| 46 | 3. **CRITICAL**: Must return `[{"json": {...}}]` format |
| 47 | 4. **CRITICAL**: Webhook data is under `_json["body"]` (not `_json` directly) |
| 48 | 5. **CRITICAL LIMITATION**: **No external libraries** (no requests, pandas, numpy) |
| 49 | 6. **Standard library only**: json, datetime, re, base64, hashlib, urllib.parse, math, random, statistics |
| 50 | |
| 51 | --- |
| 52 | |
| 53 | ## Mode Selection Guide |
| 54 | |
| 55 | Same as JavaScript - choose based on your use case: |
| 56 | |
| 57 | ### Run Once for All Items (Recommended - Default) |
| 58 | |
| 59 | **Use this mode for:** 95% of use cases |
| 60 | |
| 61 | - **How it works**: Code executes **once** regardless of input count |
| 62 | - **Data access**: `_input.all()` or `_items` array (Native mode) |
| 63 | - **Best for**: Aggregation, filtering, batch processing, transformations |
| 64 | - **Performance**: Faster for multiple items (single execution) |
| 65 | |
| 66 | ```python |
| 67 | # Example: Calculate total from all items |
| 68 | all_items = _input.all() |
| 69 | total = sum(item["json"].get("amount", 0) for item in all_items) |
| 70 | |
| 71 | return [{ |
| 72 | "json": { |
| 73 | "total": total, |
| 74 | "count": len(all_items), |
| 75 | "average": total / len(all_items) if all_items else 0 |
| 76 | } |
| 77 | }] |
| 78 | ``` |
| 79 | |
| 80 | ### Run Once for Each Item |
| 81 | |
| 82 | **Use this mode for:** Specialized cases only |
| 83 | |
| 84 | - **How it works**: Code executes **separately** for each input item |
| 85 | - **Data access**: `_input.item` or `_item` (Native mode) |
| 86 | - **Best for**: Item-specific logic, independent operations, per-item validation |
| 87 | - **Performance**: Slower for large datasets (multiple executions) |
| 88 | |
| 89 | ```python |
| 90 | # Example: Add processing timestamp to each item |
| 91 | item = _input.item |
| 92 | |
| 93 | return [{ |
| 94 | "json": { |
| 95 | **item["json"], |
| 96 | "processed": True, |
| 97 | "processed_at": datetime.now().isoformat() |
| 98 | } |
| 99 | }] |
| 100 | ``` |
| 101 | |
| 102 | --- |
| 103 | |
| 104 | ## Python Modes: Beta vs Native |
| 105 | |
| 106 | n8n offers two Python execution modes: |
| 107 | |
| 108 | ### Python (Beta) - Recommended |
| 109 | - **Use**: `_input`, `_json`, `_node` helper syntax |
| 110 | - **Best for**: Most Python use cases |
| 111 | - **Helpers available**: `_now`, `_today`, `_jmespath()` |
| 112 | - **Import**: `from datetime import datetime` |
| 113 | |
| 114 | ```python |
| 115 | # Python (Beta) example |
| 116 | items = _input.all() |
| 117 | now = _now # Built-in datetime object |
| 118 | |
| 119 | return [{ |
| 120 | "json": { |
| 121 | "count": len(items), |
| 122 | "timestamp": now.isoformat() |
| 123 | } |
| 124 | }] |
| 125 | ``` |
| 126 | |
| 127 | ### Python (Native) (Beta) |
| 128 | - **Use**: `_items`, `_item` variables only |
| 129 | - **No helpers**: No `_input`, `_now`, etc. |
| 130 | - **More limited**: Standard Python only |
| 131 | - **Use when**: Need pure Python without n8n helpers |
| 132 | |
| 133 | ```python |
| 134 | # Python (Native) example |
| 135 | processed = [] |
| 136 | |
| 137 | for item in _items: |
| 138 | processed.append({ |
| 139 | "json": { |
| 140 | "id": item["json"].get("id"), |
| 141 | "processed": True |
| 142 | } |
| 143 | }) |
| 144 | |
| 145 | return processed |
| 146 | ``` |
| 147 | |
| 148 | **Recommendation**: Use **Python (Beta)** for better n8n integration. |
| 149 | |
| 150 | --- |
| 151 | |
| 152 | ## Data Access Patterns |
| 153 | |
| 154 | Access input data through underscore-prefixed variables. Each item is a dict shaped `{"json": {...}}`, so the actual fields live under `["json"]`. |
| 155 | |
| 156 | ```python |
| 157 | # Pattern 1: _input.all() - Most common. Arrays, batch ops, aggregations |
| 158 | all_items = _input.all() # list of {"json": {...}} dicts |
| 159 | |
| 160 | # Pattern 2: _input.first() - Very common. Single objects, API responses |
| 161 | data = _input.first()["json"] # built-in safety vs all_items[0] |
| 162 | |
| 163 | # Pattern 3: _input.item - "Run Once for Each Item" mod |