Pagination
In this guide, we will look at how to work with paginated responses when querying the Gatsby API. By default, all responses limit results to ten items per page. You can modify this by adding a limit
parameter to your requests, and navigate through pages using the skip
parameter.
When an API response returns a list of objects, pagination is supported to help you navigate through large datasets. The Gatsby API uses a simple offset-based pagination approach with skip
and limit
parameters.
Example using skip and limit
In this example, we request the second page of results by skipping the first 10 items and limiting the response to 10 items per page. This approach allows you to easily navigate through all available pages.
- Name
skip
- Type
- integer
- Description
The number of items to skip (e.g., skip=10 for the second page).
- Name
limit
- Type
- integer
- Description
The maximum number of items to return per page (defaults to 10).
Pagination using cURL
curl -G https://rest.gatsby.events/person \
-H "Authorization: Bearer {token}" \
-H "organizationSlug: {organizationSlug}" \
-d skip=10 \
-d limit=10
Paginated response
{
"persons": [
{
"personId": "52907745-7672-470e-a",
"firstName": "John",
"lastName": "Doe",
// ...
},
{
"personId": "8293b8f2-3019-42a8-b",
"firstName": "Jane",
"lastName": "Smith",
// ...
},
// ... more results
]
}
Paginating through all results
To retrieve all items across multiple pages, you would implement a loop that increments the skip parameter:
import axios from 'axios';
async function getAllPersons() {
const limit = 10;
let skip = 0;
let allPersons = [];
let hasMore = true;
while (hasMore) {
const response = await axios.get('https://rest.gatsby.events/person', {
headers: {
'Authorization': `Bearer ${accessToken}`,
'organizationSlug': organizationSlug
},
params: { limit, skip }
});
const persons = response.data.persons;
allPersons = [...allPersons, ...persons];
// If we received fewer items than the limit, we've reached the end
hasMore = persons.length === limit;
skip += limit;
}
return allPersons;
}