A lot of times, REST APIs need to return 100+ results.
API pagination is a way to break such data into chunks that are easy to work with.
This improves user experience and reduces server load.
These are the 3 kind of API pagination you need to know: ↓
1. Offset-Based
The API uses two parameters: "offset" determines the position in the dataset, while "limit" specifies the maximum number of data to include on each page.
This approach became popular with apps using SQL, which already have LIMIT and OFFSET as part of the syntax.
Example: GET products?limit=20&offset40
Pros: It's simple to set up and lets you go straight to any page.
Cons: There are performance issues with large OFFSET values. It can cause data to be duplicated or skipped in dynamic datasets.
2. Keyset-Based
The API uses a key parameter (like since_id) to determine data limits. The first request doesn't contain the key parameter.
The other requests use the last key value from the previous set.
Example: GET /products?limit=20since_id=20
Pros: It is more efficient than page-based and keeps dynamic datasets from duplicates.
Cons: It is tied to the sort order and can't go straight to a certain page.