Why cursors and not page numbers
Paginated lists are ordered newest first, and new records arrive at the front while you are reading. With page numbers, a record created between two requests pushes everything down by one, so page 2 repeats the last record of page 1. A cursor points at a record rather than at a count, so new arrivals don’t move it. Each request picks up exactly where the last one stopped. The same property makes a cursor useful long after you finish reading. Because it marks a fixed position, you can keep it and later ask for everything that has arrived on the newer side of it.Two directions from one position
Each page comes with a cursor for each of its ends:end_cursormarks the oldest record on the page. Passing it asaftermoves towards older records.start_cursormarks the newest record on the page. Passing it asbeforemoves towards newer records.
after is how you read a list for the first time. before is how you come back for what is new.
Every paginated endpoint supports after; only some support before.
The names follow the order of the list, not the order of time. The list runs newest first, so the
records that come after a position in the list are older, and the records before it are newer.
A worked example
Take a list with five records,A (oldest) to E (newest), on an endpoint that supports before.
To keep the example short, say it returns two records per page.
Reading everything
The first response also has
start_cursor = position E, the newest record that exists at that
moment. That is the cursor worth keeping.
Coming back for what is new
Later, three more records are created:F, G and H (newest).
Two things here are easy to get wrong:
- The first page is the one next to your cursor, not the newest one.
before=EreturnsFandG, notGandH. If it returned the newest page, you would have no way to reachFwithout knowing how much had built up. Starting next to the cursor means you can walk forward a page at a time and never skip a record. - Inside a page, records are still newest first.
Gis listed beforeF. The pages move forward in time; the order within each page doesn’t change.
What an empty page means
start_cursor and end_cursor describe the page you were just given. An empty page has no records
to point at, so both are null.
That is why the last row of the example returns null rather than repeating H. Nothing is new, so
there is no newer position to report, and the cursor you sent is still the right one. Keep it and
send it again next time.
What cursors don’t tell you
A cursor orders records by when they were created, which shapes what paging can and can’t show you:- Changes to existing records. A record you have already read doesn’t come back when it changes.
- Deletions. A deleted record just stops appearing.
- Records that become visible late. A record gets its creation time a moment before it becomes readable. Very occasionally, a newer record becomes readable first, and a reader that has already moved its cursor past that point won’t see the older one when it appears.
Related
- Pagination for parameters, page sizes and errors.