A robust SDK and CLI for n8n workflow automation with DevOps capabilities
Project description
n8n-flow-manager ๐
n8n-flow-manager is a robust, production-ready Python SDK and CLI for the n8n automation platform. Unlike simple HTTP wrappers, this package is designed for DevOps workflows, providing type-safe models, async operations, workflow templating, and CI/CD integration capabilities.
โจ Features
Core Capabilities
- โก Async-First Design: Built on
httpxfor high-performance async operations - ๐ก๏ธ Type Safety: Complete Pydantic models for workflows, executions, and credentials
- ๐ Smart Polling: Execute workflows and wait for completion with intelligent status checking
- ๐ Jinja2 Templating: Inject environment-specific variables into workflow definitions
- ๐ค Powerful CLI: Terminal commands for backup, deploy, sync, and manage workflows
- ๐ Secure: API key authentication with proper error handling and retries
- ๐ฆ Zero Config: Works with environment variables or explicit configuration
What Makes It Different?
| Feature | n8n-flow-manager | Generic HTTP Clients |
|---|---|---|
| Type Validation | โ Pydantic models | โ Raw dicts |
| Async Support | โ Native asyncio | โ ๏ธ Sync only |
| Smart Execution | โ run_and_wait() | โ Manual polling |
| Templating | โ Jinja2 built-in | โ Not included |
| CLI Tools | โ Full featured | โ None |
| Error Handling | โ Custom exceptions | โ ๏ธ Generic errors |
๐ Project Structure
n8n-flow-manager/
โโโ src/
โ โโโ n8n_manager/
โ โโโ __init__.py # Public API exports
โ โโโ client.py # Main async client
โ โโโ exceptions.py # Custom error types
โ โโโ api/ # API modules by resource
โ โ โโโ workflows.py # Workflow operations
โ โ โโโ executions.py # Execution management
โ โ โโโ credentials.py # Credential handling
โ โโโ models/ # Pydantic data models
โ โ โโโ workflow.py # Workflow structures
โ โ โโโ execution.py # Execution states
โ โ โโโ credential.py # Credential types
โ โโโ cli/ # Command-line interface
โ โ โโโ main.py # Typer CLI app
โ โโโ utils/ # Helper utilities
โ โโโ templating.py # Jinja2 template engine
โโโ tests/ # Pytest test suite
โโโ examples/ # Usage examples
โโโ pyproject.toml # Poetry configuration
โโโ README.md # This file
๐ Installation
Requirements
- Python 3.9 or higher
- n8n instance (cloud or self-hosted)
Install from PyPI
# Install with pip
pip install n8n-flow-manager
# Install with poetry
poetry add n8n-flow-manager
# Install CLI globally with pipx
pipx install n8n-flow-manager
Install from Source
# Clone the repository
git clone https://github.com/mgobea/n8n-flow-manager.git
cd n8n-flow-manager
# Install with Poetry (recommended)
poetry install --with dev
Install CLI Globally (Use n8n-py anywhere)
# Create wrapper script
mkdir -p ~/.local/bin
cat > ~/.local/bin/n8n-py << 'EOF'
#!/bin/bash
cd /path/to/n8n-flow-manager
poetry run n8n-py "$@"
EOF
chmod +x ~/.local/bin/n8n-py
# Add to PATH (if not already)
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
# Now use directly anywhere:
n8n-py health
n8n-py list-workflows
Configuration
Create a .env file in your project root:
N8N_API_KEY=your_api_key_here
N8N_BASE_URL=https://n8n.example.com
Or export as environment variables:
export N8N_API_KEY="your_api_key_here"
export N8N_BASE_URL="https://n8n.example.com"
Getting Your API Key
- Open your n8n instance
- Go to Settings โ API
- Generate a new API key
- Copy and save it securely
๐ ๏ธ Usage Guide
1. Python SDK Usage
Basic Client Usage
import asyncio
from n8n_manager import N8NClient
async def main():
# Initialize client (reads from environment)
async with N8NClient() as client:
# List all active workflows
workflows = await client.workflows.list(active=True)
for wf in workflows:
print(f"Workflow: {wf.name} (ID: {wf.id})")
# Get specific workflow
workflow = await client.workflows.get("workflow_id")
print(f"Nodes: {len(workflow.nodes)}")
# Execute workflow and wait for result
execution = await client.executions.run_and_wait(
workflow_id="workflow_id",
timeout=60
)
print(f"Status: {execution.status}")
print(f"Success: {execution.is_successful}")
asyncio.run(main())
Creating Workflows Programmatically
from n8n_manager import N8NClient
from n8n_manager.models.workflow import Workflow, Node
async def create_simple_workflow():
async with N8NClient() as client:
workflow = Workflow(
name="Python-Created Workflow",
active=False,
nodes=[
Node(
name="Start",
type="n8n-nodes-base.start",
position=[250, 300],
parameters={}
),
Node(
name="Set Data",
type="n8n-nodes-base.set",
position=[450, 300],
parameters={
"values": {
"string": [
{"name": "message", "value": "Hello from Python!"}
]
}
}
)
],
connections={
"Start": {
"main": [[{"node": "Set Data", "type": "main", "index": 0}]]
}
}
)
created = await client.workflows.create(workflow)
print(f"Created workflow: {created.id}")
Using Templates
from n8n_manager.utils.templating import load_workflow_from_file
# Load workflow with template variables
workflow = load_workflow_from_file(
"templates/data_sync.json",
variables={
"environment": "production",
"api_endpoint": "https://api.example.com",
"timeout": 30
}
)
async with N8NClient() as client:
deployed = await client.workflows.create(workflow)
print(f"Deployed: {deployed.name}")
2. CLI Usage
The CLI provides powerful commands for workflow management.
Example Output:
$ n8n-py health
โ Connection healthy!
API URL: https://n8n.example.com/
$ n8n-py list-workflows
Workflows (33 found)
โโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโณโโโโโโโโ
โ ID โ Name โ Active โ Nodes โ
โกโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฉ
โ 1RDHBsmLkkTptybX โ Production Data Sync โ โ โ 6 โ
โ 2V3iCBkiOAPVzrUr โ Customer Onboarding โ โ โ 7 โ
โ 8qGqx5TW1QA7T8P9 โ Error Notifications โ โ โ 2 โ
โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโ
Commands
# List all workflows
n8n-py list-workflows
# List only active workflows
n8n-py list-workflows --active
Get Workflow Details
# Display workflow info
n8n-py get-workflow <workflow_id>
# Save workflow to file
n8n-py get-workflow <workflow_id> --output workflow.json
Deploy Workflows
# Deploy from JSON file
n8n-py deploy workflow.json
# Deploy with template variables
n8n-py deploy template.json --var environment=prod --var timeout=30
# Deploy and activate immediately
n8n-py deploy workflow.json --activate
Backup Workflows
# Backup all workflows
n8n-py backup --output ./backups
# Backup only active workflows
n8n-py backup --output ./backups --active-only
Execute Workflows
# Execute and wait for completion
n8n-py execute <workflow_id>
# Execute without waiting
n8n-py execute <workflow_id> --no-wait
# Execute with input data
n8n-py execute <workflow_id> --input data.json
Activate/Deactivate
# Activate workflow
n8n-py activate <workflow_id>
# Deactivate workflow
n8n-py deactivate <workflow_id>
Health Check
# Verify connection to n8n
n8n-py health
๐ Advanced Examples
CI/CD Integration
Use in GitHub Actions for automated deployments:
# .github/workflows/deploy-n8n.yml
name: Deploy n8n Workflows
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install n8n-flow-manager
run: pip install -e .
- name: Deploy workflows
env:
N8N_API_KEY: ${{ secrets.N8N_API_KEY }}
N8N_BASE_URL: ${{ secrets.N8N_BASE_URL }}
run: |
n8n-py deploy workflows/production.json --activate
Environment-Specific Deployments
import asyncio
from n8n_manager import N8NClient
from n8n_manager.utils.templating import load_workflow_from_file
ENVIRONMENTS = {
"dev": {
"api_endpoint": "https://dev.api.example.com",
"webhook_path": "webhook-dev",
"timeout": 10
},
"prod": {
"api_endpoint": "https://api.example.com",
"webhook_path": "webhook",
"timeout": 30
}
}
async def deploy_to_environment(env: str):
workflow = load_workflow_from_file(
"templates/api_workflow.json",
variables=ENVIRONMENTS[env]
)
async with N8NClient() as client:
deployed = await client.workflows.create(workflow)
await client.workflows.activate(deployed.id)
print(f"Deployed to {env}: {deployed.id}")
# Deploy to production
asyncio.run(deploy_to_environment("prod"))
Monitoring and Logging
async def monitor_executions(workflow_id: str):
async with N8NClient() as client:
executions = await client.executions.list(
workflow_id=workflow_id,
limit=50
)
for execution in executions:
if execution.is_failed:
print(f"โ Failed: {execution.id} at {execution.started_at}")
elif execution.is_successful:
print(f"โ
Success: {execution.id}")
๐งช Testing
Run the test suite with pytest:
# Install dev dependencies
poetry install --with dev
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=src/n8n_manager --cov-report=html
# Run specific test file
poetry run pytest tests/test_client.py
๐ง Development
Setting Up Development Environment
# Clone repository
git clone https://github.com/yourusername/n8n-flow-manager.git
cd n8n-flow-manager
# Install with dev dependencies
poetry install --with dev
# Install pre-commit hooks
poetry run pre-commit install
# Run linting
poetry run black src/ tests/
poetry run ruff src/ tests/
# Type checking
poetry run mypy src/
Project Roadmap
- Core client with async support
- Pydantic models for type safety
- Workflow, execution, and credential APIs
- CLI with Typer
- Jinja2 templating
- Smart execution polling
- Webhook management API
- Tag management
- Bulk operations
- Workflow validation before deploy
- Integration tests with mock n8n server
๐ API Reference
N8NClient
Main client for interacting with n8n API.
Methods:
workflows- WorkflowAPI instanceexecutions- ExecutionAPI instancecredentials- CredentialAPI instancehealth_check()- Verify API connection
WorkflowAPI
Methods:
list(active=None, tags=None)- List workflowsget(workflow_id)- Get workflow by IDcreate(workflow)- Create new workflowupdate(workflow_id, workflow)- Update workflowdelete(workflow_id)- Delete workflowactivate(workflow_id)- Activate workflowdeactivate(workflow_id)- Deactivate workflow
ExecutionAPI
Methods:
list(workflow_id=None, limit=100, status=None)- List executionsget(execution_id)- Get execution detailstrigger_workflow(workflow_id, input_data=None)- Trigger executionwait_for_execution(execution_id, timeout=300)- Wait for completionrun_and_wait(workflow_id, input_data=None, timeout=300)- Trigger and waitretry(execution_id)- Retry failed executiondelete(execution_id)- Delete execution
CredentialAPI
Methods:
list(credential_type=None)- List credentialsget(credential_id)- Get credential by IDcreate(credential)- Create credentialupdate(credential_id, credential)- Update credentialdelete(credential_id)- Delete credential
๐ค Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Contribution Guidelines
- Write tests for new features
- Follow existing code style (Black + Ruff)
- Update documentation as needed
- Add type hints to all functions
- Keep commits atomic and well-described
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- n8n - The workflow automation platform
- httpx - Async HTTP client
- Pydantic - Data validation
- Typer - CLI framework
- Rich - Terminal formatting
๐ Support
- Documentation: GitHub Wiki
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- n8n Community: n8n Community Forum
๐ฏ Use Cases
DevOps & CI/CD
- Automate workflow deployments across environments
- Version control your n8n workflows in Git
- Integrate with GitLab/GitHub Actions pipelines
Disaster Recovery
- Scheduled backups of all workflows
- Quick restore capabilities
- Environment replication
Multi-Tenant Management
- Programmatically create workflows for new clients
- Template-based workflow generation
- Bulk operations across workflows
Monitoring & Observability
- Track execution success rates
- Monitor workflow health
- Automated alerting on failures
Made with โค๏ธ for the n8n community
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file n8n_flow_manager-0.1.0.tar.gz.
File metadata
- Download URL: n8n_flow_manager-0.1.0.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.12 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c224d95e2e8ca3af9ccbc07cf20fd08bcf331f08f7bf5da0a34c0e0508f57618
|
|
| MD5 |
0ddff5607cae14ae9e759589d33bf1fb
|
|
| BLAKE2b-256 |
6a2e94486194c982b3f3e22ac825588c3ef7d88a36946338de7aec8974f254b2
|
File details
Details for the file n8n_flow_manager-0.1.0-py3-none-any.whl.
File metadata
- Download URL: n8n_flow_manager-0.1.0-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.12 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a292580c8a3b6840aa2ae7eeb0a5a093bfcc730e01bbe440b5fbabbff1964719
|
|
| MD5 |
9fdcf1b673827e4575bb8c211c053feb
|
|
| BLAKE2b-256 |
110cb028037020638bcaab5aca755d5110d80dd0ed9336c1334ceeae9dd55f86
|