Claude API curl Example

Call the Claude API with curl. Complete 2026 examples: basic message, streaming, tool use, system prompt, and JSON extraction — no SDK required.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

curl is the fastest way to verify your API key, test prompt changes, or debug requests without writing any application code.

Prerequisites

export ANTHROPIC_API_KEY="sk-ant-your-key-here"

Minimal request

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-6",
    "max_tokens": 256,
    "messages": [{"role": "user", "content": "Hello, Claude!"}]
  }'

With system prompt

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-6",
    "max_tokens": 512,
    "system": "You are a concise technical writer. Respond in bullet points.",
    "messages": [{"role": "user", "content": "What is prompt caching?"}]
  }'

Extract just the text with jq

curl -s 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-6","max_tokens":256,"messages":[{"role":"user","content":"What is 12*34?"}]}' \
  | jq -r '.content[0].text'

Streaming response (SSE)

curl --no-buffer 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-6",
    "max_tokens": 512,
    "stream": true,
    "messages": [{"role": "user", "content": "Count from 1 to 10 slowly."}]
  }' | grep "^data:" | jq -r 'select(.type=="content_block_delta") | .delta.text // empty' 2>/dev/null

JSON extraction (structured output)

curl -s 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-6",
    "max_tokens": 256,
    "system": "Return only valid JSON. No explanation.",
    "messages": [{
      "role": "user",
      "content": "Extract: name, price, category from: Blue Widget, $12.99, Hardware"
    }]
  }' | jq '.content[0].text | fromjson'

Tool use (function calling)

curl -s 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-6",
    "max_tokens": 256,
    "tools": [{
      "name": "get_weather",
      "description": "Get weather for a city",
      "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"]
      }
    }],
    "messages": [{"role": "user", "content": "What is the weather in London?"}]
  }' | jq '.content[] | select(.type=="tool_use")'

Check token usage

curl -s 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-6","max_tokens":256,"messages":[{"role":"user","content":"Hello"}]}' \
  | jq '{input_tokens: .usage.input_tokens, output_tokens: .usage.output_tokens, stop_reason: .stop_reason}'

API parameters reference

Header / FieldRequiredValue
x-api-key headerYesYour sk-ant-… key
anthropic-version headerYes2023-06-01
content-type headerYesapplication/json
modelYese.g. claude-sonnet-4-6
max_tokensYesInteger; billed regardless of actual output length
streamNotrue for SSE streaming
systemNoSystem prompt string
temperatureNo0.0–1.0, default 1

Use these curl snippets as quick smoke tests before wiring the SDK into your app. Estimate token costs at the Claude API Cost Calculator.

Frequently asked questions

What is the Anthropic API base URL for curl?
The endpoint is `https://api.anthropic.com/v1/messages`. All requests are POST with `Content-Type: application/json`. Pass your key in the `x-api-key` header and `anthropic-version: 2023-06-01` is required.
How do I stream the Claude API response with curl?
Add `'stream': true` to the JSON body and pass `--no-buffer` to curl. Each SSE line is `data: {...}` — filter for `content_block_delta` events to extract text chunks. Pipe to `jq -r 'select(.type=="content_block_delta") | .delta.text'` for clean output.
Can I use curl to test Claude API tool use?
Yes — include a `tools` array in the JSON body with name, description, and input_schema. The API returns a `tool_use` content block with structured input you'd normally parse in application code.
How do I pass my API key safely in curl?
Set the key in an environment variable: `export ANTHROPIC_API_KEY=sk-ant-…` then reference it as `$ANTHROPIC_API_KEY` in the curl header. Never paste the raw key in shell history.
What does the claude API return for curl requests?
JSON with `id`, `type`, `role`, `content` (array of blocks), `model`, `stop_reason`, and `usage` (input_tokens, output_tokens). Text responses are in `content[0].text`.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript