API Pagination Explained (Offset, Cursor & Page)
When an API has too much data to return at once, it paginates. Here's how offset, page and cursor pagination work โ and how to loop through every result.
When an endpoint could return thousands of items, it returns them in pages. Pagination is how you ask for the next page and how you know when you have them all. There are three common styles, and you need to read the docs to know which one an API uses.
Offset / page-number pagination
GET /items?page=1&per_page=50 GET /items?page=2&per_page=50 # or: ?limit=50&offset=100
- Simple; lets you jump to any page and show ‘page 7 of 40’.
- Gets slow on large offsets (the database still scans the skipped rows).
- Unstable if items are added/removed while you page — you can see duplicates or miss rows.
- Total count often in a
X-Total-Countheader or atotal/pageInfofield.
Cursor / token pagination
GET /items?limit=50
# response: { "data": [...], "next_cursor": "eyJpZCI6MTk5fQ" }
GET /items?limit=50&cursor=eyJpZCI6MTk5fQ
# keep going until next_cursor is null / absent- The cursor is an opaque pointer to ‘where you left off’ — do not parse or construct it.
- Stable and fast even on huge datasets; the standard for modern APIs (Stripe, Twitter/X, GitHub GraphQL, balldontlie).
- You cannot jump to an arbitrary page — only forward (and sometimes back).
- Stop when the response has no next cursor.
Link-header pagination
GET /items?page=1
# response header:
Link: <https://api.example.com/items?page=2>; rel="next",
<https://api.example.com/items?page=88>; rel="last"Used by the GitHub REST API and others. Follow the rel="next" URL verbatim until there isn’t one.
A safe generic loop
import requests
url, params = "https://api.example.com/items", {"limit": 100}
items = []
while url:
r = requests.get(url, params=params, timeout=10)
r.raise_for_status()
body = r.json()
items.extend(body["data"])
# cursor style:
cur = body.get("next_cursor")
params = {"limit": 100, "cursor": cur} if cur else {}
url = url if cur else None
# (for Link-header style, set url = r.links.get('next', {}).get('url'))Practical advice
- Request the largest allowed page size to minimise round trips — but note the max (often 100).
- Respect the rate limit while looping — add a small delay, and watch for 429.
- Set a cap on total pages/items so a bug can’t pull a million rows.
- Filter server-side (
?status=active&since=...) instead of fetching everything and filtering in code. - For GraphQL, pagination is usually cursor-based via
first/afterand apageInfo { hasNextPage endCursor }object.
FAQ
What is the difference between offset and cursor pagination?
Offset (?page=3 / ?offset=100) lets you jump anywhere but slows down and can skip/duplicate rows on changing data. Cursor (?cursor=abc) is forward-only but stays fast and consistent at any scale.
How do I know when I’ve reached the last page?
Cursor style: the next cursor is null or missing. Offset style: you get fewer items than the page size, or the page number exceeds the reported total. Link style: there is no rel="next".
Should I build the cursor value myself?
No — treat it as opaque. It encodes internal state (an id, a sort key, a timestamp) that the API controls. Just pass back exactly what you received.
Why is fetching page 500 so slow?
Offset pagination makes the database walk past all 499 previous pages of rows every time. APIs that expect deep paging use cursors instead; if you must use offsets, filter by a date or id range to narrow the set first.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ