Gareth B. Davies
All articles
Build itAPIs & scraping

How to handle API pagination in your automation

Pagination fails quietly until it doesn't, and the fix is rarely the API doc, it's the loop node you build around it.

The symptom always looks the same. A workflow that ran fine on ten test records suddenly loops forever, or stops after the first page and nobody notices for a week. One builder building a Google Maps scraper for a lead list hit this early: the obvious 'next page token' field looked right, but wiring it in produced duplicate listings in the output sheet run after run. Another automation pulling CRM records started fetching the same page twice because the increment logic never actually moved forward. Pagination bugs don't crash loudly. They just quietly corrupt the data, and you find out when a client asks why there are 400 duplicate rows.

The first real step is not building the loop. It's reading the actual API response, not the documentation summary of it. Every provider names its pagination fields differently, and the field that looks obvious is often wrong. A Google Maps integration failed at first because the builder assumed the next-page token went in the URL as a query parameter, when the API actually expected page_token sent in the JSON body, matching the shape of the original request. That single misplacement produced silent duplicates instead of a clean error, which is worse.

Before writing any loop logic, pull one full response from the API in a test call and look at exactly where the continuation value lives, what it's called, and whether it's null, missing, or an empty string when there's nothing left to fetch. That distinction matters more than it should, because your exit condition depends on it.

Building the loop with an actual exit condition

Once you know the real field, build explicit branching around it: check whether a next-page value exists, and only make another API call if it does. This is where builders reach for a code node to handle the whole thing in one block, and it usually works until someone else has to touch it six months later. A loop node with clear start and end points, connected properly so each iteration keeps its own context, is easier to debug and easier to hand off. One team switched from a custom code node to a loop node specifically because the code approach was unmaintainable once the workflow grew past its original scope, and batch processing across languages and translation jobs got noticeably simpler once each iteration had a defined boundary.

The common failure point sits right here.

Infinite loops rarely come from a bad exit condition on paper. They come from a loop node with a broken connection, a missing reset on a loop variable between runs, or a filter that was supposed to stop on a 'done' status but never actually fires. One workflow looped indefinitely because a sub-process kept reprocessing records that should have been filtered out by status, fixed only by adding an explicit limit node as a backstop.

Add the backstop anyway.

Even with correct exit logic, a hard cap (ten retries, a fixed maximum page count, a timeout) is cheap insurance against the one edge case you didn't anticipate, and it turns a silent runaway into a visible failure you can actually debug.

Rate limits are the second wall you hit, usually right after pagination starts working. An API that tolerates a handful of calls during testing will throttle or reject you once a loop is firing fifty or eighty times in a row against thousands of records. Add a delay inside the loop node itself, tune the page size down if the provider lets you (moving from a default page size to something like 80 items per page is often enough to stop intermittent errors on the write side), and test the full pagination path against a realistic volume before it ever reaches a live client account. A loop that works on 20 test rows and one that works on 8,000 real records are not the same problem, and the only way to know which one you've built is to run it at scale before someone else does.

n8nAirtableGoogle Maps API

Want a partner working through this with you?

The AAA Accelerator is where AI agency builders get coached on exactly these calls, from first client to full pipeline.