Skip to content

API Pagination

Published: in da1f965

Updated: in b1e378a

API Pagination

A list of ways to add pagination to an API.

Page based pagination

GET http://localhost:3000/items?page=2&size=3

Offset based pagination

Mapping directly to SQL clauses. OFFSET and LIMIT.

GET http://localhost:3000/items?offset=20&limit=10

The previous request would map to the following SQL query.

SELECT * from items LIMIT 10 OFFSET 20;

Offset can cause performance issues when the data grows larger. This is because the database needs to scan all the rows up to the offset value.1

Cursor based pagination

Keyset based pagination

It uses an index key like a primary key.

SELECT * from items WHERE id <= 1234 LIMIT 20;

Time based pagination

It uses timestamps as cursors.

Footnotes

  1. OFFSET is bad for skipping previous rows