Learn how to use the Code node in n8n with simple steps, examples, and tips to streamline automation and enhance your workflows.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
The Code node in n8n lets you run small, controlled pieces of JavaScript inside a workflow so you can transform data, build custom logic, or prepare API payloads. You drop it into a workflow like any other node, and it receives the JSON from previous nodes and outputs JSON for the next ones. Think of it as a tiny serverless function living inside your automation, good for logic that’s too complex for other nodes but still small enough that it doesn’t belong in a full external service.
The Code node is a JavaScript execution step. It runs in a sandbox, synchronously, during the workflow execution. You write JavaScript in the editor section called Run Once for Each Item (default mode) or Run Once (explicit mode if you switch it), depending on how you want it to behave.
You always output an array of objects containing a json property. This is what the next node receives.
Drop a Code node into your workflow → open it → write JavaScript inside → make sure you return an array of objects with a json key. That’s the minimum you need to use it correctly.
Inside a Code node, the input data is accessible via special variables provided by n8n:
$input.item.json).You don't create these — they’re injected by n8n.
This transforms a user's full name into first and last name. It receives something like { "name": "Ada Lovelace" } and outputs structured data.
// Split the name into two parts
const [first, last] = item.json.name.split(' ');
// Always return an array of objects with json:
return [
{
json: {
firstName: first,
lastName: last,
}
}
];
This example collects all email addresses from previous nodes and outputs a combined list in one item.
// items = array of all input items
const emails = items.map(i => i.json.email);
// Return one item that contains all emails
return [
{
json: {
emails: emails
}
}
];
The Code node is your escape hatch for logic that’s awkward with UI nodes but too lightweight to build a microservice for. It’s best used for transformations, validations, and small custom branching logic. It is not a full programming runtime, so treating it as such leads to slow workflows, timeouts, and painful debugging. Keep it small and purposeful, and test with sample data as you build.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.