Your First AI Workflow with n8n
Overview
Section titled “Overview”n8n is a powerful workflow automation tool that lets you connect AI models like OpenAI’s GPT to hundreds of apps without code. Build smart automations visually with a drag-and-drop interface.
What you’ll build: An AI-powered content moderation workflow that automatically analyzes user submissions and routes them based on sentiment.
Use cases: Content moderation, email auto-responses, data enrichment, customer support triage.
Time: 30 minutes
Prerequisites
Section titled “Prerequisites”- n8n account (free at n8n.cloud) or self-hosted installation
- OpenAI API key
- Basic understanding of automation concepts
Setting Up n8n
Section titled “Setting Up n8n”Option 1: Cloud (Easiest)
Section titled “Option 1: Cloud (Easiest)”- Go to n8n.cloud
- Create free account
- Access your workflow editor
Option 2: Self-Hosted (Docker)
Section titled “Option 2: Self-Hosted (Docker)”docker run -it --rm \ --name n8n \ -p 5678:5678 \ -v ~/.n8n:/home/node/.n8n \ n8nio/n8nAccess at http://localhost:5678
Connecting OpenAI to n8n
Section titled “Connecting OpenAI to n8n”Step 1: Add OpenAI Credentials
Section titled “Step 1: Add OpenAI Credentials”- Click Settings > Credentials
- Click Add Credential
- Search “OpenAI”
- Enter your API key
- Test connection
- Save
Step 2: Create Your First Workflow
Section titled “Step 2: Create Your First Workflow”Workflow: AI Content Analyzer
Components:
- Webhook - Receives content submissions
- OpenAI - Analyzes sentiment
- IF - Routes based on result
- HTTP Request - Sends to appropriate endpoint
Building the Workflow
Section titled “Building the Workflow”1. Add Webhook Trigger
- Drag “Webhook” node to canvas
- Set path:
/analyze-content - Method: POST
- Click “Listen for test event”
2. Add OpenAI Node
- Search “OpenAI” and add node
- Connect to webhook
- Select credential
- Operation: “Message a Model”
- Model:
gpt-4o-mini - Prompt:
Analyze this content for sentiment and toxicity:
Content: {{$json.body.content}}
Return JSON with format:{ "sentiment": "positive|negative|neutral", "toxic": boolean, "confidence": 0.0-1.0, "reason": "brief explanation"}3. Add IF Node
- Connect to OpenAI node
- Condition:
{{$json.toxic}} === true - True branch: Flag for review
- False branch: Approve
4. Add Action Nodes
For True (toxic content):
- HTTP Request node
- URL: Your moderation endpoint
- Method: POST
- Body:
{{$json}}
For False (clean content):
- HTTP Request node
- URL: Your approval endpoint
- Method: POST
- Body:
{{$json}}
Testing the Workflow
Section titled “Testing the Workflow”- Click Execute Workflow
- Send test data via webhook:
curl -X POST http://localhost:5678/webhook/analyze-content \ -H "Content-Type: application/json" \ -d '{"content": "This is a great product!"}'- Check execution log
- Verify correct routing
Common n8n + AI Patterns
Section titled “Common n8n + AI Patterns”Pattern 1: Email Auto-Responder
Section titled “Pattern 1: Email Auto-Responder”Gmail Trigger → OpenAI (Draft Response) → IF (Confident?) → Gmail (Send)Pattern 2: Data Enrichment
Section titled “Pattern 2: Data Enrichment”Database Trigger → OpenAI (Analyze) → Update Record → Slack NotifyPattern 3: Content Generation
Section titled “Pattern 3: Content Generation”Scheduler → OpenAI (Generate) → Format → Post to CMSBest Practices
Section titled “Best Practices”1. Error Handling
Section titled “1. Error Handling”Add “Error Trigger” node:
- Catches workflow failures
- Logs to monitoring system
- Sends alerts
2. Rate Limiting
Section titled “2. Rate Limiting”Use “Wait” node between API calls to avoid rate limits.
3. Cost Control
Section titled “3. Cost Control”Monitor OpenAI usage:
- Set max tokens
- Use cheaper models for simple tasks
- Cache frequent requests
4. Testing
Section titled “4. Testing”- Test with sample data first
- Use n8n’s test execution mode
- Monitor costs during testing
Example Workflows
Section titled “Example Workflows”Email Sentiment Analysis
Section titled “Email Sentiment Analysis”Gmail Trigger ↓OpenAI (Analyze Sentiment) ↓IF (Urgent + Negative?) ↓ Yes ↓ NoSlack Alert Label EmailSocial Media Monitor
Section titled “Social Media Monitor”Twitter Trigger (New Mentions) ↓OpenAI (Classify Intent) ↓Switch (Intent Type) ↓ Question ↓ Complaint ↓ Praise OpenAI Reply Alert Team Thank UserTroubleshooting
Section titled “Troubleshooting”Issue: OpenAI timeout
- Increase timeout in HTTP Request settings
- Use shorter prompts
- Check API status
Issue: Webhook not receiving data
- Verify webhook URL is correct
- Check firewall settings
- Test with curl first
Issue: High costs
- Use
gpt-4o-miniinstead ofgpt-4o - Add caching layer
- Limit workflow triggers
Next Steps
Section titled “Next Steps”Learn more:
Advanced workflows:
- Multi-step AI reasoning chains
- Custom function nodes
- Sub-workflows for reusability
Found an issue? Open an issue!