Rectangle 27 134

I'm not completely sure how your data is handled, so this may or may not work, but have you considered paginating with a timestamp field?

When you query /foos you get 100 results. Your API should then return something like this (assuming JSON, but if it needs XML the same principles can be followed):

{
    "data" : [
        {  data item 1 with all relevant fields    },
        {  data item 2   },
        ...
        {  data item 100 }
    ],
    "paging":  {
        "previous":  "http://api.example.com/foo?since=TIMESTAMP1" 
        "next":  "http://api.example.com/foo?since=TIMESTAMP2"
    }

}

Just a note, only using one timestamp relies on an implicit 'limit' in your results. You may want to add an explicit limit or also use an until property.

The timestamp can be dynamically determined using the last data item in the list. This seems to be more or less how Facebook paginates in its Graph API (scroll down to the bottom to see the pagination links in the format I gave above).

One problem may be if you add a data item, but based on your description it sounds like they would be added to the end (if not, let me know and I'll see if I can improve on this).

Timestamps are not guaranteed to be unique. That is, multiple resources can be created with the same timestamp. So this approach has the downside that the next page, might repeat the last (few?) entries from the current page.

@jandjorgensen From your link: "The timestamp data type is just an incrementing number and does not preserve a date or a time. ... In SQL server 2008 and later, the timestamp type has been renamed to rowversion, presumably to better reflect its purpose and value." So there's no evidence here that timestamps (those that actually contain a time value) are unique.

@jandjorgensen I like your proposal, but wouldn't you need some kind of information in the resource links, so we know if we go previous or next? Sth like: "previous": "api.example.com/foo?before=TIMESTAMP "next": "api.example.com/foo?since=TIMESTAMP2 We would also use our sequence ids instead of a timestamp. Do you see any problems with that?

Another similar option is to use the Link header field specified in RFC 5988 (section 5) : tools.ietf.org/html/rfc5988#page-6

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

rest - API pagination best practices - Stack Overflow

rest pagination api-design