Claude API Multimodal Input Handling: A Working Example
What "multimodal input" means for the Claude API
If you've hit this page, you're probably trying to send an image (or a PDF) alongside text in a single Claude API call and getting confused by the content block structure. The short answer: Claude's /messages endpoint accepts a content array inside each message, and you mix text blocks with image or document blocks in whatever order makes sense for your prompt. There's no separate "vision endpoint" — multimodal input is just a different shape of the same request body.
This article walks through a working example for images, a working example for PDFs, and the mistakes that cause most "it returns an error" or "it ignores my image" support tickets.
The basic content block structure
Every message in a Claude API request has a role and a content field. For text-only prompts, content is usually a plain string. For multimodal prompts, content becomes an array of typed blocks:
{
"role": "user",
"content": [
{ "type": "text", "text": "What's wrong with this chart?" },
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "<base64-encoded-image-data>"
}
}
]
}
Order matters for how the model reads the prompt — if you want Claude to describe an image and then answer a question about it, put the image block before the text block, or write the text to explicitly reference "the image above."
Working curl example: text + image
Here's a complete example that base64-encodes a local image and sends it with a question:
IMAGE_B64=$(base64 -i chart.png)
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": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "'"$IMAGE_B64"'"
}
},
{ "type": "text", "text": "Summarize the trend in this chart in two sentences." }
]
}
]
}'
Supported media_type values include image/jpeg, image/png, image/gif, and image/webp. Mismatching the declared media type and the actual file format is the single most common cause of silent failures — if your file is a JPEG, don't label it image/png.
Working JavaScript example: multiple images
You're not limited to one image per message. This is useful for comparing two screenshots, reviewing a multi-page scan, or feeding in several product photos:
import fs from "fs";
function toImageBlock(path, mediaType) {
const data = fs.readFileSync(path, { encoding: "base64" });
return {
type: "image",
source: { type: "base64", media_type: mediaType, data }
};
}
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: [
toImageBlock("before.png", "image/png"),
toImageBlock("after.png", "image/png"),
{ type: "text", text: "What changed between the 'before' and 'after' screenshots?" }
]
}
]
})
});
const result = await response.json();
console.log(result.content[0].text);
Each image consumes a meaningful chunk of the context window — a large screenshot can translate into over a thousand tokens before Claude even reads your question. If you're processing many images per request, resize them down to the resolution you actually need before encoding.
Handling PDFs and documents
For PDFs, swap type: "image" for type: "document" and use the appropriate media type:
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": "<base64-encoded-pdf-data>"
}
}
Claude processes each page of the PDF as both text and image, which is why PDFs with charts, tables, or scanned handwriting work reasonably well even without OCR preprocessing on your end. Keep PDFs under the model's page/size limits — splitting a 200-page report into logical chunks (by section, not arbitrarily) usually gives better answers than dumping the whole document in one call anyway.
Common mistakes that break multimodal requests
- Sending raw bytes instead of base64. The
datafield must be a base64 string, not a file path or binary blob. - Wrong media type.
image/jpgis not valid — it'simage/jpeg. - Oversized payloads. Very large base64 images bloat your request body and token usage. Compress or downscale first.
- Mixing up block order with multi-turn context. If an earlier turn included an image, you need to resend it in subsequent turns if you want Claude to reference it again — conversation history doesn't "remember" images outside what's explicitly in the message array you send.
- Forgetting
max_tokens. Multimodal responses that describe an image in detail can get cut off ifmax_tokensis set too low for the task.
Routing multimodal requests through SubToAPI
If your product already has an Anthropic API key but you want a stable HTTPS surface for your own app — with per-application keys, usage metadata per request, and team seats instead of sharing one raw key — SubToAPI wraps the same /messages shape shown above. You send the identical content array structure to https://api.subtoapi.app/v1/messages with Authorization: Bearer $SUBTOAPI_KEY, and multimodal input (images, documents, tool use, streaming) works exactly as documented at /docs/messages. It's useful if you're shipping a product feature on top of Claude and want clean API keys and usage tracking without building that layer yourself — see /docs/quickstart to get a key, or check /pricing for plan details.
questions
Can I send an image and a PDF in the same request? Yes. Content blocks of different types can be mixed freely in the same content array — a document block, an image block, and a text block can all appear in one message as long as the total size stays within the model's limits.
Do I have to use base64, or can I pass an image URL? Base64 ("type": "base64") is the universally supported method and what most integrations use. Check your specific client library's docs for URL-based sources, since support varies by SDK version and isn't guaranteed everywhere.
Why does Claude ignore the image and only answer the text part? This almost always comes down to a malformed content block — wrong media_type, truncated base64 data, or the image block nested incorrectly outside the content array. Validate the JSON structure against a minimal working example before adding complexity.