Pagination
TheAutoPaginatable class provides automatic pagination for list resources, allowing you to fetch all results across multiple pages with built-in rate limiting.
Installation
Class
AutoPaginatable
A generic class that wraps paginated list responses and provides automatic pagination.The type of resource being paginated (e.g., User, Organization)
Optional extended pagination parameters
The initial list response containing data and metadata
Function to fetch the next page of results
Pagination options for subsequent requests
Properties
data
Access the current page of data. Type:ResourceType[]
listMetadata
Access pagination metadata for the current page. Type:{ before?: string | null; after?: string | null }
options
The pagination options used for API calls. Type:ParametersType
object
Always returns'list' to identify the response type.
Type: 'list'
Methods
autoPagination()
Automatically paginates over the list of results, returning the complete data set. Implements automatic rate limiting (4 requests per second) to respect API rate limits. ReturnsArray of all resources across all pages
- If
options.limitwas passed to the initial request, returns only the first page (data) - Otherwise, fetches all pages and returns the complete dataset
- Automatically adds 350ms delay between requests (4rps rate limit)
- Uses
aftercursor for pagination
Types
PaginationOptions
Options for controlling pagination behavior.Maximum number of results per page
Cursor for fetching the previous page
Cursor for fetching the next page
Sort order for results
List
The list response structure.Always ‘list’
Array of resources
Usage Examples
Paginate with limit
Paginate all results
Manual pagination
Process results in batches
Rate Limiting
TheautoPagination() method automatically implements rate limiting:
- Adds 350ms delay between requests
- Achieves ~4 requests per second
- Respects WorkOS list API rate limits
- No manual throttling required