Pagination is a common technique used in APIs to retrieve large amounts of data in smaller, more manageable chunks. Pagination allows clients to retrieve data in batches, which can help to reduce the load on the server and improve the performance of the API.
API designers should provide clear and consistent documentation for pagination parameters, such as the number of items to retrieve per page and the page number to retrieve. Additionally, designers should consider including metadata in the response to indicate the total number of items and the total number of pages, which can help clients to navigate through the data more easily.
It is also important to consider security implications when implementing pagination in APIs. For example, clients should not be able to access data that they are not authorized to view, and pagination parameters should be validated to prevent injection attacks and other security vulnerabilities.
In order to get meaningful results the developer must enforce ordering (for example, by identifier).
Offset-based pagination
In offset-based pagination, the client specifies the page number and the number of items per page (the "limit"). The server then returns that specific subset of the data. This is the most straightforward form of pagination and is easy to implement. However, it can be inefficient for large datasets because the database has to count off the "offset" rows, and it can also lead to inconsistent results if items are added or removed while the client is paginating through the data.
Request Parameters
Parameter Name | Type | Description |
---|---|---|
page | integer | (Optional, default is 1) The requested page number |
per_page | integer | (Optional, default is 10) The page size |
Response Schema
Property Name | Type | Description |
---|---|---|
data | array | List of objects (results) |
current_page | integer | The current page number |
last_page | integer | The last page number (with the given per_page) |
per_page | integer | The page size |
total | integer | Total number of items |
{
"data": [
{}, {}, {}, {}, {}
],
"current_page": 1,
"last_page": 2,
"per_page": 5,
"total": 6
}