3.2 — Custom Tooling for Agents
Custom Tooling for Agents: Building Your Own API Nodes
The real power of n8n is the ability to write custom JavaScript or Python code within a node to perform tasks that standard nodes cannot. In this lesson, we learn how to build Custom API Nodes to bridge the gap between AI reasoning and real-world execution.
🏗️ The Code Node Architecture
- Input: The JSON data from the previous node (e.g., an AI-generated strategy).
- Logic: Your custom script (e.g., calculating ROI or formatting a PDF).
- Output: A clean JSON object ready for the next node.
Technical Snippet: The character-to-token estimator
In n8n, use a "Code" node to estimate token usage before sending to an LLM:
// Estimate tokens (approx 4 chars per token)
for (const item of $input.all()) {
item.json.token_estimate = Math.ceil(item.json.text_input.length / 4);
}
return $input.all();
Nuance: Error Handling in Code Nodes
Always use try/catch blocks in your custom nodes. If a script fails inside n8n without error handling, the entire workflow stops. A professional workflow "catches" the error and sends it to a Slack notification while letting the other leads continue.
Practice Lab: The Character Counter
- Trigger: Manual Trigger with a string.
- Action: Add a "Code" node. Write a script that returns the character count and the word count of that string.
- Verify: Run the workflow and ensure the data appears in the JSON output.
🇵🇰 Pakistan Activity: Build a PKR Invoice Generator
Create a Code node that generates a simple invoice for Pakistani clients:
Input JSON:
{"client": "Kolachi Restaurant", "service": "SEO Audit", "amount": 45000, "city": "Karachi"}
Code Node Logic:
for (const item of $input.all()) {
const gst = item.json.amount * 0.17; // 17% GST
item.json.invoice = {
client: item.json.client,
subtotal: `PKR ${item.json.amount.toLocaleString()}`,
gst: `PKR ${gst.toLocaleString()}`,
total: `PKR ${(item.json.amount + gst).toLocaleString()}`,
note: `Invoice for ${item.json.service} — ${item.json.city}`
};
}
return $input.all();
This is the kind of micro-automation that Pakistani freelancers need daily — and you can sell it as part of a bigger workflow package.
Homework: The Data Normalizer
Build a custom Code node that takes a "Messy" website URL (e.g., http://WWW.TEST.COM/path?query=1) and normalizes it to a clean root domain (test.com). Use this to deduplicate leads in your database.
📺 Recommended Videos & Resources
- n8n Code Node: JavaScript Execution Environment — Official documentation for custom code
- Type: Documentation
- Link description: Visit docs.n8n.io, search "Code Node"
- JavaScript for n8n Automation (Crash Course) — YouTube tutorial on writing production-ready code nodes
- Type: YouTube
- Link description: Search YouTube for "JavaScript n8n code node 2026"
- Error Handling in n8n Code Nodes (try/catch) — Critical for production workflows
- Type: YouTube
- Link description: Search YouTube for "n8n error handling try catch"
- URL Parser & Normalization (Real Example) — Practical regex patterns for domain extraction
- Type: YouTube
- Link description: Search YouTube for "URL normalization JavaScript"
- n8n Performance: Loop vs Code Node Efficiency — Optimize for speed when processing 1000+ leads
- Type: Community Forum
- Link description: Search n8n Community for "performance optimization"
🎯 Mini-Challenge
Write your URL normalizer: Create a Code node that takes 10 messy URLs (different formats, protocols, subdomains) and cleans them all to just the root domain. Test it, then use it to find and remove duplicate companies from a lead list!
🖼️ Visual Reference
📊 Custom Code Node Data Pipeline
Raw Input (Chaos):
┌────────────────────────────────────┐
│ " http://WWW.EXAMPLE.COM/path?x=1"│
│ "EXAMPLE.com" │
│ "192.168.1.1" │
│ "ftp://cdn.example.com/file.pdf" │
│ "example.com:8080" │
└──────────────┬─────────────────────┘
│
↓ Code Node
┌──────────────────┐
│ JavaScript Logic │
│ - Extract domain │
│ - Normalize case │
│ - Remove path │
│ - Remove port │
│ - Return root │
└─────────┬────────┘
│
Clean Output (Deduplicated):
┌────────────────────────────────────┐
│ "example.com" │
│ "example.com" (Duplicate removed) │
│ "192.168.1.1" │
│ "example.com" (Matched above) │
│ "example.com" (Matched above) │
└────────────────────────────────────┘
Result: 5 URLs → 2 unique domains
Lesson Summary
Quiz: Custom Tooling for Agents
5 questions to test your understanding. Score 60% or higher to pass.