Cursor Pagination
"Cursor" pagination, also known as "keyset" pagination, is a method where you use a pointer (the cursor) to navigate through your results rather than the more traditional "offset/limit" method. The primary advantage of cursor-based pagination is its ability to efficiently paginate through large datasets without the performance pitfalls that can come with offset-based approaches.
In many APIs in Shoplazza, you'll receive a "cursor" value indicating how to retrieve the next set of results. The inclusion of a "pre_cursor" suggests the ability to navigate back to previous sets of results as well.
Here's a simple example:
Assume you are fetching a list of users through an API endpoint. The first API call might return a response like this:
{"data": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},...
],"cursor": "abc123","pre_cursor": null
}
To fetch the next set of results, you'd use the cursor value (in this case, "abc123").
If you later navigate to another page and receive a response like:
{"data": [
{"id": 101, "name": "Zack"},
{"id": 102, "name": "Zoe"},...
],"cursor": "def456","pre_cursor": "abc123"
}
You can use the cursor value ("abc123") to go back and fetch the previous set of results.
To use these cursors, you typically pass them as query parameters to your API request. For instance:
- Fetch the first set of results: GET /users
- Fetch the second set of results: GET /users?cursor=abc123
- Fetch the third set of results: GET /users?cursor=def456
- Fetch the previous set of results while you are on the second set: GET /users?pre_cursor=abc123 (The exact implementation may vary depending on the API.)
This is just a conceptual example; the specifics will vary based on the API's implementation. Always refer to the API documentation for exact usage details.