Working with paginated list responses in the FlexPrice API
Many FlexPrice API endpoints that return lists of resources support pagination to manage large result sets efficiently. This guide explains how to work with paginated responses.
# Get first page (items 1-20)curl -X GET "http://localhost:8080/v1/customers?limit=20&offset=0" \ -H "X-API-Key: your-api-key"# Get second page (items 21-40)curl -X GET "http://localhost:8080/v1/customers?limit=20&offset=20" \ -H "X-API-Key: your-api-key"# Get third page (items 41-60)curl -X GET "http://localhost:8080/v1/customers?limit=20&offset=40" \ -H "X-API-Key: your-api-key"
Balance between reducing API calls and response size. For most use cases, 20-100 items per page is optimal.
# Too small - many API callscurl "http://localhost:8080/v1/customers?limit=5"# Good balancecurl "http://localhost:8080/v1/customers?limit=50"# Might be too largecurl "http://localhost:8080/v1/customers?limit=500"
2
Handle empty results
Check if the items array is empty to handle pages with no results.
if (response.items.length === 0) { console.log('No items found');}
3
Cache total counts
The total count doesn’t change frequently. Cache it to avoid unnecessary requests.
// Fetch first page to get totalconst firstPage = await client.customers.list({ limit: 50, offset: 0 });const totalPages = Math.ceil(firstPage.pagination.total / 50);// Use cached totalPages for pagination UI
4
Consider cursor pagination for real-time data
For frequently changing data (like event streams), consider using filters or time-based queries instead of offset pagination.
# Query events by time window instead of paginationcurl -X POST http://localhost:8080/v1/events/query \ -d '{ "filters": { "timestamp_gte": "2024-03-15T00:00:00Z", "timestamp_lt": "2024-03-16T00:00:00Z" } }'
5
Handle pagination edge cases
Account for scenarios like deleted items or filtered views.
// When filtering, total reflects filtered countconst response = await client.customers.list({ limit: 20, offset: 0, filters: { status: 'active' }});// pagination.total is count of active customers onlyconsole.log(`${response.pagination.total} active customers`);
Deep pagination can be slow. Fetching results at very high offsets (e.g., offset=10000) takes longer because the database must scan and skip many rows.For better performance with large datasets:
Use filters to narrow results before paginating
Cache results when possible
Consider time-based queries for event data
Export to files for bulk operations (use the Tasks API)