Losing original value on loop

I have a workflow that needs to loop the request, as there’s a limit to how many records can be returned per request.

I have the looping working perfectly if I hardcode the request ID into the API call, however when I try and use a variable for it (as it’s going to have to do this for multiple different IDs) it loses the ID after the first loop.

Below is the flow in the simplest form.

How do I fix this to retain the ID?

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Try using {{ $('Set ID').item.json.id }} to reference the ID instead. That references the json from the specific node instead of the json from the previous node.

But instead of that you might want to use the pagination feature built into the http node. It will simplify this and make it a bit more robust… The one http node using pagination will replace all of the nodes after you http node

See the setup of the node below

In the query parameter section i added the name of “offset” like you had and put the value at {{ $pageCount * offset }}. $pageCount in the pagination hold the index of the current request. The first request is 0, so the first offset will be 0 then will increase from there.

Then in the “Complete Expression” section you need to tell it when to stop making new requests. This would be the same logic you use in that if node. This expression needs to be evaluated to true or false. I added {{ $response.body.Total_Number_Leads > $json.offset + 100 }} since that’s what you had in the if node. The $response references the response from the latest call.

Let me know if that makes sense and if you have follow up questions

1 Like