Delete a Page from Your Agent Using the CustomGPT API – A Step-by-Step Guide

Author Image

Written by: Priyansh Khodiyar

In this tutorial, you’ll learn how to remove an outdated or unwanted page from your agent on CustomGPT.ai using our CustomGPT API. This guide will show you each step—from creating an agent to finding and deleting a specific page—so you can keep your agent organized and up to date.

Make sure you have read our Getting Started with CustomGPT.ai for New Developers blog to get an overview of the entire platform.

1. Prerequisites to CustomGPT API

Before you begin, please make sure you have the following:

  • CustomGPT.ai Account: Sign up at CustomGPT.ai if you haven’t done so already.
  • API Key: Get your API token from your dashboard and keep it secure.
  • Basic Python Knowledge: Knowing a bit about Python and REST APIs will help.
  • Google Colab: We will run the code on Google Colab, so you don’t need to set up a local environment.

With these basics in place, let’s move on to setting up your environment in Google Colab.

2. Setting Up Your Environment in Google Colab

Upload the Notebook: Open Google Colab and upload the Delete_a_page_from_the_project.ipynb file.

Verify Dependencies: This notebook uses Python’s built-in json module and the requests library to make API calls. Make sure these are available in your environment.

Configure Your CustomGPT API Key: At the top of the notebook, you’ll see the CustomGPT API endpoint and token configuration:

api_endpoint = 'https://app.customgpt.ai/api/v1/'

api_token = 'ADD_YOUR_API_TOKEN_HERE'

headers = {

    'Content-type': 'application/json',

    'Authorization': 'Bearer ' + api_token

}

Replace ‘ADD_YOUR_API_TOKEN_HERE‘ with your actual API token. This token is needed to authenticate your requests, so keep it confidential.

Get Your API Key

To get your API key, there are two ways:

Method 1 – Via Agent

  1. Agent > All Agents.
  2. Select your agent and go to deploy, click on the API key section and create an API. 

Method 2 – Via Profile section.

  1. Go to profile (top right corner of your screen)
  2. Click on My Profile
  3. 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.

After setting up your CustomGPT API key and environment, the next step is to create a new agent.

3. Code Walkthrough

Here, we break down the code step-by-step to explain how to create an agent, list its pages, and delete one of them.

3.1 Creating a New Agent

Agent Setup: The notebook starts by creating a new agent with a sitemap. This step is necessary so that your agent has pages you can later manage.

project_name = 'Example ChatBot using Sitemap'

sitemap_path = 'https://adorosario.github.io/small-sitemap.xml'

payload = json.dumps({

    "project_name": project_name,

    "sitemap_path": sitemap_path

})

url = api_endpoint + 'projects'

create_project = requests.request('POST', url, headers=headers, data=payload)

print(create_project.text)

Note: Even though the code uses the term project_name, think of this as the name for your agent. The sitemap_path gives a URL to a sitemap that seeds your agent’s content. The POST request creates the agent and returns a unique agent ID.

Once your agent is created, we move on to creating a conversation to simulate a complete agent setup.

3.2 Creating a Conversation

Conversation Setup:
While our main goal is to delete a page, the notebook also sets up a conversation. This helps in managing and tracking your agent’s details.

name = 'Test Converasation'

payload = json.dumps({

    "name": project_name

})

url = api_endpoint + 'projects' + f"/{project_id}" + '/conversations'

create_conversation = requests.request('POST', url, headers=headers, data=payload)

print(create_conversation.text)

This code initiates a conversation using the agent’s unique ID. The response will include metadata that may be useful for other agent operations.

Now that you have a conversation set up, the next step is to retrieve your agent’s ID and list its pages.

3.3 Retrieving the Agent ID and Pages

Before you can delete a page, you need the agent’s ID and a list of pages associated with it.

data = json.loads(create_project.text)["data"]

project_id = data["id"]

url = api_endpoint + 'projects' + f"/{project_id}" + '/pages'

project_pages = requests.request('GET', url, headers=headers)

print(project_pages.text)
  • Extracting the Agent ID: The agent ID is taken from the creation response.
  • Listing Pages: A GET request to the pages endpoint returns all the pages of your agent, including their IDs.

With the list of pages in hand, we can now identify the page to delete.

3.4 Identifying and Deleting a Page

Finding the Page:
The code below selects the first page from the list and extracts its unique page ID.

data = json.loads(project_pages.text)["data"]

page_id = data["pages"]['data'][0]['id']

print(page_id)
  • Extracting the Page ID: This grabs the ID of the page you want to remove.

Deleting the Page: Now, use the DELETE request to remove the unwanted page.

url = api_endpoint + 'projects' + f"/{project_id}" + '/pages' + f"/{page_id}"

delete_page = requests.request('DELETE', url, headers=headers)

print(delete_page.text)
  • Deleting the Page: The DELETE request targets the specific page endpoint. The response confirms whether the deletion was successful.

With the DELETE request sent, we are ready to run and test the notebook to see the results.

4. Running and Testing the Notebook

Step-by-Step Execution:

  1. Run Each Cell: Execute each cell in your Colab notebook one by one.
  2. Observe the Outputs:
    • Confirm that the agent is created and you receive an agent ID.
    • Check the list of pages to ensure they are retrieved.
    • Verify that the DELETE request returns a success message indicating the page has been removed.

Verifying Changes on CustomGPT.ai:
Log in to your CustomGPT.ai dashboard to check that the deleted page no longer appears under your agent. This confirms that your API calls worked as expected.

Once you have verified the results, you can move on to troubleshooting if any issues arise.

5. Troubleshooting Common Issues

Here are some common problems you might encounter and tips on how to fix them:

  • Invalid API Token: Make sure your CustomGPT API token is correct, active, and properly inserted into the code.
  • Endpoint Construction Errors: Double-check that the agent ID and page ID are correctly placed in the URL.
  • JSON Parsing Errors: Ensure that the API responses are in the correct format and that you are using the right keys.
  • Network Issues: Verify that your internet connection is stable to avoid request timeouts.

If you need more help, consult the official CustomGPT.ai documentation or reach out to our community.

6. Conclusion

In this guide, you learned how to delete a page from your agent using the CustomGPT API. You:

  • Set up your environment and configured your API token.
  • Created a new agent and initiated a conversation.
  • Retrieved a list of pages associated with your agent.
  • Identified and deleted a specific page with a DELETE request.

By following these steps, you can keep your agent data clean and current by removing pages that are no longer needed. If you have any questions or need additional support, please refer to the CustomGPT.ai documentation or contact our community.

Happy coding, and enjoy managing your agents with CustomGPT. API!

Build a Custom GPT for your business, in minutes.

Deliver exceptional customer experiences and maximize employee efficiency with custom AI agents.

Trusted by thousands of organizations worldwide

Related posts

Leave a reply

Your email address will not be published. Required fields are marked *

*

3x productivity.
Cut costs in half.

Launch a custom AI agent in minutes.

Instantly access all your data.
Automate customer service.
Streamline employee training.
Accelerate research.
Gain customer insights.

Try 100% free. Cancel anytime.