n8n-code-python
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.
Author
Category
Development ToolsInstall
Download and extract to your skills directory
Copy command and send to OpenClaw for auto-install:
n8n Code Python - Complete Guide to the n8n Python Code Node
Skill Overview
Provide guidance on writing Python code for the Code node in n8n workflows, covering data access patterns, standard library usage, common error prevention, and recommendations on when to choose Python vs. JavaScript.
Use Cases
1. Data Processing Using Python Standard Library
When the workflow needs to perform statistical analysis, regular expression processing, hash encryption, or other features from Python’s standard library, the Python Code node is an ideal choice. Supported modules include
json, datetime, re, base64, hashlib, urllib.parse, math, random, and statistics.2. Developers More Familiar with Python Syntax
For developers with a Python background, using Python features such as list comprehensions and dictionary operations to transform data is more natural and efficient. Suitable for scenarios like batch data filtering, aggregation calculations, and string processing.
3. Data Validation and Statistical Analysis
When you need to use Python’s
statistics module to compute averages, medians, standard deviations, or implement complex data validation logic, the Python Code node can fulfill these needs concisely.Core Features
1. Guidance on Data Access Modes
Explain in detail the usage scenarios of three data access methods:
_input.all(), _input.first(), and _input.item, as well as important notes for accessing Webhook data via _json["body"]. Cover the differences between Python (Beta) and Python (Native) execution modes to help developers choose the appropriate mode.2. External Library Restrictions and Standard Library References
Clearly state that the n8n Python environment cannot import external libraries such as
requests, pandas, or numpy, and provide a complete reference to the available Python standard library. Include alternatives for common needs like making HTTP requests, performing data analysis, and web scraping.3. Common Error Prevention
List the Top 5 common mistakes and their solutions: importing external libraries, missing return statements, incorrect return format, dictionary access causing
KeyError, and issues with Webhook data nesting. Provide correct code templates and best-practice recommendations.Frequently Asked Questions
Should the n8n Code Node choose Python or JavaScript?
Recommend using JavaScript in 95% of cases. JavaScript offers full n8n helper functions (e.g., $helpers.httpRequest()), advanced date-time operations via Luxon’s DateTime, better documentation support, and more community resources.
Choose Python only when:
statistics module)Which libraries can be used in the n8n Python code node?
Only Python standard libraries are allowed; no third-party libraries can be imported.
Available standard library modules include:
json (JSON parsing)datetime (date and time operations)re (regular expressions)base64 (encoding/decoding)hashlib (hash functions)urllib.parse (URL parsing)math (math functions)random (random numbers)statistics (statistical analysis)Not supported: requests, pandas, numpy, scipy, BeautifulSoup, lxml, and other common third-party libraries.
What are the differences between _input.all(), _input.first(), and _input.item in n8n?
The three data access methods correspond to different scenarios:
_input.all(): Gets all input items. Suitable for batch processing, aggregation, filtering/conversion, etc. (most common)_input.first(): Gets only the first input item. Suitable for single-object handling and API response processing._input.item: Only available in the "Run Once for Each Item" mode; it represents the current item in the loop.When you select "Run Once for All Items" (default), use _input.all() or _input.first(). When you select "Run Once for Each Item", use _input.item.
What is the correct return format for the n8n Python code node?
You must return a list of dictionaries containing the "json" key:
# ✅ Correct format
return [{"json": {"field1": value1, "field2": value2}}]
# ✅ Multiple results
return [
{"json": {"id": 1, "data": "first"}},
{"json": {"id": 2, "data": "second"}}
]
# ❌ Incorrect: missing list wrapper
return {"json": {"field": value}}
# ❌ Incorrect: missing "json" key
return [{"field": value}]All code paths must return the same format; when there are no results, return an empty list [].
What is the difference between Python (Beta) mode and Native mode in n8n?
Python (Beta) - Recommended:
_input, _json, _node helper syntax_now, _today, _jmespath()Python (Native) (Beta):
_items and _item variables are allowedFor most n8n workflows, it’s recommended to use Python (Beta) mode for a better integration experience.