Phase 2: n8n for AI Automation · 50 min · n8n · JavaScript
Sub-Workflows and Code Nodes — Advanced n8n Patterns
Modular workflows are maintainable workflows.
Hiring signal: Sub-workflows and Code nodes are what separate n8n beginners from n8n power users. In technical interviews, being able to explain when to use a sub-workflow vs a Code node, and write a Code node that transforms JSON data, demonstrates real n8n production experience.
What you will learn
- Build n8n sub-workflows for reusable logic and modular automation design
- Use Code nodes to write custom JavaScript for data transformation, API calls, and conditional routing
- Decide when to use a Code node vs a built-in node
- Apply common Code node patterns: JSON parsing, date formatting, array filtering, HTTP requests
The Problem
A marketing team builds an n8n workflow that handles lead processing: receive webhook → enrich with Clearbit → score with AI → route by score → add to CRM → send welcome email. It works. Then they add a second workflow for customer support tickets: receive email → classify with AI → route by category → create ticket → notify agent. Both workflows need the same AI classification logic and the same CRM update pattern. Instead of reusing, they copy-paste the nodes. Three months later, the AI prompt needs updating — they change it in one workflow, forget the other, and leads get misclassified.
This is the modularity problem. Without sub-workflows and Code nodes, n8n workflows become monolithic, duplicated, and unmaintainable. Sub-workflows let you build reusable logic blocks. Code nodes let you write custom JavaScript when no built-in node fits.
Treat workflows like code, not scripts
Every principle from software engineering applies: DRY (don't repeat yourself), single responsibility, separation of concerns. Sub-workflows are your functions. Code nodes are your escape hatch when built-in nodes can't express the logic.
The Concept
Sub-Workflows: Functions for n8n
A sub-workflow is a workflow that gets called by another workflow. The parent passes data in, the sub-workflow processes it, and returns results.
┌─────────────────────────────────────────────────────────┐
│ PARENT WORKFLOW │
│ │
│ [Trigger] → [Transform] → [Execute Sub-Workflow] → ... │
│ │ │
│ ▼ │
│ ┌──────────────────────┐ │
│ │ SUB-WORKFLOW │ │
│ │ │ │
│ │ [Input] → [Process] │ │
│ │ → [Return Value] │ │
│ └──────────────────────┘ │
│ │ │
│ ▼ │
│ [Continue] │
└─────────────────────────────────────────────────────────┘
| Property | Main Workflow | Sub-Workflow |
|---|
| Trigger | Webhook, schedule, manual | Execute Workflow trigger (called by parent) |
| Input | External event | Data from parent workflow |
| Output | Final result | Return value to parent |
| Reusable | Usually no | Yes — called from multiple parents |
Code Nodes: The Escape Hatch
Code nodes let you write JavaScript (or Python) directly inside n8n. Use them when:
- Data transformation that's too complex for the Set node
- Conditional routing with logic beyond IF/Switch nodes
- Custom API calls that need header manipulation or signing
- Array manipulation: filtering, mapping, grouping, deduplication
- Date/time arithmetic beyond what the Date node supports
You have three workflows that all need to validate and normalize phone numbers. What's the best approach?
A sub-workflow centralizes the logic. When the validation rules change (e.g., adding a new country code format), you update one sub-workflow and all three parent workflows automatically use the new logic. Duplicating Code nodes means three places to update — and you'll forget one.
When to Use a Code Node vs a Built-in Node
| Situation | Use Built-in | Use Code Node |
|---|
| Simple data mapping | Set node | — |
| HTTP request | HTTP Request node | — |
| If/else branching | IF node | — |
| Multi-field transform with custom logic | — | Code node |
| Custom authentication signing (HMAC, AWS Sig v4) | — | Code node |
| Deduplicate items by a computed key | — | Code node |
| Parse a non-standard date format | — | Code node |
| Simple value extraction | Set node | — |
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Build It, Use It, Ship It, Exercises, Key Terms, Common Pitfalls — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy