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.
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();
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.
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.