Use cursor pagination: request the next page and handle tokens
Cursor pagination uses a continuation token or next-page link returned by a response to request subsequent results. The client sends the cursor back unchanged, but its presence alone does not guarantee that multiple requests read a snapshot from the same point in time. That guarantee depends on the API's ordering and token contract and whether it supports snapshots.
Cursor pagination uses a continuation token or next-page link returned by a response to request subsequent results. The client sends the cursor back unchanged, but its presence alone does not guarantee that multiple requests read a snapshot from the same point in time. That guarantee depends on the API's ordering and token contract and whether it supports snapshots.
Request flow
For the first request, an API may accept a page size, filters, and sort order, then return results with a cursor or nextLink. The client follows the link or passes the token unchanged in the next request. It stops when the API-defined signal says there are no more results, such as a missing next link or hasMore: false. Follow the API contract to determine the terminal signal.
This illustrative example shows the token round trip; actual field names and response formats vary by service:
GET /api/orders?limit=2&sort=createdAt,id{
"items": [{"id": 101}, {"id": 102}],
"nextLink": "/api/orders?limit=2&sort=createdAt,id&cursor=opaque-token"
}The client uses the returned link for its next request. If the API requires the client to construct the request, do not interpret or alter the token; preserve the same filters, sorting, and API version.
Microsoft Data API builder's REST $after is one specific contract: the API returns a continuation token that marks the last record's position, and the next request sends it with $after. Its documentation says to treat the token as opaque and never construct or modify it. This product example is not a universal format.
How the cursor relates to sorting
A cursor often resumes after a previous position rather than “skipping N items” in the current result like a page number. If the last item's sort keys are (created_at, id), the server can find items after those keys. Because sort values can tie, include a unique secondary key to make the order deterministic, and keep filters and sorting consistent across pages.
Elasticsearch search_after is a product-specific method that sends the last hit's sort values in the next request. The search query and sort must remain the same. Without a unique tie-breaker, documents with equal sort values can change relative order and be missed or duplicated. To preserve a stable search state during deep traversal, Elasticsearch supports using a point in time (PIT); delete the PIT when finished. Do not treat the search_after array as a universal cursor-token format.
Treat tokens as opaque and handle them safely
The client should use the token as a value defined by the contract and should not guess or change its internal structure. A Base64 string is encoded, not encrypted. If a token contains sensitive state or internal identifiers, the server should avoid exposing them or protect them appropriately. A token does not replace authorization: recheck the user's current permissions and data scope on every request.
The server should be able to validate which filters, sort order, tenant, and API version a token belongs to. Document token expiry and responses for invalid or stale tokens as well. The client should not retry those errors indefinitely; decide whether restarting from page one is safe, considering the user flow and risk of duplicate processing. Check whether a link or token could expose sensitive information in logs, analytics events, or external sharing.
A cursor does not mean a snapshot
A cursor identifies where to continue, but it does not necessarily freeze the result set. A key-based cursor can reduce the position shifts caused by inserting an item at the beginning, as can happen with offset paging. However, a changed sort key or a new item after the cursor may still affect later pages. Deletions, filter changes, and token lifetime also affect results.
If you need the complete, exact result set from one point in time, check whether the API offers a snapshot or PIT. Elasticsearch describes preserving index state with a PIT and combining it with search_after. A frozen snapshot may be unnecessary for ordinary list navigation. Define the consistency you need, then verify what the API actually guarantees.
Client implementation checklist
Stop on the API-defined terminal signal, such as the absence of a next link.
Reuse the returned cursor or `nextLink` without changing it.
Confirm filters, sorting, and version remain consistent across pages; follow provided links as the contract specifies.
Document page size, token expiry, invalid-token errors, and restart behavior.
Check whether a retry could process already received items again; make client processing idempotent if needed.
If an extraction must represent one point in time, verify snapshot support instead of relying on the cursor alone.
Sources
[Microsoft Data API builder: Pagination with `$after` in REST](https://learn.microsoft.com/en-us/azure/data-api-builder/keywords/after-rest)
[Elasticsearch: Paginate search results](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/paginate-search-results)
[Microsoft Azure Architecture Center: RESTful Web API Implementation Best Practices](https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-implementation)