API Pagination, Filtering, and Sorting
Pagination, filtering, and sorting enable APIs to deliver large datasets efficiently, consistently, and in a user-friendly way.
Overview
Pagination limits the number of records an API returns in a single request, preventing bloated responses and reducing load on both server and client. Filtering lets you narrow results by specific criteria without requiring separate endpoints. Sorting controls the order of results. Together, these three mechanisms form the foundation for flexible API queries. The most common pagination strategies are offset pagination, cursor pagination, and keyset pagination—each with tradeoffs in simplicity, consistency, and performance. Filters and sorting are typically controlled via query parameters, with clear conventions and descriptive names being essential. A well-designed API documents its pagination and filtering options clearly and includes helpful metadata in responses such as links, totals, or cursors.
Key Components
Offset Pagination
Offset pagination uses parameters like page or offset and limit to retrieve a slice of results. For example: ?page=2&limit=20. This approach is straightforward to implement and understand. The downside is performance degradation with large offset values and data inconsistency when the result set changes between requests.
Cursor Pagination
Cursor pagination uses an opaque cursor based on a sorted value. For example: ?cursor=abc123&limit=20. It performs better and stays more consistent than offset pagination across large datasets. The trade-off is that jumping to arbitrary pages becomes harder, and it depends on a stable sort order.
Keyset Pagination
Keyset pagination resembles cursor pagination but uses explicit values like ?createdAfter=2026-07-01T00:00:00Z&limit=20. It’s highly performant and stable but less flexible when multiple sort fields are needed. It works particularly well for time-ordered data streams.
Page and Limit
page and limit are the classic parameters for offset pagination. page specifies which page to retrieve, and limit sets how many items appear per page. Typical defaults are page=1 and limit=20. APIs should enforce a maximum limit to prevent abuse.
Total Count and Links
Responses should include metadata that aids navigation. This includes total, page, limit, next, prev, first, and last. HATEOAS links help clients navigate pages without manually constructing URLs.
Filter Parameters
Filters are usually controlled via query parameters. Examples include ?status=active, ?category=books, or ?minPrice=10&maxPrice=50. Filters should be meaningfully named, documented, and validated. Boolean combinations can be expressed through separate parameters or a dedicated filter query syntax.
Sorting
Sorting is often controlled through parameters like sort or orderBy. Examples are ?sort=name or ?sort=-createdAt, where a leading minus indicates descending order. Clear conventions and documentation are vital so clients use the API correctly.
Search Parameters
Full-text search is typically expressed through a parameter like q or search, for example ?q=python. More complex searches are often implemented using search services like Elasticsearch or OpenSearch and exposed through a dedicated search API.
Combining Filters, Sorting, and Pagination
In practice, filters, sorting, and pagination are often combined. Example: ?status=active&sort=-createdAt&page=1&limit=25. The API must combine, validate, and document these parameters meaningfully. Keeping the order of operations consistent is crucial.
Limit and Defaults
A sensible default limit reduces data volume and response time. At the same time, enforce a maximum limit to prevent oversized requests. Clients should be able to choose the limit within the allowed boundaries.
Consistency with Growing Data
When the dataset changes during pagination, records can appear twice or be skipped. Cursor and keyset pagination are more stable than offset pagination in this scenario. With offset pagination, clients should expect inconsistencies, or the API should use snapshots.
Practical Example
A product API supports offset pagination, filtering, and sorting.
Request:
GET /api/v1/products?category=electronics&minPrice=100&sort=-rating&page=2&limit=10
Response:
{
"data": [
{ "id": 15, "name": "Laptop", "price": 999, "rating": 4.8 },
{ "id": 22, "name": "Monitor", "price": 299, "rating": 4.7 }
],
"pagination": {
"page": 2,
"limit": 10,
"total": 145,
"pages": 15,
"next": "/api/v1/products?category=electronics&minPrice=100&sort=-rating&page=3&limit=10",
"prev": "/api/v1/products?category=electronics&minPrice=100&sort=-rating&page=1&limit=10"
}
}
The client can navigate using the links without reconstructing the filters and sort order.
FAQ: Pagination, Filtering, and Sorting
1. What is pagination in APIs?
2. What is offset pagination?
page and limit to retrieve a slice of results. It’s easy to implement but becomes slow and inconsistent with large datasets.3. What is cursor pagination?
4. What is keyset pagination?
createdAfter or lastId to load the next page. It’s highly performant and suits time-ordered data streams especially well.5. What is a default limit?
6. What are HATEOAS links in pagination?
7. What is a filter parameter?
?status=active or ?category=books.8. What is a sort parameter?
?sort=name or ?sort=-createdAt for descending order. Clear conventions are important.9. What is a search parameter?
q or search enables full-text search via the API. Complex searches are often implemented using dedicated search services like Elasticsearch or OpenSearch.10. Why is pagination important for performance?
11. What is a maximum limit?
12. What is inconsistency in pagination?
13. What is the total count in paginated responses?
14. How do you document filters and sorting?
15. What are best practices for pagination, filtering, and sorting?
References
- https://www.rfc-editor.org/rfc/rfc8288
- https://www.martinfowler.com/articles/patterns-of-distributed-systems/pagination.html
- https://use-the-index-luke.com/no-offset
Book Recommendations for API Design
If you’d like to dive deeper into API design, data querying, and software architecture, we recommend these books:
API Development
Books about API design, REST, GraphQL, OpenAPI and API architecture
Designing Data-Intensive Applications von Martin Kleppmann
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
API Design Patterns von JJ Geewax
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.




