Should paging be zero indexed within an API?

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.

Leave a Comment