GET /lucca-api/leaves?include=links&limit=25&page=0 HTTPS/1.1
Host: https://example.ilucca.net
Authorization: <ACCESS_TOKEN>
Api-Version: <API_VERSION>
Accept: application/json
{
"url": "https://example.ilucca.net/lucca-api/leaves?limit=25&page=0",
"type": "leaves",
"items": [],
"totalCount": 2127,
"links": {
"prev": {
"href": "https://example.ilucca.net/lucca-api/leaves?limit=25&page=!8e7"
},
"next": {
"href": "https://example.ilucca.net/lucca-api/leaves?limit=25&page=!S7DHF"
}
}
}
All collection endpoints are subject to pagination. This means you most likely won’t be able to retrieve all resources in a single HTTP request. Paging is enforced in order to enhance response times and lower error rates.
You can control paging through two query parameters:
(int) limit
defines the maximum number of items per page you will get.
(string) page
is the continuation token obtained during a previous request allowing you to get the next page or the previous page.
The Lucca API enforces a maximum value to the limit
parameter, usually 100, as well as a default value if none is given. Please refer to the Lucca API Reference for the given collection endpoint.
Paginated responses have:
items
contains the resources in the given page ;
totalCount
indicates how many resources exist across all pages (while applying the query filters) ;
links
contains both the links to the previous and the next pages (null
if there is none).
Both the totalCount
and links
properties are only returned if your HTTP request explicitly includes them: ?include=totalCount,links
.
Paginated requests should be sorted explicitly. By default, most API endpoints fall back to a “id ascending” sorting when the ?sort
query parameter is omitted.
As a result, retrieving all pages may look something like this:
import merge from 'deepmerge-json';
// Retrieve all pages from a Lucca API collection
async function getAllPages(apiUrl, accessToken) {
let allItems = {items: [], embedded: {}};
// Recursive page fetch
const fetchPage = async (url) => {
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch page: ${response.status} ${response.statusText}`);
}
const data = await response.json();
const items = data.items || [];
const embedded = data.embedded || {};
allItems.items = allItems.items.concat(items);
allItems.embedded = merge(allItems.embedded, embedded);
// Check if there's a next page
// Alternative: you could also predict how many pages there are from the totalCount property value,
// then go for a for loop.
const nextPageUrl = data.links && data.links.next && data.links.next.href;
if (!!nextPageUrl) {
// Fetch the next page
await fetchPage(nextPageUrl);
}
};
// Start fetching from the initial API URL
await fetchPage(apiUrl);
return allItems;
}
// Example usage
const apiUrl = 'https://example.ilucca-sandbox.net/lucca-api/leaves?include=totalCount,embedded,links';
const accessToken = 'your_oauth_access_token';
getAllPages(apiUrl, accessToken)
.then((allItems) => {
console.log('All items:', allItems);
})
.catch((error) => {
console.error('Error:', error.message);
});