Pagination
In this guide, we will explore how to handle paginated responses when querying your data. By default, the API returns results paginated with a limit of 1000 items per page. You can adjust this number up to a maximum of 10,000 by using the pageSize
parameter in your request.
Understanding Pagination
When you make a request to endpoints that the max amount (pageSize
) amount of items, the response is paginated. Each paginated response includes a data
object containing the items for the current page and a pagination
object that provides navigation details:
current_page
: Indicates the current page number.has_more
: A boolean that shows whether there are more items to fetch. Iftrue
, you can request the next page to continue retrieving data.next_page
: Provides the URL to access the next page of results. This is only included if there are more items to fetch.prev_page
: Provides the URL to access the previous page of results. This is only included if the current page is greater than 1.
Navigating Pages
To navigate through pages, you can use the page
and pageSize
query parameters:
page
: Specifies the number of the current page you wish to retrieve.pageSize
: Specifies the number of items each page should contain, allowing you to control the volume of data returned in a single API call.
Example using Sleep Data Request
In this example, we retrieve sleep data for a specific user over a specified date range, broken into pages. This request fetches the first page of data between January 1 and January 10, 2024, with each page containing up to 5 entries. The response's has_more
attribute will indicate if additional data is available beyond the first page.
- Name
startDate
- Type
- string
- Description
Start date for the data retrieval in ISO 8601 format.
- Name
endDate
- Type
- string
- Description
End date for the data retrieval in ISO 8601 format.
- Name
page
- Type
- integer
- Description
Specifies the page number to retrieve.
- Name
pageSize
- Type
- integer
- Description
Limits the number of entries per page.
API Request using Fetch
const url = 'https://api.onetwentyone.ai/api/v1/sleep/USERUUID';
const startDate = '2024-01-01';
const endDate = '2024-01-10';
const page = 1;
const pageSize = 5;
const access_token = 'your_access_token_here'; // Replace with your actual access token
fetch(`${url}?startDate=${startDate}&endDate=${endDate}&page=${page}&pageSize=${pageSize}`, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
},
})
.then(response => response.json())
.then(data => {
console.log('Data:', data);
console.log('Pagination:', data.pagination);
})
.catch(error => {
console.error('Error:', error);
});
Paginated Response
{
"pagination": {
"current_page": 1,
"has_more": true,
"next_page": "https://api.onetwentyone.ai/api/v1/sleep/USER123?startDate=2024-01-01&endDate=2024-01-10&page=2&pageSize=5"
},
"data": [
{
"session_id": "XYZ123",
"start_time": "2024-01-01T22:00:00Z",
"end_time": "2024-01-02T06:00:00Z",
"duration": 28800
// Additional sleep session details...
},
// More sleep session objects...
]
}