Today I Learned - Rocky Kev

TIL different ways to paginate API calls

POSTED ON:

TAGS:

I think about REST APIs a lot, both from a consumer-level (I am consuming the API data!) and a architecture-level.

Imagine if you were making something like Twitter, where you make a API call for a /[user_name]/tweets. What would it send back?

If your answer is "Well, all of the user's tweets", great!

We're halfway there.

What if the user is addicted to your service, and generates 1,000 tweets per day for a year straight? Will your API respond with 365,000 tweets? What if they want that data, but by date order? How about by most reactions? How about tweets from when they were in Chicago between the summer.

Sure, you can create filters and queries to output a specific amount. But that assumes the consumer knows what to look for.

When exposing large data sets through APIs, it needs to provide a mechanism to paginate the list of resources.

That way, instead of 365,000 tweets shared... maybe you're sharing segments of 1,000 tweets. Smaller payload, faster delivery, less angry person.

Welcome to thinking about Rest APIS!

TIL - the various Paginating requests in API, via
Ignacio Chiazzo
!

A major rule of creating endpoints: Exposing endpoints is very easy. Deprecating and deleting them is extremely hard and potentially impossible.

Things to consider:

Pagination

The most common pagination techniques are Page-based pagination (also called offset-based pagination), KeySet-Based pagination, and Cursor-based Pagination.

Page — based pagination

tl;dr - Divid the content into pages.

Pros:

Cons:

KeySet-based pagination

tl;dr: use a key param. Examples: since_id, since_updated_at, since_created_at.

Pros:

Cons:

Cursor-based pagination

tl;dr: A cursor will be a piece of data that contains a pointer to an element and the info to get the next/previous elements. The server should return the cursor pointing to the next page in each request. In most cases, the cursor is opaque, so users cannot manipulate it.

Clients should not store the cursor on their side. Google API Documentation suggests adding an expiration date to the token and expiring cursors sent in requests.

Pros:

Cons:

That's three different ways to paginate your endpoints!

I highly recommend reading the post Paginating requests in API, via Ignacio Chiazzo, to see how other major companies are imlpementing it!


Related TILs

Tagged:

TIL different ways to paginate API calls

When exposing large data sets through APIs, it needs to provide a mechanism to paginate the list of resources. That way, instead of 365,000 tweets shared... maybe you're sharing segments of 1,000 tweets. Smaller payload, faster delivery, less angry person.

TIL different ways to paginate API calls

When exposing large data sets through APIs, it needs to provide a mechanism to paginate the list of resources. That way, instead of 365,000 tweets shared... maybe you're sharing segments of 1,000 tweets. Smaller payload, faster delivery, less angry person.

TIL different ways to paginate API calls

When exposing large data sets through APIs, it needs to provide a mechanism to paginate the list of resources. That way, instead of 365,000 tweets shared... maybe you're sharing segments of 1,000 tweets. Smaller payload, faster delivery, less angry person.