Resources · n8n integration

n8n Code nodeConfigure Code in n8n.

The n8n Code node runs your own JavaScript or Python on the items flowing through a workflow. Two parameters drive it: Language and Mode. Mode decides whether the code runs once for the whole batch or once per item. Useful the day no built-in node does the transform you need.

Verified Trustpilot reviews · AI, automation & growth agency

Why automate

What does the n8n Code node actually do?

The Code node lets you write JavaScript or Python that runs inside a workflow, on the data handed over by the node before it. n8n passes data between nodes as a list of objects, each one wrapped under a json key, and your code reads that shape and returns it. It exists for the transforms and the logic the built-in nodes don't cover, and for nothing else.

First scenario, cleaning a payload before it hits storage. An API answers with nested objects and half the keys you don't need. A few lines in the Code node flatten it into flat rows, and the write to Postgres stops failing on a column that was never there. The node runs on each incoming item, so the shape you return is the shape the next node receives.

Second scenario, one summary instead of many messages. With Mode on Run Once for All Items, the code sees the whole batch at once, counts what matters and returns a single item. That one item becomes one message in Slack rather than a notification per row. The opposite setting, Run Once for Each Item, suits per record enrichment where each pass only cares about its own object.

Third scenario, logic no node exposes. Sorting items by a computed score, building a string from three fields, deciding which branch a record belongs to: that is where the Code node earns its place. The published templates show the pattern well, from an interactive JavaScript tutorial to a GitHub pull request review that sits between an HTTP call and an AI chain.

Now the alternatives, because Code is often the wrong answer. Code nodes cannot access credentials by design: this.getCredentials() and $getCredentials() throw, and that is deliberate, it keeps authentication data out of your script. An authenticated API call belongs in the HTTP Request node, which handles credentials for you, or in the dedicated node for that service. To route dynamically between credentials, a Switch node in front of two nodes does the job. And if your plan was to keep a list of already processed records in static data, the Remove Duplicates node saves that state across executions for you.

The known limits are worth reading before you start. The JavaScript sandbox does not support import or export, so modules load with require. On n8n Cloud, importing modules is not supported at all; on a self-hosted instance you install the module in the same environment as n8n, or extend the Docker image, then allow it through the NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL environment variables. Version 2 is the version described here. If you are weighing platforms before writing any code, the n8n review covers that ground.

Parameters

Which parameters does the Code node expose?

The Code node has 2 parameters. For each one: the node as you configure it in n8n, what the parameter changes, and our field notes.

01

Mode

mode

What you see in n8n

Notes & use cases

Mode decides how many times your code runs against the incoming data. It is the first thing to set, because it changes what your script can see on each pass.

Key parameters

  • Run Once for All Items (runOnceForAllItems): runs the code only once, no matter how many input items there are, with the whole batch in scope.
  • Run Once for Each Item (runOnceForEachItem): runs the code as many times as there are input items, one object in scope per pass.
  • JavaScript and Python: the code box below the selector, where you write what actually runs.
Use cases
counting and summarizing a batch calls for the first setting, rewriting one field on every record calls for the second.
02

Language

language

What you see in n8n

Notes & use cases

Language picks the runtime that evaluates the code box. The selector offers two values, and the choice changes the built-in helpers you can call, never the data structure the node has to return.

Key parameters

  • JavaScript (javaScript): gives you luxon variables such as $today for dates and $jmespath for querying JSON structures.
  • Python (pythonNative): gives you the equivalent built-in methods and variables, _today for dates and _jmespath for queries.
Use cases
teams already writing Python for their data scripts keep their habits here; everyone else usually stays on JavaScript, which is also the language of the published tutorials.
Need help

Need help automating Code with n8n?

A person reads every message.

FAQ

Code node in n8n: your questions

01Is the n8n Code node included, on Cloud and self-hosted?
Yes. Code is a core node shipped with n8n, so there is nothing to install and no extra cost on the n8n side, on n8n Cloud as well as on a self-hosted instance running the Community Edition under the Sustainable Use license. n8n self-hosts through Docker or npm, or runs on n8n Cloud, the hosted offer from n8n, and a workflow behaves the same either way. One difference does apply to this node: importing external modules is not supported on Cloud, only on a self-hosted instance where you control the environment.
02What do you need to configure before the Code node runs?
Nothing on the account side. The Code node has no credential and no Authentication selector, so there is no key to paste and no app to authorize. Pick a Language, pick a Mode, write the code, done. That is also a hard limit: Code nodes cannot access credentials at all, by design, which keeps authentication data away from your script. The node does need a trigger in front of it, since it does not start a workflow on its own. A Schedule Trigger, a Webhook or the trigger of a service all work.
03What are the limits of the Code node?
Three matter in practice. Your code has to return the structure n8n expects, a list of objects each wrapped under a json key pointing to an object, otherwise the node errors with a message about items or about that json key not being an object. Returning nothing, or undefined, produces the same class of error. The JavaScript sandbox rejects import and export, so modules load through require, and only on self-hosted instances where the module is installed and allowed through environment variables. Version 2 is the current node version.
04When should you use Code rather than another node?
Use Code when the transform or the logic has no node of its own: reshaping nested data, computing a value from several fields, sorting or aggregating a batch. Use something else the moment a node already covers the job. An authenticated API call goes to the HTTP Request node, which supports credentials, or to the dedicated node for that service. Routing between different credentials belongs to a Switch node placed before them. Skipping records already handled in a previous execution is what the Remove Duplicates node does, and it stores that state for you.
05n8n or Make when you need custom code?
It depends on where you want the code to run. Make is a hosted automation platform with no self-hosting option, billed per operation, so a workflow that loops over many items has a cost that follows the volume. n8n runs on your own server through Docker or npm, or on n8n Cloud if you prefer not to host it, and the same workflow behaves identically. If your data has to stay inside your infrastructure, or if you need modules installed next to n8n, self-hosting settles the question. If you never want to touch a server, the hosted side of both platforms is the honest comparison.
Hack'celeration Lab

Get our weekly integration tips.

No spam. Unsubscribe anytime.