What is the best way to loop on condition?

This is my workflow i am calling a sub workflow which internally does api call am passing offset and max limit to sub workflow and then accumulating data and checking for has_more and looping back again . Am not sure is this the right way to do it, or if we have a better way to do it?

1 Like

Welcome @mylearnings_abu to our community! I’m Jay and I am a n8n verified creator.

Your approach is solid and this is the standard pattern for offset-based pagination in n8n. One thing to watch: if you’re using the Accumulate Data (or Merge) node to collect pages, make sure you have “Wait for All Input Connections” enabled, otherwise the final output may not include all accumulated items. Also consider storing the offset in workflow static data ($getWorkflowStaticData('global').offset) instead of passing it as a manual field if you ever need to resume a failed run mid-pagination. For cleaner code, you can also collapse the sub-workflow call + has_more check into a single loop using a Loop Over Items node on the paginated results directly, skipping the separate Accumulate step if downstream processing can handle batches.

hi @mylearnings_abu
the http request node is much better for API pagination. does your case allow for it?
it’s simpler with HTTP Request with Pagination enabled → return all pages → continue the workflow

Yes if it was a api then i could have used it directly but i need to call a sub workflow which internally does other operations

@mylearnings_abu, you need to be careful with recursion.
according to the docs Loop Over Items (Split in Batches) | n8n Docs this is the standard Loop Over Items + Reset + IF for paginated services when you don’t know the number of pages in advance. Just make sure you have a safe stopping condition to avoid infinite loops, for example has_more === false, maximum offset or maximum page limit.

Thank you @tamy.santos for your suggestion, wht i was thinking is priorly if we know the total count we can divide them in batches and use Loop Node , but in case we dont know the total count then how can we use loop Node can you guide me?

hi @mylearnings_abu

Yes, you can use Loop Over Items even when we don’t know the total count, but the pattern changes. Instead of creating all batches upfront, you use the loop with an exit condition: call the sub-workflow, accumulate the data, check has_more, increment the offset, and only continue while has_more = true. In your case, since the next page is only known after the API/sub-workflow response, the pattern you set up makes sense. I would just add a safeguard against infinite loops, like maxPages, maxOffset, or CURRENT_PAGE < MAX_PAGES, in case the API keeps returning has_more = true by mistake. The Loop Over Items documentation shows exactly this use case with Reset + IF for dynamic pagination scenarios.