Pagination
Efficiently retrieve large sets of data
Posta uses cursor-based pagination for endpoints that return lists of objects. This guide explains how to work with paginated responses and retrieve large datasets efficiently.
How pagination works
When you make a request to an endpoint that returns multiple objects, you can control the number of items returned and navigate through pages using two parameters:
pageSize
: The number of items to return per page.nextToken
: A cursor for the next page of results.
- Treat
nextToken
as an opaque identifier that’s only used to retrieve the next items in a list and not for other programmatic purposes. nextToken
should not be persisted as it’s only valid for the given pagination.
Using pagination
Initial request
For your first request, you typically only need to specify the pageSize
:
Subsequent requests
The response will include a nextToken
if there are more results available. Use this token in your next request to retrieve the next page:
Continue using the nextToken
from each response until you receive a response without a nextToken
, indicating you’ve reached the end of the results.
Response format
A paginated response will have the following structure:
Best practices
- Consistent page size: Keep the
pageSize
consistent between requests for predictable behavior. - Handle missing tokens: If a response doesn’t include a
nextToken
, you’ve reached the end of the results. - Error handling: Implement proper error handling for cases where a
nextToken
might become invalid. - Iterate efficiently: Use a loop or recursive function to automatically fetch all pages when needed.
Limitations
- The maximum
pageSize
is 100 items.
Setting a very large pageSize
may impact API performance and response times. We recommend using a moderate page size and iterating through results as needed.