How to Get Citation Details from File via your CustomGPT Agent using CustomGPT RAG APIs (Explained Step by Step)

Author Image

Written by: Priyansh Khodiyar

Have you ever wanted to know where your chatbot’s answers are coming from? With CustomGPTs RAG API, you can not only build chatbots (called agents here) but also track the exact source of the answers — thanks to citations.

This blog will walk you through a Python notebook called Get_Citation_File.ipynb, which shows how to:

  • Create an agent
  • Upload content or use a sitemap
  • Start a conversation
  • Send questions
  • Get citation details — i.e., proof of where the answer came from

Let’s dive into the code, step by step, and see how it all works together.

1. Prerequisites to use CustomGPT RAG API

Before you begin, make sure you have the following:

  • CustomGPT.ai Account: Sign up and log in at CustomGPT.ai.
  • RAG API Key: Retrieve your RAG API token from your dashboard.
  • Basic Python Knowledge: Knowing a bit about Python, REST APIs, and JSON will be helpful.
  • Google Colab: We’re demonstrating this in Google Colab, but you can use any Python environment.

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

With these basics covered, let’s code.

Step 1: Setup – Connect to CustomGPT’s RAG API

What’s happening here?

You’re telling Python, “Hey, I want to use the CustomGPT’s RAG API, and here’s my access key.”

The code:

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

api_token = 'YOUR_API_TOKEN'

import requests

import json

Explanation:

  • api_endpoint is the base URL for all the RAG API calls.
  • api_token is your personal key that gives you permission to use the RAGAPI.
  • We import requests to make RAG API calls (i.e., send data to/from CustomGPT).
  • json helps us handle data in JSON format, which is how the RAG API talks.

Get Your RAG API Key

To get your RAG 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.

Now that we’ve connected to the API and set up our tools, let’s move on to creating our agent and giving it something to learn from.

Step 2: Create an Agent (With a Sitemap Source)

What’s happening?

Here, you’re creating an agent and giving it content to learn from. In this case, we’re using a sitemap URL as the content source.

The code:

project_name = 'Example ChatBot using Sitemap'

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

payload = {

    "project_name": (None, project_name),

    "sitemap_path": (None, sitemap_path)

}

headers = {

    'Authorization': 'Bearer ' + api_token

}

url = api_endpoint + 'projects'

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

print(create_project.text)

Explanation:

  • You define the agent’s name (project_name) and give it a sitemap URL.
  • payload is the data sent to the RAG API to create the agent and assign the content.
  • headers include your RAG API token to prove you’re allowed to do this.
  • You send all this info to projects endpoint using a POST request.
  • Finally, you print the RAG API’s response — it tells you if the agent was created and returns useful details like its ID.

Great! We’ve created our agent and provided it with a sitemap to use as a knowledge base. But what if we want to give it even more content? Let’s upload a file as an additional source.

Step 3: Add a File Source to the Agent (Optional)

What’s happening?

In addition to the sitemap, you can upload a file to add more content to the agent.

The code:

from google.colab import files

uploaded_file = files.upload()

file_content = next(iter(uploaded_file.values()))

file_data = file_content

payload = {

    "file": file_data

}

headers = {

    'Authorization': 'Bearer ' + api_token

}

url = api_endpoint + 'projects/' + str(project_id) + '/sources'

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

print(create_project.text)

Explanation:

  • You upload a file using Google Colab’s file picker.
  • file_data stores the file’s content.
  • You build a new payload, this time only including the file.
  • You send this file to the agent using the /sources endpoint.
  • This adds the file to the agent’s knowledge base.

With our agent now loaded with content from both a sitemap and an uploaded file, we’re ready to start chatting with it. Let’s create a conversation so we can ask questions.

Step 4: Create a Conversation with the Agent

What’s happening?

Now that the agent has content, you’re setting up a chat session — like opening a conversation window.

The code:

name = 'Test Conversation'

payload = json.dumps({"name": name})

headers = {

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

    'Authorization': 'Bearer ' + api_token

}

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

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

print(create_conversation.text)

Explanation:

  • You give your conversation a name (Test Conversation).
  • You send this info in JSON format to the conversations endpoint.
  • The RAG API creates a session and returns a session_id — you’ll need this for chatting.
  • Now you’re ready to ask questions!

Now that the conversation is set up, we can send a question to our agent and see what it comes up with. Let’s ask it something based on the sitemap content.

Step 5: Send a Question (Prompt) to the Agent

What’s happening?

You’re now sending your first question to the agent and getting a response.

The code:

conversation_data = json.loads(create_conversation.text)["data"]

session_id = conversation_data["session_id"]

prompt = "Who is tom"

stream = 0

url = api_endpoint + 'projects/' + str(project_id) + '/conversations/' + str(session_id) + '/messages'

payload = json.dumps({

    "prompt": prompt,

    "stream": stream

})

response_sitemap_query = requests.post(url, stream=False, headers=headers, data=payload)

print(response_sitemap_query.text)

Explanation:

  • You extract session_id from the conversation so that you can keep chatting in the same session.
  • You define your question (Who is tom).
  • You send this question to the RAG API, and the agent responds.
  • You print out the response — and it includes citations!

The agent has responded! But how do we know where it got the answer from? That’s where citations come in. Let’s fetch the citation ID and look at the source of the answer.

Step 6: Get the Citation ID and Details

What’s happening?

Now, you’re grabbing the citation ID from the previous answer and asking CustomGPT, “Where exactly did this answer come from?”

The code:

message_data = json.loads(response_sitemap_query.text)['data']

citation_id = message_data['citations'][0]

url = api_endpoint + 'projects/' + str(project_id) + '/citations/' + str(citation_id)

citation_response = requests.get(url, headers=headers, data=payload)

print(citation_response.text)

Explanation:

  • The response includes a citation ID, which points to the source of the answer.
  • You send a GET request to the citations endpoint to fetch detailed info about that source.
  • The RAG API replies with where the info came from — either a URL or a file.

We’ve got the citation details, but let’s go a step further. We can preview the exact file or webpage where the answer came from to fully verify it.

Step 7: Preview the Citation Source

What’s happening?

You’re now previewing the citation, either from a URL or from the uploaded file.

The code:

data = json.loads(citation_response.text)['data']

page_url = data['page_url']

if page_url:

  preview_file = requests.get(page_url, headers=headers)

  print(preview_file)

else:

  print(data['url'])

Explanation:

  • If the citation came from a URL, you fetch and preview that page.
  • If it came from an uploaded file, you get the file’s internal path or ID.
  • This helps you verify the answer’s source.

Now that we’ve seen how to get a citation from a sitemap query, let’s try another question—this time based on the file we uploaded—to see how it all works for that content.

Step 8: Send Another Query (from File) + Get Its Citation

What’s happening?

You repeat the same process but ask about something that came from the uploaded file.

The code:

prompt = "Who is Vanka"

payload = json.dumps({

    "prompt": prompt,

    "stream": stream

})

response_file_query = requests.post(url, stream=False, headers=headers, data=payload)

print(response_file_query.text)

message_data = json.loads(response_file_query.text)['data']

citation_id = message_data['citations'][0]

url = api_endpoint + 'projects/' + str(project_id) + '/citations/' + str(citation_id)

citation_response = requests.get(url, headers=headers, data=payload)

print(citation_response.text)

data = json.loads(citation_response.text)['data']

page_url = data['page_url']

if page_url:

  preview_file = requests.get(page_url, headers=headers)

  print(preview_file.content)

else:

  print(data['url'])

Explanation:

  • Ask a new question (“Who is Vanka”) — probably based on your uploaded file.
  • Fetch its citation ID and details.
  • Preview the source — again, URL or file.

And just like that, we’ve completed the full cycle—from creating an agent and asking questions to retrieving and verifying citations from different content sources.

Summary: How Does Everything Work Together?

Here’s a quick flow of the process:

  1. Connect to the RAG API with your token.
  2. Create an agent and load it with a sitemap or file.
  3. Start a conversation (chat session).
  4. Send a question to the agent.
  5. Get the answer + citation ID.
  6. Fetch citation details to verify the source.
  7. Preview the source (URL or file content).

Why this is powerful:

With this flow, you can not only build smart chatbots but also make them transparent and verifiable — no more guessing where answers come from!

Absolutely! Here’s the ending section for your blog in the same structure and tone you provided, but tailored specifically for your “Get Citation Details from Your CustomGPT Agent” blog. This covers running the notebook, troubleshooting, and wraps up with a friendly conclusion.

Running and Testing the Notebook

Step-by-Step Execution

Run Each Cell: Go through the notebook one cell at a time — from setting up the RAG API connection, creating the agent, uploading content, to sending queries and fetching citation details.

Monitor the Outputs:

  • Agent Creation: Confirm that the agent is created successfully and you receive an ID in the response.
  • Conversation Setup: Check that a conversation is initiated and a session ID is generated.
  • Message Response: Ensure your question gets a response that includes citation IDs.
  • Citation Retrieval: Verify that citation details (like URLs or file previews) are printed correctly and match the sources you provided.

Verifying on CustomGPT.ai

Log in to your CustomGPT.ai dashboard to see your newly created agent. You can review the conversation logs and citation details directly in the dashboard to ensure everything is running as expected.

With everything up and running, let’s quickly go over some common issues and how to solve them.

Troubleshooting Common Issues

Here are some typical hiccups and how to address them:

  • Invalid RAG API Token: Make sure you’ve replaced ‘YOUR_API_TOKEN’ with your actual token. Also, verify that your token hasn’t expired and has the right permissions.
  • File Upload Issues (Multipart/Form-Data Errors): Double-check your payload formatting for file uploads. When uploading files in Colab, ensure you’re correctly capturing file content for the RAG API call.
  • JSON Parsing Errors: If Python throws a JSON error, verify the RAG API response using print() before trying to parse it. Make sure you’re using the correct keys to extract data.
  • Missing Citation Data: Not seeing citation IDs? Confirm that your question is based on content the agent has access to, and check whether citations are enabled in your account settings.

For more help, don’t hesitate to check out the official CustomGPT.ai documentation or join the community support for tips and troubleshooting.

Conclusion

Congrats! You’ve just learned how to:

  • Create a CustomGPT agent using a sitemap or uploaded file.
  • Start a conversation and ask questions within that agent.
  • Capture the agent’s responses along with citation IDs.
  • Retrieve and preview detailed citation information tied to your queries.

This entire process helps ensure transparency and trust in chatbot responses by letting you track exactly where the information came from.

Whether you’re building chatbots for content verification, customer support, or research, knowing how to access and manage citations can seriously level up your solution.

Got questions or need more guidance? Feel free to consult the CustomGPT.ai documentation or connect with fellow users in our community forum.

Happy coding, and enjoy exploring the full potential of CustomGPT agents! 🚀

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.