Pagination
What is pagination
When data sets get large, it becomes inefficient to return them in a single response. To make it faster for both the server and API clients, we divide the data into small chunks. The API client asks for a chunk of data, and the server returns it along with a token called a cursor. The API client then sends the cursor back to the server to request the next chunk of data. This process repeats until the API client has iterated through all the data and is called pagination.
How does pagination work in the API?
All “list” endpoints in Cash App Pay use pagination, and support two pagination-related fields on the request:
cursor
, which is a string used by the server to fetch the next page of data. Use thecursor
from the response to populate this field.limit
, which is an integer from 1 to 100 that sets the maximum number of resources that can be returned in the response.
A cursor
is provided at the top-level of each paginated response if there is another page of data to load. Once no cursor is present on the response, you’ve iterated through all the data.
Example: Listing all payments
In this example, we’ll attempt to list all the payments taken by our API client. First, we’ll build a cURL
request to call list payments:
The API will respond with some JSON that looks like this:
Since we set the limit
parameter to 1
, only one resource is returned. To grab the next resource, we would make a similar request, but with the addition of the cursor as a query parameter:
In the response, notice that there is no longer a cursor
field:
This means that there is no more data to page through; we’ve now iterated through the entire data set.