What is n8n and Why Should You Care?
n8n (pronounced "n-eight-n") is an open-source workflow automation tool that lets you connect APIs, databases, and services without writing server code. Think of it as Zapier or Make.com, but free, self-hosted, and with no limits on task count.
For AI projects specifically, n8n shines because it bridges the gap between AI models and the rest of your infrastructure. You can trigger AI tasks, process results, and route outputs to various destinations—all visually.
Core Concepts
Before diving into examples, understand these building blocks:
Nodes
Nodes are individual operations. Each node does one thing: fetch data, transform data, call an API, send an email, etc. n8n has 400+ built-in nodes, and you can create custom ones with JavaScript or Python.
Workflows
Workflows are sequences of nodes connected together. Data flows from node to node, with each node processing and passing along information to the next.
Triggers
Triggers start workflows. Common types:
- Webhook: Start when an HTTP request is received
- Schedule: Start at specific times (cron jobs)
- Manual: Start with a button click
- Event-based: Start when something happens (new email, file upload)
Real-World Example: AI Daily Digest
Here's a workflow I use for my RAG SaaS to send daily email summaries:
Step 1: Schedule Trigger
Set to run every morning at 8 AM using cron syntax:
0 8 * * *
Step 2: Query Database
Connect to PostgreSQL and fetch users with activity in the last 24 hours:
SELECT email, name,
COUNT(*) as conversations,
MAX(created_at) as last_activity
FROM users u
JOIN conversations c ON u.id = c.user_id
WHERE c.created_at > NOW() - INTERVAL '24 hours'
GROUP BY u.id;
Step 3: Process with AI
For each user, generate a summary using Ollama:
{
"node": "HTTP Request",
"method": "POST",
"url": "http://localhost:11434/api/generate",
"body": {
"model": "llama3.2",
"prompt": "Summarize this user's conversations: {{ $json.conversation_history }}"
}
}
Step 4: Send Email
Use the Gmail node to send personalized digest emails with the AI-generated summary.
AI Pipeline Automation
n8n excels at orchestrating complex AI workflows. Here's how I automated document processing:
The Workflow
- File Trigger: Watch a Dropbox folder for new PDFs
- PDF Extraction: Extract text from uploaded documents
- Chunking: Split text into semantic chunks
- Embedding: Generate embeddings with a local model
- Store: Insert into PostgreSQL vector database
- Notify: Send confirmation Slack message
Error Handling
Critical for production systems. I use:
- Error Trigger Node: Catch any failures in the workflow
- Retry Logic: Automatically retry failed API calls 3 times
- Dead Letter Queue: Store failed items for manual review
- Alerting: Slack notification when workflows fail
Connecting to External Services
n8n's strength is connecting disparate services. Here's a template for connecting to common AI services:
OpenAI Integration
{
"node": "HTTP Request",
"method": "POST",
"url": "https://api.openai.com/v1/chat/completions",
"authentication": {
"mode": "header",
"headerAuth": "Bearer YOUR_API_KEY"
},
"body": {
"model": "gpt-4",
"messages": [
{"role": "user", "content": "{{ $json.user_prompt }}"}
]
}
}
Slack Notifications
{
"node": "Slack",
"parameters": {
"channel": "#ai-alerts",
"text": "Workflow completed: {{ $json.summary }}"
}
}
Advanced Features
Sub-nodes and Loops
Process multiple items in parallel using the Split In Batches node:
[
{"name": "User A", "email": "userA@example.com"},
{"name": "User B", "email": "userB@example.com"},
{"name": "User C", "email": "userC@example.com"}
]
Each item goes through the workflow simultaneously, dramatically reducing processing time.
Code Nodes
For complex transformations, use the Code node with JavaScript:
// Transform data for API call
const items = $input.all();
return items.map(item => ({
id: item.json.id,
name: item.json.name.toUpperCase(),
timestamp: new Date().toISOString()
}));
Variables and Memory
Store state between workflow runs using:
- Workflow Variables: Temporary storage for current run
- Static Data: Persistent key-value storage
- Database Nodes: Read/write to external databases
Deployment Options
Choose based on your needs:
- n8n Cloud: Managed service, easiest setup, has limits
- Self-hosted Docker: Full control, no limits, requires maintenance
- Self-hosted with n8n Cloud: Best of both worlds
Best Practices
- Start with triggers: Understand when and why your workflow runs
- Test incrementally: Run with test data before going live
- Add logging: Use Slack/email nodes to track workflow progress
- Version control: Export workflows as JSON for backup
- Monitor metrics: Track execution times and failure rates
Conclusion
n8n has become an essential tool in my AI engineering toolkit. It bridges the gap between AI models and production systems without requiring DevOps expertise. Start with one simple workflow—scheduled email reminders, for example—and expand from there. The productivity gains compound quickly.