Skip to content
IRC-CodingIRC-Coding
PaginationFilteringSortingOffset PaginationCursor PaginationKeyset PaginationQuery Parameters

API Pagination, Filtering & Sorting: Best Practices

Master API pagination, filtering and sorting. Learn offset, cursor, keyset pagination, query parameters and design best practices.

S

schutzgeist

5 min read
API Pagination, Filtering & Sorting: Best Practices

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.

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?

Pagination limits the number of records an API returns per request. It prevents large, slow responses and reduces strain on both server and client.

2. What is offset pagination?

Offset pagination uses parameters like 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?

Cursor pagination uses an opaque cursor to load the next page. It performs better and stays more stable than offset pagination, but jumping to arbitrary pages is less flexible.

4. What is keyset pagination?

Keyset pagination uses explicit values like 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?

A default limit is the standard number of items per page when a client doesn’t specify one. Typical values are 20 or 50.

6. What are HATEOAS links in pagination?

HATEOAS links in pagination provide URLs for the next, previous, first, and last pages. They enable clients to navigate without manually constructing URLs.

7. What is a filter parameter?

A filter parameter is a query parameter that narrows results by a specific criterion, such as ?status=active or ?category=books.

8. What is a sort parameter?

A sort parameter controls the order of results, such as ?sort=name or ?sort=-createdAt for descending order. Clear conventions are important.

9. What is a search parameter?

A search parameter like 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?

Pagination reduces data per request, lowers latency and memory usage, and eases the load on databases and networks. Without it, large queries can overwhelm both servers and clients.

11. What is a maximum limit?

A maximum limit is the upper bound on how many items per page a client can request. It prevents oversized requests from overloading the API.

12. What is inconsistency in pagination?

Inconsistency occurs when the dataset changes between page requests. Records can appear twice or be skipped. Cursor pagination is less prone to this than offset pagination.

13. What is the total count in paginated responses?

The total count is the total number of records matching the filter criteria. It helps clients understand the full scope of results and calculate the total number of pages.

14. How do you document filters and sorting?

Filters and sorting should be clearly described in OpenAPI or your API documentation. Include allowed parameters, data types, defaults, allowed values, and examples.

15. What are best practices for pagination, filtering, and sorting?

Best practices include choosing the right pagination strategy, setting sensible default and maximum limits, using descriptive parameter names, maintaining stable sort criteria, providing useful navigation links, documenting clearly, and validating all parameters carefully.

References

  1. https://www.rfc-editor.org/rfc/rfc8288
  2. https://www.martinfowler.com/articles/patterns-of-distributed-systems/pagination.html
  3. 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

Designing Data-Intensive Applications von Martin Kleppmann

Bei Amazon ansehen

Affiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.

API Design Patterns von JJ Geewax

API Design Patterns von JJ Geewax

Bei Amazon ansehen

Affiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.

Back to Blog
Share:

Related Posts