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
What Is This?
Overview
The n8n Code node supports Python as a beta language option, allowing workflow developers to write Python scripts directly inside n8n automation pipelines. This feature enables teams already familiar with Python to leverage their existing knowledge when building data transformations, processing logic, and custom automation steps without switching to JavaScript.
Python in n8n Code nodes runs in a sandboxed environment with access to a subset of the Python standard library. The execution model follows n8n's item-based data structure, meaning Python code must interact with incoming workflow data through specific built-in variables rather than standard Python input mechanisms. Understanding this syntax is essential before writing any functional code.
It is worth noting that n8n officially recommends JavaScript for the vast majority of Code node use cases. Python support is labeled beta, which means certain libraries, features, and behaviors available in standard Python environments may not work as expected inside n8n. Use Python only when your use case specifically requires Python standard library functionality that has no practical JavaScript equivalent.
Who Should Use This
- Python developers who are new to n8n and prefer working in a familiar language before learning JavaScript
- Data engineers building transformation pipelines who need Python string or math utilities
- Automation specialists who need to process structured data using Python's built-in modules such as json, math, or datetime
- Teams migrating existing Python logic into n8n workflows as an intermediate step
- Developers prototyping data manipulation scripts before moving them to a dedicated Python service
- Technical users who need to apply Python standard library functions that are not easily replicated in JavaScript
Why Use It?
Problems It Solves
- Allows Python-familiar developers to contribute to n8n workflows without requiring JavaScript proficiency
- Enables use of Python standard library modules for tasks like date formatting, mathematical operations, and string parsing
- Provides a path for integrating Python-based logic directly into automation flows without external service calls
- Reduces the need to spin up separate microservices for simple Python transformations
- Bridges the gap for teams that maintain Python-heavy codebases and want consistent language use across tooling
Core Highlights
- Native Python execution inside n8n Code nodes without external dependencies
- Access to n8n workflow data through
_input,_json, and_nodebuilt-in variables - Support for Python standard library modules available in the sandboxed runtime
- Item-based processing model consistent with n8n's core data architecture
- Beta status with active development and expanding capabilities
- Familiar Python syntax for developers coming from data or scripting backgrounds
- Ability to return structured data back into the n8n workflow item format
How to Use It?
Basic Usage
To access incoming data in a Python Code node, use the _input variable. Each item in the workflow is accessible through _input.all(), which returns a list of items.
items = _input.all()
result = []
for item in items:
name = item.json["name"]
result.append({"greeting": f"Hello, {name}"})
return resultTo access the JSON data of the first item directly, use _input.first().json.
first_item = _input.first().json
value = first_item["amount"] * 1.2
return [{"adjusted_amount": value}]Specific Scenarios
Scenario 1: Date formatting using the datetime module
from datetime import datetime
items = _input.all()
output = []
for item in items:
raw_date = item.json["timestamp"]
parsed = datetime.fromisoformat(raw_date)
output.append({"formatted_date": parsed.strftime("%B %d, %Y")})
return outputScenario 2: Mathematical processing with the math module
import math
items = _input.all()
return [{"sqrt_value": math.sqrt(item.json["number"])} for item in items]Real-World Examples
- Reformatting date strings from an API response into a human-readable format before sending an email notification
- Calculating logarithmic or trigonometric values from sensor data before storing results in a database
- Parsing and cleaning text fields using Python string methods before passing data to a downstream node
When to Use It?
Use Cases
- Formatting dates and timestamps using Python's datetime module
- Performing mathematical calculations using the math or statistics modules
- Parsing and transforming string data with Python built-in string methods
- Processing JSON payloads with custom logic before passing to API nodes
- Applying list comprehensions or filtering logic to workflow item arrays
- Converting data types or normalizing values across multiple items
- Prototyping transformation logic before moving it to a dedicated service
Important Notes
Requirements
- n8n version that supports Python in Code nodes (check your instance version)
- Familiarity with n8n's item-based data model and built-in variable syntax
- Python standard library only, no third-party packages such as pandas or requests
- Code must return a valid list structure for n8n to process downstream nodes correctly
More Skills You Might Like
Explore similar skills to enhance your workflow
X Twitter Growth
X/Twitter growth engine for building audience, crafting viral content, and analyzing engagement. Use when the user wants to grow on X/Twitter, write t
Remotion Video Toolkit
Complete toolkit for programmatic video creation with Remotion + React. Covers animations
Image Enhancer
Improves the quality of images, especially screenshots, by enhancing resolution, sharpness, and clarity. Perfect for preparing images for presentation
Phase 1: Parse Arguments
Read the argument for the launch date or dry-run mode. Dry-run mode generates the checklist without creating sign-off entries or writing files
Marketing Demand Acquisition
Marketing Demand Acquisition automation and integration
Blogwatcher
Monitor blogs and RSS/Atom feeds for updates using the blogwatcher CLI