API Directory โ€บ Guides โ€บ API Pagination Explained (Offset, Cursor & Page)

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

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

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

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&rsquo;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

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’