← Blog

Claude API PDF Document Input Tutorial

2026-11-23 · 5 min read · SubToAPI Team

Claude can read PDFs directly — no OCR pipeline, no text extraction library, no manual chunking. You send the file as a document content block in a Messages API request, and Claude processes both the text and the visual layout of the pages (tables, charts, scanned pages) in the same call. This tutorial covers the exact request format, size limits, and common gotchas when wiring PDF input into a real application.

If you've tried passing a PDF as a plain string or dumping extracted text into a prompt, you've probably lost table structure or hit token limits on long documents. The document block avoids both problems: Claude sees the actual pages, and you don't pay the token cost of a separate OCR step.

How PDF input works in the Messages API

A PDF is sent as a content block alongside your text, inside the content array of a user message. There are two ways to provide the file: base64-encoded bytes, or a public URL.

Base64 source:

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "base64",
              "media_type": "application/pdf",
              "data": "JVBERi0xLjQKJ..."
            }
          },
          {
            "type": "text",
            "text": "Summarize the key terms in this contract."
          }
        ]
      }
    ]
  }'

URL source (if the PDF is already hosted somewhere accessible):

{
  "type": "document",
  "source": {
    "type": "url",
    "url": "https://example.com/report.pdf"
  }
}

The media_type must be application/pdf for the base64 variant — Claude will reject other types in a document block. Order matters for readability but not for correctness: you can place the document block before or after the text block, as long as both are in the same content array.

Encoding a local file in JavaScript

import fs from "fs";

const pdfBase64 = fs.readFileSync("./invoice.pdf").toString("base64");

const response = await fetch("https://api.anthropic.com/v1/messages", {
  method: "POST",
  headers: {
    "x-api-key": process.env.ANTHROPIC_API_KEY,
    "anthropic-version": "2023-06-01",
    "content-type": "application/json"
  },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: [
          {
            type: "document",
            source: {
              type: "base64",
              media_type: "application/pdf",
              data: pdfBase64
            }
          },
          { type: "text", text: "Extract the invoice number, total, and due date as JSON." }
        ]
      }
    ]
  })
});

const data = await response.json();
console.log(data.content[0].text);

Asking for structured JSON output in the prompt, as above, is a reliable way to turn unstructured PDFs into machine-readable data without a separate parsing step.

Limits and practical constraints

Streaming and tool use with PDF input

PDF document blocks work the same way inside streaming requests and tool-enabled requests — there's no separate API for document input. Set "stream": true to get incremental output while Claude processes the file, which matters for longer PDFs where generation can take several seconds. If you're building an agent that extracts data and then calls a function (e.g., writing the result to a database), combine the document block with your tools array exactly as you would for a text-only prompt. See /docs/tools for the tool-calling format if you're running SubToAPI.

Using PDF input through SubToAPI

If your app already calls Claude through SubToAPI, the request shape is identical — you just point at a different base URL and use your sub_live_ key instead of a raw Anthropic key:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "base64",
              "media_type": "application/pdf",
              "data": "JVBERi0xLjQKJ..."
            }
          },
          { "type": "text", "text": "List all line items and their totals." }
        ]
      }
    ]
  }'

This is useful if you want usage metadata per team member, streaming support, and a dashboard view of PDF-processing costs without managing a raw provider key. Full request/response details are in /docs/messages, and /docs/quickstart walks through getting a key from /signup.

Common mistakes to avoid

Questions

Can Claude read scanned (image-only) PDFs without OCR? Yes. The document content block renders each page as Claude sees it, so scanned pages are read directly. Accuracy depends on scan resolution and font size, same as any vision-based reading task.

What's the maximum PDF size the Claude API accepts? Page count is capped (around 100 pages) and total request size is capped (roughly 32MB), whichever limit you hit first. For longer documents, split into chunks or send specific page ranges.

Can I send a PDF URL instead of base64 data? Yes, use "source": {"type": "url", "url": "..."} instead of the base64 variant, as long as the URL is publicly reachable without authentication.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →