Self-Host n8n + AI: Build Intelligent Automation Workflows in 20 Minutes
Imagine connecting your AI models to your entire tech stack — automatically summarizing emails, triaging support tickets, generating content, or orchestrating complex multi-step tasks with LLM reasoning at the core. That's exactly what you get when you combine n8n (the open-source workflow automation platform) with AI capabilities. And when you self-host it, you keep full control over your data, your prompts, and your API keys.
In this guide, we'll set up n8n with AI integration on your own server in about 20 minutes. You'll be able to build workflows that leverage OpenAI, Anthropic, Ollama, or any OpenAI-compatible API — all running on infrastructure you control.
Why Self-Host n8n with AI?
Cloud automation platforms like Zapier and Make are convenient, but they come with limitations: per-task pricing that scales brutally, limited AI integration options, and your data flowing through third-party servers. Self-hosting n8n solves all three:
- Cost control: Unlimited executions on your own infrastructure. Pay for your VPS, not per operation.
- Data privacy: Your workflows, prompts, and data never leave your server unless you want them to.
- AI flexibility: Connect to any LLM — cloud APIs, local Ollama instances, or self-hosted LiteLLM gateways.
- Extensibility: 400+ integrations plus custom code nodes when you need something bespoke.
Prerequisites
Before we start, you'll need:
- A VPS or dedicated server with at least 2 vCPUs and 4GB RAM (8GB recommended if running local AI models)
- Docker and Docker Compose installed (see our Docker setup guide if needed)
- A domain name pointed to your server (optional but recommended for HTTPS)
- An AI provider API key (OpenAI, Anthropic, Groq, etc.) or a local Ollama instance
Step 1: Create the Docker Compose File
Create a directory for n8n and set up the compose file:
mkdir -p ~/n8n && cd ~/n8n
mkdir -p ~/.n8n
Create docker-compose.yml:
version: "3"
services:
n8n:
image: n8nio/n8n:latest
restart: unless-stopped
ports:
- "5678:5678"
environment:
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=your-secure-password-here
- N8N_HOST=n8n.yourdomain.com
- N8N_PORT=5678
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://n8n.yourdomain.com/
- GENERIC_TIMEZONE=UTC
# AI Provider Configuration
- OPENAI_API_KEY=sk-your-openai-key-here
- ANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
# Optional: Ollama for local models
- OLLAMA_HOST=http://host.docker.internal:11434
volumes:
- ~/.n8n:/home/node/.n8n
- /etc/localtime:/etc/localtime:ro
extra_hosts:
- "host.docker.internal:host-gateway"
Note: If you're using Ollama for local AI models, make sure it's running on the host and accessible. The extra_hosts entry allows the container to reach your host machine.
Step 2: Launch n8n
docker compose up -d
Check the logs to confirm it's running:
docker compose logs -f n8n
You should see output indicating n8n is ready on port 5678.
Step 3: Set Up Reverse Proxy (Optional but Recommended)
For production use, put n8n behind a reverse proxy with SSL. Here's a Traefik configuration example:
# Add to your Traefik docker-compose or labels
labels:
- "traefik.enable=true"
- "traefik.http.routers.n8n.rule=Host(`n8n.yourdomain.com`)"
- "traefik.http.routers.n8n.entrypoints=websecure"
- "traefik.http.routers.n8n.tls.certresolver=letsencrypt"
- "traefik.http.services.n8n.loadbalancer.server.port=5678"
Or use Caddy for simplicity:
n8n.yourdomain.com {
reverse_proxy localhost:5678
}
Step 4: Create Your First AI Workflow
Navigate to https://n8n.yourdomain.com (or http://your-server-ip:5678) and log in with your basic auth credentials.
Example: AI-Powered Email Summarizer
Let's build a workflow that automatically summarizes incoming emails using AI:
- Trigger: Add an IMAP Email node to watch for new emails
- Extract: Use a "Set" node to extract the email body and subject
- AI Processing: Add an OpenAI node (or AI node)
- Model:
gpt-4o-mini(cost-effective) orgpt-4ofor complex tasks - Prompt:
Summarize this email in 2-3 sentences: {{ $json.body }} - Temperature: 0.3 for consistent summaries
- Model:
- Action: Send the summary to Slack, Discord, or save to a database
Example: Content Generation Pipeline
Build a more complex workflow:
- Schedule Trigger: Run daily at 8 AM
- RSS Feed: Pull latest articles from your favorite blogs
- AI Node: Generate a LinkedIn post based on each article
- System prompt: "You are a tech content writer. Create engaging LinkedIn posts from articles."
- User prompt: "Article: {{ $json.content }}. Create a post with hook, 3 key points, and CTA."
- Filter: Only proceed if AI confidence is high
- Output: Save to Google Sheets or post directly via LinkedIn API
Step 5: Connect to Local AI Models (Ollama)
One of the most powerful self-hosting combinations is n8n + Ollama. Run AI models locally with zero API costs:
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Pull a model
ollama pull llama3.2
# Ollama runs on http://localhost:11434 by default
In n8n, use the HTTP Request node or the dedicated Ollama node:
- URL:
http://host.docker.internal:11434/api/generate - Model:
llama3.2,mistral,codellama, etc. - Prompt: Your workflow data
This setup gives you fully private AI automation — no data ever leaves your server.
Step 6: Verification
Test your setup:
- Create a simple workflow with a Manual Trigger → OpenAI node → Set node
- Run it and check that the AI response appears in the output
- Check n8n executions log to see full request/response details
- If using Ollama, verify connectivity:
curl http://localhost:11434from the host
Advanced: AI Agent Workflows
n8n's AI capabilities go beyond single prompts. You can build agent-like workflows:
- Chain multiple AI calls: Research → Outline → Draft → Edit
- Tool use: Give AI access to web search, calculators, or your internal APIs
- Conditional routing: Use AI to classify input and route to different processing paths
- Human-in-the-loop: Pause workflows for manual approval before AI takes action
Next Steps
Now that you have n8n + AI running, explore these related guides:
- Self-Host Ollama — Run AI models locally for fully private automation
- Self-Host LiteLLM — One API gateway for all your AI models
- Self-Host n8n — General n8n setup guide
- Self-Host Grafana + Prometheus — Monitor your n8n instance
With n8n and AI on your own infrastructure, you've unlocked unlimited automation possibilities — without per-task pricing or data privacy concerns. Build something great.