Claude Code + n8n
Module 12.1: Claude Code + n8n
Section titled “Module 12.1: Claude Code + n8n”Estimated time: ~35 minutes
Prerequisite: Phase 11 (Automation & Headless)
Outcome: After this module, you will understand n8n basics, know how to trigger Claude Code from n8n workflows, and be able to build visual AI automation pipelines.
1. WHY — Why This Matters
Section titled “1. WHY — Why This Matters”You’ve learned to automate Claude with GitHub Actions and scripts. But what if you need:
- Non-developers to create AI workflows?
- Visual debugging of complex pipelines?
- Quick prototyping without writing YAML?
- Integration with 400+ services without coding each one?
n8n gives you visual workflow automation. Drag, drop, connect. Claude Code becomes a node in a larger system. Perfect for teams with mixed technical skills or rapid iteration. While GitHub Actions is code-centric, n8n is visual-first — build AI pipelines without touching YAML.
2. CONCEPT — Core Ideas
Section titled “2. CONCEPT — Core Ideas”What is n8n?
Section titled “What is n8n?”n8n is an open-source workflow automation platform — think Zapier but self-hosted. Your data stays with you, and you have full control.
Key features:
- Visual editor with drag-and-drop workflow design
- 400+ integrations (Slack, Google Sheets, databases, APIs)
- Triggers: webhooks, schedules, email, database changes
- Can run any CLI command — this is how we connect Claude Code
n8n + Claude Code Architecture
Section titled “n8n + Claude Code Architecture”[Trigger] → [n8n Workflow] → [Execute Claude Code] → [Process Output] → [Action] │ │ │ │ │ Email Visual Editor claude -p Parse JSON Slack Webhook Drag & Drop Headless Transform Database Schedule TrelloKey n8n Nodes for Claude Code
Section titled “Key n8n Nodes for Claude Code”| Node | Purpose | Use Case |
|---|---|---|
| Execute Command | Run claude -p | Core Claude execution |
| HTTP Request | Call APIs directly | Alternative approach |
| Code | JavaScript processing | Parse Claude output |
| IF | Conditional branching | Route based on AI response |
| Set | Transform data | Prepare prompts |
Installation
Section titled “Installation”# Option 1: npm (recommended for development)$ npm install -g n8n$ n8n start
# Option 2: Docker (recommended for production)$ docker run -it --rm -p 5678:5678 n8nio/n8nAccess the editor at http://localhost:5678.
3. DEMO — Step by Step
Section titled “3. DEMO — Step by Step”Scenario: Build a workflow where incoming emails are summarized by Claude and sent to Slack.
Step 1: Install and start n8n
Section titled “Step 1: Install and start n8n”$ npm install -g n8n$ export ANTHROPIC_API_KEY="sk-ant-api03-..."$ n8n startExpected output:
n8n ready on port 5678Editor is now accessible via: http://localhost:5678Step 2: Create new workflow
Section titled “Step 2: Create new workflow”- Open
http://localhost:5678 - Click “New Workflow”
- Name it “Email Summarizer”
Step 3: Add Webhook trigger (for testing)
Section titled “Step 3: Add Webhook trigger (for testing)”Add a Webhook node:
- Method: POST
- Path:
/email-summary
This gives you a test URL like http://localhost:5678/webhook/email-summary.
Step 4: Add Execute Command node for Claude
Section titled “Step 4: Add Execute Command node for Claude”Add an Execute Command node and configure:
{ "command": "claude", "arguments": "-p \"Summarize this email. At the end, write URGENT: YES or URGENT: NO.\n\nSubject: {{ $json.subject }}\n\nBody: {{ $json.body }}\""}Step 5: Parse Claude output with Code node
Section titled “Step 5: Parse Claude output with Code node”Add a Code node:
const response = $input.first().json.stdout;const isUrgent = response.includes('URGENT: YES');const summary = response.replace(/URGENT: (YES|NO)/g, '').trim();
return [{ json: { summary, isUrgent, originalSubject: $('Webhook').first().json.body.subject }}];Step 6: Add IF node for routing
Section titled “Step 6: Add IF node for routing”Add an IF node:
- Condition:
{{ $json.isUrgent }}equalstrue - True branch → Urgent Slack channel
- False branch → Regular Slack channel
Step 7: Add Slack nodes
Section titled “Step 7: Add Slack nodes”Add two Slack nodes:
True branch (urgent):
{ "channel": "#urgent", "text": "🚨 *Urgent Email*\n\n{{ $json.summary }}"}False branch (regular):
{ "channel": "#email-summaries", "text": "📧 *Email Summary*\n\n{{ $json.summary }}"}Step 8: Test the workflow
Section titled “Step 8: Test the workflow”$ curl -X POST http://localhost:5678/webhook/email-summary \ -H "Content-Type: application/json" \ -d '{"subject": "Server down!", "body": "Production server is unresponsive since 3am."}'Expected: Claude summarizes, detects urgency, routes to #urgent channel.
4. PRACTICE — Try It Yourself
Section titled “4. PRACTICE — Try It Yourself”Exercise 1: Webhook to Claude
Section titled “Exercise 1: Webhook to Claude”Goal: Create a simple webhook that accepts a prompt and returns Claude’s response.
Instructions:
- Create workflow with Webhook trigger
- Add Execute Command:
claude -p "{{ $json.prompt }}" - Add “Respond to Webhook” node returning the output
- Test with curl
💡 Hint
The “Respond to Webhook” node lets you return data to the HTTP caller. Connect it after the Execute Command node.
✅ Solution
Workflow: Webhook → Execute Command → Respond to Webhook
Execute Command config:
{ "command": "claude", "arguments": "-p \"{{ $json.prompt }}\""}Respond to Webhook config:
{ "respondWith": "json", "responseBody": "={{ $json.stdout }}"}Test:
curl -X POST http://localhost:5678/webhook/xxx \ -H "Content-Type: application/json" \ -d '{"prompt": "What is 2+2?"}'Exercise 2: Scheduled Daily Analysis
Section titled “Exercise 2: Scheduled Daily Analysis”Goal: Every day at 9am, fetch a webpage and have Claude analyze it.
Instructions:
- Schedule Trigger: daily at 9:00
- HTTP Request: fetch a news page or API
- Execute Command: Claude analyzes content
- Send email with analysis
💡 Hint
Use the Schedule Trigger node with cron expression 0 9 * * *. The HTTP Request node can fetch any URL.
✅ Solution
Workflow: Schedule → HTTP Request → Execute Command → Send Email
Schedule: 0 9 * * * (9am daily)
HTTP Request: https://news.ycombinator.com/
Execute Command:
{ "command": "claude", "arguments": "-p \"Summarize the top 5 stories from this page:\n\n{{ $json.data }}\""}Exercise 3: Multi-Step Pipeline
Section titled “Exercise 3: Multi-Step Pipeline”Goal: Chain multiple Claude calls: analyze → suggest → format.
Instructions:
- Webhook input with raw text
- Claude #1: Analyze sentiment and key points
- Claude #2: Suggest actions based on analysis
- Claude #3: Format as bullet points
- Return final output
💡 Hint
Each Execute Command node’s output becomes input for the next. Reference previous outputs with $('NodeName').first().json.stdout.
✅ Solution
Workflow: Webhook → Execute Command (Analyze) → Execute Command (Suggest) → Execute Command (Format) → Respond to Webhook
Node 1 - Analyze:
{ "command": "claude", "arguments": "-p \"Analyze this text for sentiment and key points:\n\n{{ $json.text }}\""}Node 2 - Suggest (reference Node 1):
{ "command": "claude", "arguments": "-p \"Based on this analysis, suggest 3 actions:\n\n{{ $('Execute Command').first().json.stdout }}\""}Node 3 - Format (reference Node 2):
{ "command": "claude", "arguments": "-p \"Format these suggestions as bullet points:\n\n{{ $('Execute Command1').first().json.stdout }}\""}Test:
curl -X POST http://localhost:5678/webhook/pipeline \ -H "Content-Type: application/json" \ -d '{"text": "Customer complained about slow delivery times."}'5. CHEAT SHEET
Section titled “5. CHEAT SHEET”Quick Start
Section titled “Quick Start”npm install -g n8nexport ANTHROPIC_API_KEY="sk-ant-..."n8n start# Open http://localhost:5678Execute Command Node
Section titled “Execute Command Node”{ "command": "claude", "arguments": "-p \"{{ $json.prompt }}\""}Parse Output (Code Node)
Section titled “Parse Output (Code Node)”const output = $input.first().json.stdout;return [{ json: { result: output } }];Common Triggers
Section titled “Common Triggers”| Trigger | Use Case |
|---|---|
| Webhook | External events, API calls |
| Schedule | Cron-based, daily/hourly |
| Email (IMAP) | Incoming emails |
| Database | Row changes |
Data References
Section titled “Data References”{{ $json.field }} // Current node{{ $('NodeName').first().json.field }} // Other node{{ $input.first().json.stdout }} // In Code nodeUseful Nodes
Section titled “Useful Nodes”| Node | Purpose |
|---|---|
| Set | Prepare/transform data |
| IF | Conditional routing |
| Switch | Multi-way routing |
| Merge | Combine branches |
| Error Trigger | Handle failures |
6. PITFALLS — Common Mistakes
Section titled “6. PITFALLS — Common Mistakes”| ❌ Mistake | ✅ Correct Approach |
|---|---|
| Claude CLI not in n8n’s PATH | Use full path: /usr/local/bin/claude |
| API key not available | Set ANTHROPIC_API_KEY before starting n8n |
| No timeout on Claude calls | Set timeout in Execute Command (60000ms+) |
| Quotes breaking prompts | Use Set node to prepare complex prompts |
| Ignoring Claude errors | Check exitCode and stderr in output |
| Huge prompts in command line | Write to temp file, pass file path |
| No error handling | Add Error Trigger node to catch failures |
7. REAL CASE — Production Story
Section titled “7. REAL CASE — Production Story”Scenario: Vietnamese marketing agency receives 50+ client briefs daily via email. Manual processing took 2 hours every morning.
Problem: Briefs arrived in different formats. Staff had to read each one, extract requirements, log to spreadsheet, create project cards, and notify teams. Error-prone and slow.
n8n + Claude Solution:
[Email Trigger] → [Claude: Extract] → [Claude: Suggest] → [Google Sheets] ↓ [Slack] ← [Trello: Create Card]Implementation:
- Email arrives with client brief
- Claude extracts: client name, deadline, requirements, budget
- Claude suggests: team assignment, approach, timeline
- Auto-logs to Google Sheets
- Creates Trello card with all details
- Notifies appropriate Slack channel
Results (after 2 months):
- Processing time: 2 hours → 5 minutes (just review)
- Zero missed briefs
- Consistent data extraction format
- Marketing manager modifies workflow herself — no engineering needed
Quote: “n8n let our marketing manager build AI automation without asking engineering. She just drags and drops. We went from ‘can you build this?’ to ‘I already built it.’”
Cost: n8n is free (self-hosted). Claude API: ~$30/month for 50 briefs/day.
Next: Module 12.2: Workflow Patterns →