Table of contents

Just like how humans divide up written content into pages, software APIs use pagination to digest large quantities of data.
But 3rd-party APIs often use varying forms of pagination depending on when they were produced or the type of data that’s being returned. accommodating to these various approaches can, in and of itself, complicate your integration builds quickly.
We’ll explore this issue further and break down a solution for overcoming this challenge. But first, let’s explore what API pagination means and the different types of API pagination you might come across.
It's the process of dividing a large dataset into smaller, discrete parts or 'pages' to make accessing and managing large volumes of data more efficient. This approach is particularly useful for improving the user experience in browsing large lists or datasets, as it reduces load times and data overload.
For example, you may want to GET data from 10,000 employee records. But receiving such a high volume of data at once can cause performance issues, such as lengthy data loads. Pagination makes it so that those records aren’t returned all at once; instead, records are batched in ‘pages’ that you can access programmatically.
Related: What is multiple API integration?
Let's take a closer look at the different versions of pagination.
Cursor pagination is like reading a book with a bookmark. It allows the client to request data in a sequence and uses a 'cursor' to keep track of its position. This cursor is often a unique identifier (like an ID or timestamp) of the last item from the previous request.
This approach is incredibly efficient for real-time data and large datasets because it reduces the load on the server and provides a consistent user experience.
This code helps show a large list of items in smaller, numbered sections, and keeps track of where you are in the list.
Offset pagination involves specifying a starting point (the offset) and the number of records to retrieve (the limit). For example, offset=10&limit=10 would skip the first 10 records and return the next 10.
This code allows a user to request specific "pages" of data from a list, each containing a set number of items. The page and limit help in dividing the entire list into smaller, more manageable sections.
1. Getting page and limit: The code looks for two pieces of information from the user—which page of items they want to see (page) and how many items should be on each page (limit).
2. Calculating the offset: It calculates the offset, which is basically the number of items to skip before starting to show items on the requested page. This is done by multiplying the page number by the limit and subtracting the limit.
3. Fetching the data: The code then gets the items from the database, starting from the calculated offset and fetching up to the limit number of items.
4. Sending the items back: Lastly, it sends back the fetched items in a format that's easy to use (like a list of item details).
Related: The top challenges of integrating software
Keyset pagination, sometimes called 'seek' pagination, is a bit like using a ruler to keep your place in a list. It involves selecting records where a certain field (the keyset) is greater or less than the value from the last retrieved row. This method is efficient for large datasets and works well when sorting by a unique, sequential field like a timestamp or an auto-incrementing ID.
This code helps show a big list in smaller parts, one chunk at a time, and remembers where it stopped last so it can continue from there next time.
1. Starting point: The code looks for a last_id which tells it where it left off last time. If there's no last_id, it starts from the beginning.
2. How many to show: It also checks how many items to display at once (limit), which is set to 10 by default.
3. Getting the items: The code then gets the items from the list, starting after the last_id (if there is one), and only takes as many as the limit.
4. Sending the list back: Finally, it sends this list of items back to whoever asked for it.
Seek pagination is akin to using a combination of bookmarks and a table of contents in a book. It typically involves using a unique field (or a combination of fields) to directly seek a position in the dataset, rather than incrementally navigating through it. This method is particularly useful when dealing with very large datasets and when the client knows exactly where to jump in the dataset.
This code helps users either get a bunch of items from a list, limited to a certain number, or start from a specific item and get a limited number of items from there.
1. What it does: This code sets up a way for users to get a list of items from a database, but with a limit on how many items they can see at once.
2. Starting point (seek_id): Users can provide a seek_id, which is like asking, "Can I see the item that has this specific ID?" If they provide this ID, the code will look for items starting from that specific item.
3. Limiting the number of items: There's a limit on how many items the user can get at once, which is set to 10 unless the user specifies a different number.
4. Fetching and sending the items: The code then gets the items according to these instructions (starting at seek_id if given, and up to the limit number of items). It sends back these items in a format that's easy to use.
Related: How to calculate the costs of an API integration
Time-based pagination is like reading a diary, where you flip through pages based on dates. It's ideal for datasets where entries are time-stamped and the user is interested in viewing records from a specific period.
This approach is common in applications that deal with logs, historical data, or real-time feeds where temporal context is important.
In essence, this function is for getting a specific number of items from a list, where those items fall within a certain time range specified by the user.
1. Time filters: The function allows users to specify a start_time and an end_time to filter items. These items have timestamps associated with them.
2. Limiting the number of items: There's also a limit on how many items can be retrieved at once. By default, it's set to 10, but users can specify a different limit.
3. Fetching the items: The function then runs a database query to get the items that match the time criteria and limits the result to the specified number of items.
4. Sending the results back: Finally, it sends the list of retrieved items back in a format that's easy to understand and use, which is likely a JSON response.
Page number pagination is like using a traditional index in a book. Users specify the exact page number they wish to access. This method is straightforward and user-friendly, especially for datasets where the total number of pages is known and not excessively large.
In short, this code helps users see parts of a big list, a set number of items at a time, and lets them choose which part to see.
1. Choosing the page: The user can pick which page of items they want to see, like page 1, page 2, and so on. If they don't choose, it shows the first page.
2. How many Items on a page: It also lets the user decide how many items should be on each page. If they don't say, it shows 10 items per page.
3. Finding the starting point: The code calculates where to start showing items for the chosen page. For example, on page 2, it skips the first 10 items (if there are 10 items per page) and starts from item 11.
4. Getting the items: Then it gets the right amount of items from the list, starting from where it was calculated.
5. Sending the list back: Finally, it sends this list of items back to the user.
Related: Software integration best practices
As you look to scale the number of integrations you build and maintain, you might find challenges in dealing with the APIs' varied approaches to pagination. Here are a few to keep in mind:
APIs use various pagination methods, like page-based, cursor-based, offset-limit, or time-based. Aligning these differing approaches into a single, consistent pagination model for your application can be complex. This involves understanding and adapting to each API's unique method while maintaining a seamless experience for your application's users.
As your application scales, you'll likely encounter massive volumes of data. Efficiently processing, storing, and querying millions of data objects without performance degradation is a substantial challenge. This includes ensuring that pagination doesn't become a bottleneck in data retrieval and processing.
Different APIs may impose varying limits on page sizes and data retrieval rates. You must balance these limits to optimize data fetching, minimize API calls, and ensure a responsive user experience.
This balancing act requires smart caching strategies, efficient batch processing, and adept asynchronous data handling.
Many APIs impose rate limits and data quotas. When your application relies on multiple APIs, managing these limits becomes complex. You need to ensure that your pagination strategy doesn't lead to excessive API calls that might exceed these limits, leading to potential service disruptions or additional costs.
Maintaining a consistent and intuitive user experience can be challenging when integrating multiple pagination methods. Users should feel a seamless experience regardless of the underlying pagination mechanism.
Rather than building out your own pagination architecture, Merge provides a single form of pagination to interact with data from its 180+ integrations.
Merge uses cursor pagination, and pagination is specified via the cursor and page_size query parameters. The next and previous cursors are attached to paginated API responses. Their values inform the cursor where to point to next. You can learn more about our approach to pagination in our docs.
In addition to unified pagination, Merge also handles rate limits, offers a set of unified Common Data Models, and more.
Wondering how Merge can support your integration strategy? You can find out by connecting with one of our integration experts!