Pagination
An example of how to use pagination for fetching a lot of data at once from the REST API.
Overview
If your application needs to fetch more data than a single request can handle, pagination can be used to seamlessly fetch blocks of the data, or pages, until completion. It splits a large request into smaller ones at the cost of a few extra lines of code. While this example shows how one can fetch projects with pagination, it can be adapted for other requests if desired.
Preliminaries
Basic Auth For simplicity, we here use Basic Auth for authentication. We recommend replacing this with an OAuth2 flow production-level integrations.
Service Account Credentials You must create and know the credentials of a Service Account. Any role will suffice.
REST API This example utilizes our REST API to interact with our cloud. See the REST API Reference for a full list of available endpoints.
Example Code
The following points summarize the provided example code.
Projects are fetched in groups of 100 at a time.
When all projects have been fetched, print the total number of projects.
Iterate through and print the display name for each project.
Environment Setup
If you wish to run the code locally, make sure you have a working runtime environment.
The following packages are required to run the example code.
pip install requests==2.31.0The following modules are required by the example code and must be installed.
npm install [email protected]No additional packages are required.
Add the following environment variables as they will be used to authenticate the API.
Source
The following code snippet implements pagination in a few languages.
Expected Output
The number of projects resulting from each page will be displayed in the stdout.
Last updated
Was this helpful?