Add a File to an Agent Using CustomGPT.ai’s RAG APIs Python SDK – A Step‑by‑Step Guide

Author Image

Written by: Priyansh Khodiyar

Add a File to an Agent Using CustomGPT.ai’s RAG APIs Python SDK

Need to feed PDFs, Word docs, or plain‑text files into your chatbot’s knowledge base? This tutorial shows you exactly how to add a file to an agent (project), using the CustomGPTs RAG APIs Python SDK implementation. We’ll walk through:

  1. Installing and authenticating the SDK
  2. Reading a local file as bytes
  3. Uploading that file to an existing project (agent)
  4. Verifying that the file was indexed and is ready for chat

If you’re new to the platform, first check out Getting Started with CustomGPT.ai for New Developers, then come back here.

Notebook version: See the complete runnable code in our GitHub Cookbook.

Prerequisites

RequirementDetails
CustomGPT Account & API KeyCreate or sign in at CustomGPT → generate a token under Settings → API Tokens
Python 3 EnvironmentGoogle Colab, Jupyter Notebook, or local Python 3
customgpt‑client SDKInstall with pip install customgpt-client
Project IDThe agent you want to add files to
Local FilePDF, TXT, DOCX, or MD—max ≈ 10 MB (plan‑dependent)

When these are in place, we can jump into code.

1. Install & Authenticate the SDK

First, install the SDK (if you haven’t already) and set your API key so every call is authenticated.

# 1 – Install the CustomGPT SDK from PyPI

!pip install customgpt-client

# 2 – Import the SDK and helper class

from customgpt_client import CustomGPT          # Main interface

from customgpt_client.types import File         # Helper to wrap uploads

# 3 – Authenticate all SDK calls

CustomGPT.api_key = "YOUR_API_TOKEN"            # ← replace with real token

What’s happening?

  1. pip install downloads the SDK.
  2. CustomGPT exposes high‑level methods—no raw REST calls needed.
  3. File is a convenience wrapper that packages raw bytes + filename.
  4. Setting api_key means every SDK request includes your bearer token.

Get API keys

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.
Create API

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.

With authentication done, let’s read a file into memory.

2. Read the Local File

We need the file’s bytes so we can send them over HTTP.

# Path to the document you want to ingest

file_path = "path/to/document.pdf"          

# Open in binary (rb) mode and read the bytes

with open(file_path, "rb") as f:

    file_bytes = f.read()                   # ← now we have raw bytes

print("Read", len(file_bytes), "bytes")

Why binary mode?

  • Text mode (“r”) might alter line endings or encodings.
  • Binary (“rb”) preserves the exact byte sequence for files like PDF/DOCX.

Now that we have raw bytes, we can build the payload expected by the SDK.

3. Construct the File Payload

Wrap the bytes and filename in the SDK’s File helper.

# Create a File object: payload = bytes, file_name = original name

file_payload = File(

    payload   = file_bytes,           # Actual file data

    file_name = "document.pdf"        # Used by the backend to infer type

)

Why use File?

The SDK needs to know:

  • The binary content (payload) to upload.
  • The filename (file_name) so CustomGPT can store and index correctly.

Using File packages both into a single object.

Payload ready? Let’s upload it to your project.

4. Upload the File to Your Project

Call the SDK’s add_file endpoint and point it at your project ID.

add_resp = CustomGPT.Project.add_file(

    project_id = "YOUR_PROJECT_ID",    # ← replace with real project

    file       = file_payload          # ← object from previous step

)

print("Add File Status:", add_resp.status)  # Expect "success"

Under the hood

  • Sends POST /projects/{id}/files with multipart/form‑data.
  • CustomGPT splits the file into chunks and adds each as a “page.”
  • The response contains page IDs so you can track indexing.

Upload accepted? Great—let’s confirm that indexing completes.

5. Verify File Indexing

We’ll query all pages for the project and look for our filename.

# Fetch pages created for this project

pages_resp = CustomGPT.Page.list(project_id="YOUR_PROJECT_ID")

pages = pages_resp.parsed.data.data     # List of Page objects

# Filter pages originating from our file

for page in pages:

    if page.file_name == "document.pdf":

        print(f"Page ID: {page.id} | Status: {page.status}")

Status values

StatusMeaning
queuedPage is waiting to be crawled/embedded
indexingEmbeddings are being generated
indexedReady for chat queries
failedSomething went wrong (check file type/size)

If you see indexed, your document is live in the chatbot’s knowledge base.

All set! Let’s cover some common questions before you head off.

FAQs

Which file types are supported?

PDF, DOCX, TXT, MD, and other standard text formats.

Can I upload multiple files?

Upload one file per call; loop for batch ingestion.

Max file size?

~10 MB (varies by plan).

Do uploads consume credits?

Yes—token usage corresponds to the file’s size/content.

Delete a mistakenly added file?

List pages, then call Page.delete(project_id, page_id) for each page created from that file.

Related Posts

Need more? Dive into the CustomGPT API docs or check the GitHub Cookbook for complete notebooks.

Next Steps

  • Search your new content: Start a conversation and test queries specific to the uploaded file.
  • Automate batching: Wrap the upload code in a loop for dozens of files.
  • Combine with sitemaps: Blend file and URL content for a richer knowledge base.

Happy coding—and enjoy powering your chatbot with rich, document‑based knowledge!

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.