What is pagination in programming

Pagination is a term from a polygraph and books printing that means dividing content into discrete pages or sections.

In case we have not enough room on a page for displaying all data, the good way is to split it into small subpages.

Actually, in the programming world, it means exactly the same. Let's separate this theme on two main subjects. UI and Backend(API) pagination. We will cover only second part in this article.


Pagination in REST API

There are two main approaches to implement pagination on API. Both of them used on different types of application. So you need to know it even in case you are front-end/client developer.

URL parametres

One of the most popular and simplest way. It is mean that you have additional URL params, like 'page' and 'limit'.

Quick example: in case we have URL like '&limit=10&page=2' - we will get items starting from 20 till 30.

Note: page starting from zero.

All routes with pagination have a specific structure for making working with pagination more easier.

{  data: [...],  limit: 10, // how many items per page you want to get.  total: 100, /// how many items we have in total, can be fetched  page: 2 // mean wich page do you want ot fetch. }

This approach we are using for our DUMMYAPI.IO. You can try to play around pagination on the API Explorer page.

Try to use a link below with parameters from example.
https://dummyapi.io/explorer#post?limit=10&page=2

Cursor pagination

In this case, you do not need to set parameters in URL. Doing the first request you will get your first page data with a link to the second page of data. Typically you will get the same URL but with a hash on the end.

Example of response from Facebook API:

{  "data": [...],  "paging": {   "cursors": {    "before": "{hash}",    "after": "{hash}"   }  },  "summary": {   "total_count": 100  } }