Skip to content
Guides

n8n item linking: how items flow and how pairedItem works

How n8n item linking works: why $('Node').item finds the right item, the errors when links break, and how to fix them with pairedItem or itemMatching.

By then8nblog Editors6 min read
Tested on n8n 2.48.6intermediate
Illustration of item linking in n8n, how $('Node').item finds the right item: automatic links when the same number of items goes in and out, the paired item unavailable error for new items from a Code node, and the fix of returning pairedItem or using itemMatching(index)

Every item a node outputs carries a link back to the input item it came from. That chain is how {{ $('Customers').item.json.email }} finds the right customer three nodes later, even after other nodes changed the data. When a node creates new items without that link, usually a Code node, .item fails with "Paired item data for item from node ... is unavailable", and the fix is to return pairedItem with each item.

The short version:

  • Data between nodes is a list of items, each wrapped in json (and binary for files). Most nodes run once per item.
  • n8n links items automatically in common cases. In our test, a Code node that returned the same number of new items was linked in order without any extra code.
  • Linking breaks when a Code node returns a different number of new items, when a node combines items (Aggregate, Summarize, Merge), or when you reference a node on another branch.
  • Fix it in the Code node by adding pairedItem: <input index> to each item you return, or read earlier data with $('Node').itemMatching(index).
  • Or avoid .item with .first(), .last() or .all()[index] when you know which item you want.

We ran every example in this guide on n8n 2.48.6, the current stable release, and copied the error messages from the executions.

Items: the shape of data in n8n

According to n8n's docs on how it structures data, what passes between nodes is always an array of objects, each with a json key (and a binary key for files):

[
  { "json": { "id": 1, "name": "Ada", "email": "ada@example.com" } },
  { "json": { "id": 2, "name": "Grace", "email": "grace@example.com" } }
]

When a node receives several items, it runs its operation once for each of them, and {{ $json.name }} means "the name of the item being processed right now." The Code node adds the json key and the array for you if you leave them out. Our expressions cheat sheet covers $json, $input and the rest of the syntax.

Each output item includes metadata, pairedItem, that points to the input item (or items) that produced it. Following those pointers back gives a thread through the whole workflow. When a node doesn't set the links itself, n8n's docs on how items link say it guesses:

  • One input, one output: the output links to the input.
  • One input, several outputs: every output links to that input.
  • Several inputs and outputs: if you keep the input items (reordered, or with some removed), n8n links them correctly. If the counts are equal, it links them in order. If the counts differ or you create completely new items, it can't link them.

That's why .item usually just works. In our test, a workflow had a Code node called Customers that returned three customers, and a second Code node, Rebuild, that returned three brand-new items with only the name:

return $input.all().map(item => ({ json: { name: item.json.name } }));

The output of Rebuild had pairedItem 0, 1 and 2 even though the code never set it, so an Edit Fields (Set) node after it could restore each email with {{ $('Customers').item.json.email }}.

.item versus .first(), .last() and .all()

ExpressionReturnsUse it when
$('Node').itemThe item in that node linked to the current itemYou want "the same record" from an earlier step
$('Node').first(), .last()The first or last item of that node, whatever item is runningThe node outputs one item (a config lookup, a token) or you want a fixed position
$('Node').all()[index]The item at a positionYou know the position
$('Node').itemMatching(index)In a Code node, the item linked to input item indexYou need linked data in code

When item linking breaks

The item linking errors page lists three situations. We reproduced each one; the messages n8n showed on 2.48.6 are worded differently from the docs, so search for both.

Our Rebuild node dropped the first customer and returned two new items:

return $input.all().slice(1).map(item => ({ json: { name: item.json.name } }));

With three items in and two new items out, n8n can't guess the links. The Edit Fields node with {{ $('Customers').item.json.email }} failed, and the execution stopped with:

Paired item data for item from node 'Rebuild' is unavailable. Ensure 'Rebuild' is providing the required output.

The docs call this "Info for expression missing from previous node." Custom and community nodes can cause it too; then the node's author has to fix it.

A node combined several items into one

We put an Aggregate node (Aggregate set to All Item Data (Into a Single List)) after Customers. Its one output item links back to all three customers, so .item has more than one candidate. The Edit Fields node failed with:

Multiple matches found

The docs' name for it is "Multiple matching items for expression," and they list Summarize, Aggregate and Merge as nodes that combine items. Use .first(), .last() or .all()[index], or reference a node before the combining step that holds the same data.

The node is on another branch

We sent the Webhook's output to two Code nodes, Customers (three items) and Get orders (two orders with a total), joined them with a Merge node in its default Append mode, and read {{ $('Get orders').item.json.total }} after it. The Merge output had five items: the three customers first, then the two orders. Each one links back to one input only.

This one doesn't fail. The execution succeeded, the two order items got their totals, and the three customer items got total: null. The docs confirm the behavior: the expression resolves to null, the node still succeeds, and the editor shows the error ("is on another branch") only in the expression preview. Check your outputs for unexpected nulls after a Merge.

To fix it, use Combine mode in the Merge node when each output item should hold data from both branches, or use .first() and similar when you don't need item-by-item matching.

Fix it in the Code node with pairedItem

Return pairedItem with each new item: the index of the input item that produced it. From n8n's docs, this is only needed when the node has more than one input item and you add or return new items.

The same "drop the first customer" node, with links:

return $input.all()
  .map((item, index) => ({ json: { name: item.json.name }, pairedItem: index }))
  .slice(1);

The index is taken before .slice(1), so Grace keeps index 1 and Linus index 2. With this version, {{ $('Customers').item.json.email }} returned grace@example.com and linus@example.com, and the execution succeeded:

Customers, a Rebuild Code node that sets pairedItem, and a Lookup that reads $('Customers').item

Download .json
Webhook (Webhook)WeWebhookCustomers (Code)CoCustomersRebuild (Code)CoRebuildLookup (Edit Fields)EFLookup

Nodes (4)

  • Code ×2
  • Edit Fields
  • Webhook

Credentials you'll need

None

To import: copy the JSON, open a workflow in n8n and paste onto the canvas (Ctrl+V or ⌘V). Credentials are never included; connect your own after importing.

When one output item comes from several inputs, which is the Aggregate case, there's no single index to give. Then reading earlier data with .first() or .all() is the cleaner fix.

Read linked items in code with itemMatching

In a Code node that processes all items at once (Run Once for All Items, the default mode), use itemMatching() with the index of the input item. This restored each email after Rebuild in our test:

return $input.all().map((item, i) => ({
  json: { ...item.json, email: $('Customers').itemMatching(i).json.email },
}));

It follows the same links, so it needs them to be intact, just like .item. n8n's itemMatching docs also show the Python version.

Common errors and fixes

Error or symptomCause and fix
Paired item data for item from node '<Node>' is unavailableA node (usually a Code node) returned new items without links. Add pairedItem to each returned item, or use .first()/.all().
Multiple matches foundThe item came from a node that combined several items (Aggregate, Summarize, Merge). Use .first(), .last() or .all()[index], or reference an earlier node.
An expression after a Merge returns null for some itemsThose items came from the other branch. Use Combine mode in the Merge, or .first() and similar.
.item returns the wrong recordA step changed the number or order of items in a way n8n linked differently from what you expect. Check pairedItem in the node's output, or match on an ID instead of relying on position.

Sources

  1. How n8n structures data (n8n docs)
  2. How items link through workflows (n8n docs)
  3. Item linking errors (n8n docs)
  4. Preserving linking in the Code node (n8n docs source on GitHub)
  5. Accessing linked items in the Code node (n8n docs)

then8nblog Editors

Editorial team

The then8nblog editorial team researches every article from n8n's release notes, documentation and source code, states the n8n version each guide was tested on, and has every post reviewed by a human editor before it goes live.