← Blog

Claude API Image Input: Vision Tutorial

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

Claude's API supports image input, meaning you can send a photo, screenshot, chart, or scanned document alongside a text prompt and get back a text response describing, analyzing, or extracting data from it. This is Claude's "vision" capability, and it works through the same Messages endpoint you use for text — you just add an image content block next to your text block.

This tutorial covers exactly how to structure that request: encoding images correctly, which formats and sizes are supported, how to send multiple images in one call, and common mistakes that cause 400 errors or wasted tokens.

How image input works in the Claude API

Claude doesn't take an image URL by default in the standard API flow — you send the image as base64-encoded data inside the message content array. Each message can mix multiple content blocks: some type: "image", some type: "text". Claude processes them in the order you provide them, so if you want the model to look at an image and then answer a specific question, put the image block first and the text block second.

A single user message with an image looks like this:

{
  "role": "user",
  "content": [
    {
      "type": "image",
      "source": {
        "type": "base64",
        "media_type": "image/jpeg",
        "data": "<base64-encoded-image-data>"
      }
    },
    {
      "type": "text",
      "text": "What's shown in this chart? List the three highest values."
    }
  ]
}

Supported media_type values are image/jpeg, image/png, image/gif, and image/webp. If you send an unsupported format or malformed base64, the API returns a 400 with a descriptive error — check that your encoding didn't accidentally include a data URI prefix (data:image/png;base64,...) in the payload, since the field expects raw base64 only.

Step-by-step: sending an image with curl

Here's a full example using curl, encoding a local file and sending it to the Claude API:

IMAGE_BASE64=$(base64 -i screenshot.png | tr -d '\n')

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_BASE64"'"
            }
          },
          {
            "type": "text",
            "text": "Describe any UI bugs visible in this screenshot."
          }
        ]
      }
    ]
  }'

The response comes back as a normal Messages response — a text block with Claude's analysis. There's no separate "vision endpoint"; it's the same model and same API surface as text-only requests.

JavaScript example with multiple images

You can include more than one image in a single message, which is useful for side-by-side comparisons or multi-page documents:

const fs = require("fs");

function imageBlock(path, mediaType) {
  const data = fs.readFileSync(path).toString("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: [
          imageBlock("before.png", "image/png"),
          imageBlock("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);

Claude evaluates each image independently and in sequence, so clearly label what each image represents in your accompanying text if the order matters to the answer.

Image size and token cost

Image tokens are calculated based on resolution, not file size. Very large images consume more tokens and cost more per request, so resizing before upload is worth doing if you're processing images in bulk. As a rule of thumb, keep the longest edge under roughly 1500px for most document and screenshot use cases — this rarely loses meaningful detail for text extraction or UI analysis while keeping costs predictable. If you're building a pipeline that handles hundreds of images a day, track token usage per request so you can catch unexpectedly large uploads before they inflate your bill.

Common use cases

Simplifying vision requests with SubToAPI

If you're already using SubToAPI to turn your Claude access into a managed API, image input works the same way through our endpoint — send the same multi-block message structure to https://api.subtoapi.app/v1/messages with your sub_live_... key in the Authorization: Bearer header. You get the added benefit of per-key usage metadata, so you can see exactly how many tokens your image-heavy requests are consuming without digging through logs. Full request and response formats are in the docs, and the quickstart walks through authentication if you're setting this up for the first time. Plans start at Solo for solo builders and scale up to Team and Scale tiers for multi-seat usage — see pricing for details, or just sign up and try it with the free trial.

Best practices before you ship

questions

Can I send an image URL instead of base64 to the Claude API? No — the standard Messages API expects base64-encoded image data in the source.data field, not a hosted URL. You need to fetch and encode the image yourself before sending the request.

What image formats does Claude's vision support? JPEG, PNG, GIF, and WebP are supported via the media_type field. Other formats, or base64 strings that include a data URI prefix, will return a 400 error.

Does sending images cost more tokens than text? Yes — image tokens scale with resolution. Resizing large images before upload (longest edge around 1500px for most use cases) keeps costs predictable without sacrificing accuracy for document or screenshot analysis.

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 →