Watch the demo video at the top for a live walkthrough of this guide! (coming soon)
1. Introduction
Hi there! In this guide, we’ll show you how to list all projects (or “agents” as we now call them) for your account using the CustomGPT RAG API—with pagination. Pagination helps you manage and view projects when there are many, breaking them into pages. We’ll walk you through the setup, the code, and explain each step in simple, conversational language.
Make sure you have read our Getting Started with CustomGPT.ai for New Developers blog to get an overview of the entire platform.
Get the cookbook link here – https://github.com/Poll-The-People/customgpt-cookbook/blob/main/examples/List_all_projects_for_an_account_with_pagination.ipynb
2. Setting Up the Environment
Before we dive in, let’s set up our workspace in Google Colab. This step involves specifying the API URL, inserting your API token, and importing the necessary libraries.
Code Sample:
# setup API URL and API Token
api_endpoint = 'https://app.customgpt.ai/api/v1/'
api_token = 'ADD_YOUR_API_TOKEN_HERE'
headers = {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + api_token
}
# imports
import requests
import json
What this does:
- API Endpoint: Sets the base URL for all API calls to CustomGPT.
- API Token: Uses your API token for authentication (remember to replace ‘
ADD_YOUR_API_TOKEN_HERE
‘ with your actual token). - Headers: Prepares the HTTP headers, including content type and authorization details.
- Imports: Loads the requests library for making HTTP requests and json for handling JSON data.
Now that our environment is ready, let’s review what you need before proceeding further.
3. Prerequisites to Use CustomGPT RAG APIs
Before you start, ensure you have the following:
- CustomGPT.ai Account: Sign up and log in at CustomGPT.ai.
- API Key: Generate your API token from your account dashboard.
- Basic Python Knowledge: Familiarity with Python and REST APIs will help you follow along.
- Google Colab: We’re using Google Colab, so no local setup is required!
With these prerequisites checked off, we’re ready to see how pagination works when listing all your agents.
Your token authenticates your requests and grants you access to manage your agents securely. Make sure your key is ready and correctly inserted into the code.
Get API keys
To get your API key, there are two ways:
Method 1 – Via Agent
- Agent > All Agents.
- Select your agent and go to deploy, click on the API key section and create an API.
Method 2 – Via Profile section.
- Go to profile (top right corner of your screen)
- Click on My Profile
- You will see the screen something like this (below screenshot). Here you can click on “Create API key”, give it a name and copy the key.
Please save this secret key somewhere safe and accessible. For security reasons, You won’t be able to view it again through your CustomGPT.ai account. If you lose this secret key, you’ll need to generate a new one.
4. List Projects with Pagination
In this section, we’ll list your projects (or agents) with pagination. This is useful if you have many projects and need to view them in manageable chunks.
Code Sample:
url = api_endpoint + 'projects'
page_1 = requests.request('GET', url, headers=headers)
print(page_1.text)
page_2 = requests.request('GET', url, headers=headers, params={"page": 2})
print(page_2.text)
page_3 = requests.request('GET', url, headers=headers, params={"page": 3})
print(page_3.text)
What this code does:
- Build the URL: Concatenates the base API endpoint with the ‘projects’ endpoint. This URL is used to request the list of all projects.
- Fetch Page 1: Sends a GET request without any pagination parameters, which returns the first page of projects.
- Fetch Page 2: Uses the params parameter with {“page”: 2} to get the second page of projects.
- Fetch Page 3: Similarly, sends a GET request with {“page”: 3} to retrieve the third page of projects.
- Print Responses: Each print statement outputs the JSON response, letting you see the projects listed on that page.
Now that you’ve seen how to fetch the projects page by page, let’s move on to troubleshooting some common issues you might face.
5. Troubleshooting Common Issues
Even with clear steps, sometimes issues arise. Here are a few common problems and how to fix them:
- Invalid API Token: Verify that you have replaced ‘
ADD_YOUR_API_TOKEN_HERE
‘ with your real API key. - Connection Errors: Ensure your internet connection is stable and that the API endpoint URL is correct.
- Pagination Not Working: Double-check that you’re sending the correct page parameter (e.g., {“page”: 2}) with your GET request.
- JSON Parsing Errors: If you run into issues, print the raw response to check its structure and confirm you’re using the right keys.
If these tips don’t resolve your issues, consult the official CustomGPT documentation or reach out to the community support forum for further help.
6. Conclusion
Great job! In this guide, you learned how to:
- Set up your Google Colab environment to work with the CustomGPT API.
- Configure the API endpoint, token, and headers.
- List all your projects (agents) using pagination to view them page by page.
This approach makes it easier to manage many projects by breaking the data into smaller, more manageable pages. If you have any questions or need additional help, feel free to check out the CustomGPT documentation or ask in our community. Happy coding, and enjoy building your chatbot solutions with CustomGPT!