ChatPDF API

The ChatPDF backend API allows you to programmatically extract information from PDF files and create custom chatbots based on your PDFs.
Sources
Before asking questions to a PDF, you need to add it to ChatPDF as a source. There are three ways to do so:
  • Upload the PDF on ChatPDF.com manually
  • Add a PDF from a publicly accessible URL via API
  • Upload a PDF file from your computer via API
PDFs are limited to 2,000 pages or 32 MB per file.
Chats
To ask questions to a PDF, use the chat endpoint, which expects a source ID and an array of messages. To ask a single question, send one message with the role user. To ask a follow-up question, send multiple messages, where AI-responses should have the role assistant. The ChatPDF chat API is stateless, which means that you need to send all relevant messages in one request, previous messages to the same source are not automatically included.

Add PDF manually on ChatPDF.com

After uploading a PDF to chatpdf.com, you can use the PDF with the API. To do so, open the chat and copy the source ID from the URL bar. If your URL is https://www.chatpdf.com/c/zNXgrtLfhbFw7KabJgE1 the corresponding source ID is cha_zNXgrtLfhbFw7KabJgE1. Note the cha_ prefix.

Add PDF via URL

If your PDF file is publicly accessible via a URL, this is the easiest way to send it to ChatPDF. This endpoint returns a source ID that can be used to interact with the PDF file.
Endpoint
POST https://api.hundi.chatpdf.com/sources/add-url
Request
{ "url": "https://uscode.house.gov/static/constitution.pdf" }
Response
{ "sourceId": "src_xxxxxx" }
Example

Add PDF via File Upload

Add a PDF file by uploading it to ChatPDF as a multipart form data. You can only upload one file at a time. This endpoint returns a source ID that can be used to interact with the PDF file.
Endpoint
POST https://api.hundi.chatpdf.com/sources/add-file
Request
<form data, see example>
Response
{ "sourceId": "src_xxxxxx" }
Example

Chat with PDF

Send a chat message to a PDF file using its source ID.
Endpoint
POST https://api.hundi.chatpdf.com/chats/message
Request
Ask a single question:
{
  "sourceId": "src_xxxxxx",
  "messages": [
    {
      "role": "user",
      "content": "how much is the world?"
    }
  ]
}
Ask a follow-up question:
{
  "sourceId": "src_xxxxxx",
  "messages": [
    {
      "role": "user",
      "content": "How much is the world?"
    },
    {
      "role": "assistant",
      "content": "The world is 10 dollars."
    },
    {
      "role": "user",
      "content": "Where can I buy it?"
    }
  ]
}
You can include up to 6 messages in one request. If the total number of OpenAI tokens in the these messages exceed 2,500, older messages are ignored until that limit is no longer exceeded.
Response
{ "content": "The world is 10 dollars." }
Example
Reference Sources
ChatPDF can include references to the PDF pages that were used to generate the response. To do so, include the referenceSources: true field in the request body:
{
  "referenceSources": true,
  "sourceId": "src_xxxxxx",
  "messages": [
    {
      "role": "user",
      "content": "how much is the world?"
    }
  ]
}
The response will include inline references in the form [P<N>], e.g. [P2], [P234], and an additional field references:
{
  "content": "The world is 10 dollars [P2] and can be bought at the supermarket [P5].",
  "references": [
    { "pageNumber": 2 },
    { "pageNumber": 5 }
  ]
}
Stream Response
You can optionally stream the response word by word. To do so, include the stream: true field in the request body:
{
  "stream": true,
  "sourceId": "src_xxxxxx",
  "messages": [
    {
      "role": "user",
      "content": "how much is the world?"
    }
  ]
}

Delete PDF

Delete one or multiple PDF files from ChatPDF using their source IDs.
Endpoint
POST https://api.hundi.chatpdf.com/sources/delete
Request
{ "sources": ["src_xxxxxx"] }
Response
This endpoint returns an empty response.
Example