Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should paging be zero indexed within an API?

When implementing a Rest API, with parameters for paging, should paging be zero indexed or start at 1. The parameters would be Page, and PageSize.

For me, it makes sense to start at 1, since we are talking about pages

like image 463
The Applicationist Avatar asked Feb 23 '16 23:02

The Applicationist


People also ask

Should API have pagination?

API pagination is essential if you're dealing with a lot of data and endpoints. Pagination automatically implies adding order to the query result. The object ID is the default result, but results can be ordered in other ways as well.

What is paging in API?

Some APIs, such as Contacts can return millions of results. We obviously can't return all of them at once, so we need to return a subset - or a page - at a time. This technique is called paging and is common to most APIs. Paging can be implemented in many different ways, some better than others.

What is REST API pagination?

You can paginate the JSON response that is called from the REST API. The order of the data is retained from page to page. Given the ability to paginate, you can quickly populate tables and make new REST calls every time you go to the next page of the data on the table.

What is page offset in API?

Offset is the position in the dataset of a particular record. By specifying offset , you retrieve a subset of records starting with the offset value. Offset normally works with length , which determines how many records to retrieve starting from the offset.


1 Answers

There's no standard for it. Just have a look around: there are hundreds of thousands of APIs using different approaches.

Most of APIs I know use one of the following approaches for pagination:

  • offset and limit or
  • page and size

Both can be 0 or 1 indexed. Which is better? That's up to you.

Just pick the one that fits your needs and document it properly.


Additionally, you could provide some links in the response payload to make the navigation easier between the pages.

Consider, for example, you are reading data from page 2. So, provide a link for the previous page (page 1) and for the next page (page 3):

{     "data": [         ...     ],     "paging": {         "previous": "http://api.example.com/foo?page=1&size=10",          "next": "http://api.example.com/foo?page=3&size=10"      } } 

And remember, always make an API you would love to use.

like image 125
cassiomolin Avatar answered Sep 28 '22 05:09

cassiomolin