Nov 27, 2025

How to handle pagination in REST APIs?

Leave a message

Handling pagination in REST APIs is a crucial aspect of API design, especially for an API provider like us. Pagination allows us to manage large datasets effectively, improving performance and user experience. In this blog, we'll explore various techniques for handling pagination in REST APIs, discuss their pros and cons, and provide practical examples.

Why Pagination is Necessary

When dealing with large datasets, returning all the data in a single response can be inefficient and resource - intensive. It can lead to long response times, high memory usage on both the server and the client side, and potential network congestion. Pagination solves these problems by dividing the dataset into smaller, more manageable "pages" and returning one page at a time.

Common Pagination Techniques

Offset - Limit Pagination

Offset - limit pagination is one of the most straightforward and commonly used techniques. It involves specifying two parameters: offset and limit. The offset indicates the starting position of the data to retrieve, and the limit specifies the maximum number of items to return in the response.

For example, if we have an API endpoint /api/products that returns a list of products, a request with offset = 20 and limit = 10 would return the 21st to the 30th products.

The following is a Python Flask example of implementing offset - limit pagination:

from flask import Flask, request, jsonify

app = Flask(__name__)

products = [{"id": i, "name": f"Product {i}"} for i in range(100)]

@app.route('/api/products', methods=['GET'])
def get_products():
    offset = int(request.args.get('offset', 0))
    limit = int(request.args.get('limit', 10))
    paginated_products = products[offset:offset + limit]
    return jsonify(paginated_products)

if __name__ == '__main__':
    app.run(debug=True)

Pros:

  • Easy to understand and implement.
  • Works well with databases that support LIMIT and OFFSET clauses, such as MySQL and PostgreSQL.

Cons:

  • As the offset value increases, the performance can degrade significantly, especially for large datasets. This is because the database has to skip over a large number of rows to reach the desired offset.
  • It can be difficult to handle data that is updated during pagination, as the offset may no longer point to the correct position.

Cursor - Based Pagination

Cursor - based pagination uses a unique identifier (the cursor) to mark the position in the dataset. Instead of specifying an offset, the client provides the cursor value in the request to get the next page of data.

For example, if we have a list of users sorted by their creation date, the cursor could be the creation date of the last user in the previous page. The server would then return the next set of users with a creation date greater than the cursor value.

Here is a simple JavaScript example using Node.js and Express:

const express = require('express');
const app = express();

const users = [
    { id: 1, name: 'User 1', created_at: '2023-01-01' },
    { id: 2, name: 'User 2', created_at: '2023-01-02' },
    // more users...
];

app.get('/api/users', (req, res) => {
    const cursor = req.query.cursor;
    const limit = parseInt(req.query.limit) || 10;
    let startIndex = 0;
    if (cursor) {
        startIndex = users.findIndex(user => user.created_at > cursor);
    }
    const paginatedUsers = users.slice(startIndex, startIndex + limit);
    const nextCursor = paginatedUsers.length > 0 ? paginatedUsers[paginatedUsers.length - 1].created_at : null;
    res.json({
        users: paginatedUsers,
        next_cursor: nextCursor
    });
});

app.listen(3000, () => {
    console.log('Server running on port 3000');
});

Pros:

  • Better performance compared to offset - limit pagination, especially for large datasets. It doesn't require the database to skip over a large number of rows.
  • More robust when dealing with data updates, as the cursor always points to a specific position in the dataset.

Cons:

  • More complex to implement, especially when dealing with multiple sorting criteria.
  • Requires a unique and sortable identifier to use as the cursor.

Page Number Pagination

Page number pagination is similar to offset - limit pagination, but instead of specifying an offset, the client requests a specific page number. The server calculates the offset based on the page number and the limit.

For example, if the limit is 10 items per page, a request for page 3 would have an offset of 20 (since offset = (page_number - 1) * limit).

from flask import Flask, request, jsonify

app = Flask(__name__)

products = [{"id": i, "name": f"Product {i}"} for i in range(100)]

@app.route('/api/products', methods=['GET'])
def get_products():
    page = int(request.args.get('page', 1))
    limit = int(request.args.get('limit', 10))
    offset = (page - 1) * limit
    paginated_products = products[offset:offset + limit]
    return jsonify(paginated_products)

if __name__ == '__main__':
    app.run(debug=True)

Pros:

  • Easy for users to understand, as it is similar to traditional pagination in web pages.
  • Simple to implement.

Cons:

  • Similar to offset - limit pagination, it can suffer from performance issues as the page number increases.
  • It can be difficult to handle data updates during pagination.

Considerations for API Providers

As an API provider, we need to consider several factors when implementing pagination:

Standardization

We should follow industry - standard practices for pagination. This includes using consistent parameter names (such as offset, limit, page, cursor), and providing clear documentation on how pagination works.

PyrazinamideMethocarbamol

Metadata

In addition to the paginated data, we should provide metadata in the response, such as the total number of items, the total number of pages, and the next and previous page links. This helps the client to better understand the dataset and navigate through the pages.

For example, the response could be in the following format:

{
    "data": [
        { "id": 1, "name": "Product 1" },
        { "id": 2, "name": "Product 2" }
    ],
    "total_items": 100,
    "total_pages": 10,
    "next_page": "/api/products?offset=20&limit=10",
    "prev_page": null
}

Security

We need to ensure that the pagination parameters are properly validated to prevent SQL injection or other security vulnerabilities. For example, we should convert the offset and limit parameters to integers and set reasonable limits to prevent excessive resource consumption.

Conclusion

Handling pagination in REST APIs is an important skill for API providers. By choosing the right pagination technique based on the dataset size, performance requirements, and user experience, we can provide a more efficient and user - friendly API. Whether you are dealing with a small dataset or a large - scale application, understanding the pros and cons of different pagination methods is essential.

If you are interested in our APIs or have any questions about pagination or other API - related topics, feel free to contact us for procurement and further discussions. We also offer a wide range of products such as Methocarbamol, Azasetron Hydrochloride, and Pyrazinamide.

References

  • Richardson, Leonard, and Sam Ruby. RESTful Web Services. O'Reilly Media, 2007.
  • Banks, Adam. Building RESTful Web APIs with Python and Flask. Packt Publishing, 2018.
Send Inquiry