When an API response contains more data than can be returned in a single page, the API will return with a token to request the next page of results.
But how will you expect to receive a paginated response? If your response contains a next page token, make the same request again using that token as a query parameter.
Say you have 70 users on your account, as in, more than the default page size of 30. A request to the list users will initially respond with 30 users. To get the full list, you’ll need to make three requests (30, 30, then 10).
Requesting https://api.zoom.us/v2/users for the first time, you’ll see a next_page_token
in the response body. This indicates you’ll need to make subsequent requests.
The first request returns 30 users:
GET https://api.zoom.us/v2/users
Response:
{
page_count: 3,
page_number: 1,
page_size: 30,
total_records: 70,
next_page_token: 'aaaaaaaaaaaaaaaaaaaaaaaaaaa', // for example
users: [] // count: 30
}
The second request returns 30 users:
GET https://api.zoom.us/v2/users&next_page_token=aaaaaaaaaaaaaaaaaaaaaaaaaaa
Response:
{
page_size: 30,
total_records: 70,
next_page_token: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb', // for example
users: [] // count: 30
}
The third request returns 10 users:
GET https://api.zoom.us/v2/users&next_page_token=bbbbbbbbbbbbbbbbbbbbbbbbbbb
Response:
{
page_size: 30,
total_records: 70,
next_page_token: '', // no token is returned in the final response
users: [] // users.length = 10
}
Many Zoom API endpoints, including from users API, will allow you to set larger page sizes up to 300 results.
Handling pagination through recursive functions
In JavaScript, this can be handled using recursive functions to handle larger responses conditionally. Below is an example of handling responses in NodeJS.
// https://www.npmjs.com/package/axios
import axios from 'axios'
// example headers for authorization
const headers = {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + YOUR_ACCESS_TOKEN,
},
};
// For this sample, we'll manually set page size in the URL to force pagination.
let manualPageSize = 2;
let baseURL = `https://api.zoom.us/v2/users?page_size=${manualPageSize}`;
// Create a function that takes in the endpoint and the initial/current array of users
const getUserList = (url, userArray) => {
axios.get(url, headers).then((response) => {
const retrievedUsersArray = userArray.concat(response.data.users);
if (response.data.next_page_token) {
let nextPath = `${baseURL}&next_page_token=${response.data.next_page_token}`;
return getUserList(nextPath, retrievedUsersArray);
} else {
return retrievedUsersArray;
}
});
};
// Call the function using the base URL (without next_page_token) and an empty array
getUserList(baseURL, []);