← Blog

Claude API Developer Documentation Examples Guide

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

If you're searching for Claude API developer documentation examples, you're probably trying to get from "I have an API key" to "I have working code" as fast as possible. This article walks through the core request patterns — basic messages, streaming, tool use, and error handling — with copy-pasteable examples you can adapt directly.

The official Anthropic documentation covers the full API surface, but it's organized as reference material, not as a quickstart. Below is a condensed, example-first version of the same concepts: what a request looks like, what a response looks like, and the small details (headers, roles, token limits) that trip people up on the first try.

The Basic Request Shape

Every Claude API call is a POST to a messages endpoint with a JSON body containing a model name, a max_tokens value, and a messages array. Here's the minimal example:

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": "Explain the difference between a list and a tuple in Python."}
    ]
  }'

The response comes back as a JSON object with a content array (usually one text block), a stop_reason, and token usage counts. The things developers most often get wrong at this stage:

Multi-Turn Conversations

Claude is stateless between requests — there's no server-side conversation memory. To continue a conversation, you resend the full message history each time:

{
  "model": "claude-sonnet-4-5",
  "max_tokens": 1024,
  "messages": [
    {"role": "user", "content": "What's a race condition?"},
    {"role": "assistant", "content": "A race condition occurs when..."},
    {"role": "user", "content": "Give me a concrete example in JavaScript."}
  ]
}

This is the most common source of confusion in example code: people assume the API "remembers" prior turns because chat UIs behave that way. Your application layer owns conversation state, not the API.

Streaming Responses

For chat interfaces or anything latency-sensitive, streaming avoids waiting for the full response to generate before showing anything to the user. Set "stream": true and consume server-sent events:

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,
    stream: true,
    messages: [{ role: "user", content: "Write a haiku about debugging." }],
  }),
});

const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  console.log(decoder.decode(value));
}

Each chunk arrives as a named event (message_start, content_block_delta, message_stop, etc.). Most example bugs here come from parsing the raw SSE text incorrectly rather than using an SSE-aware client.

Tool Use (Function Calling)

Claude can call tools you define by returning a structured request instead of plain text. You define the tool schema, send it alongside the conversation, and handle the tool_use block Claude returns:

{
  "model": "claude-sonnet-4-5",
  "max_tokens": 1024,
  "tools": [
    {
      "name": "get_weather",
      "description": "Get current weather for a city",
      "input_schema": {
        "type": "object",
        "properties": { "city": { "type": "string" } },
        "required": ["city"]
      }
    }
  ],
  "messages": [{ "role": "user", "content": "What's the weather in Lisbon?" }]
}

When Claude decides to use the tool, the response contains a tool_use content block with the tool name and arguments. Your code executes the actual function, then sends the result back as a tool_result message to get Claude's final answer. This round-trip — request, tool call, tool result, final response — is the pattern every tool-use example follows, regardless of what the tool actually does.

A Simpler Path: SubToAPI

Writing and maintaining this request/response handling, plus key rotation, usage tracking, and billing per application, adds up for small teams. SubToAPI wraps the same Claude capabilities — messages, streaming, and tool use — behind application API keys (sub_live_...) that you issue per project or per customer, with usage metadata and team seats managed in one dashboard instead of custom tooling.

The request shape is intentionally familiar if you've already read Anthropic's docs:

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": "Summarize this PR description."}]
  }'

If you're evaluating options, the quickstart covers setup end to end, the messages reference documents request/response fields, streaming covers SSE consumption, and tools walks through the function-calling round-trip. Plans start at €9/month on the Solo tier, with Team (€19/seat) and Scale (€49/seat) options for larger groups — see pricing — and every plan starts with a free trial via signup.

Error Handling You Should Plan For

Example code that only shows the happy path will break in production. At minimum, handle:

Wrapping these in a small retry helper around your request function covers most production failure modes without needing a full resilience library.

Questions

Do I need the official SDK to use the Claude API? No. The API is plain HTTPS/JSON, so curl, fetch, or any HTTP client works fine — the SDK just adds typed helpers and retry logic.

Why doesn't Claude remember earlier messages in my app? The API is stateless. You must resend the full message history on every request; there's no server-side session unless your application stores and replays it.

What's the fastest way to test a request before writing code? Use curl with your API key directly in the terminal — it's the quickest way to confirm your request shape and headers are correct before wiring up application logic.

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 →