← Blog

Claude API Curl Request Examples for Every Use Case

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

If you want to call the Claude API from the command line, curl is the fastest way to test a request before writing any application code. This article walks through working curl examples for the most common tasks: basic messages, system prompts, multi-turn conversations, streaming, images, and tool use.

Every example below uses the standard messages endpoint shape. You can run these directly against Anthropic's API with your own key, or against a compatible gateway like SubToAPI if you want a simpler key format and usage dashboard. We'll note the differences where they matter.

Basic text request

The minimum viable Claude API request needs three things: a model name, a max_tokens value, and a messages array.

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 curl -d in one sentence."}
    ]
  }'

The response is a JSON object with a content array, a stop_reason, and token usage counts under usage. If you're using SubToAPI's gateway, the call is almost identical but authenticated with a bearer token instead:

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": "Explain curl -d in one sentence."}
    ]
  }'

See /docs/quickstart for the full setup and /docs/messages for the complete request schema.

Adding a system prompt

System prompts set behavior and tone without being part of the conversation history. Pass them as a top-level system field, not as a message:

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": 500,
    "system": "You are a terse code reviewer. Answer in bullet points only.",
    "messages": [
      {"role": "user", "content": "Review this function: def add(a,b): return a+b"}
    ]
  }'

Multi-turn conversations

Claude doesn't maintain state between requests — you resend the full conversation history each time, alternating user and assistant roles:

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": 500,
    "messages": [
      {"role": "user", "content": "What is a race condition?"},
      {"role": "assistant", "content": "A race condition happens when two threads access shared state without proper synchronization, and the outcome depends on execution order."},
      {"role": "user", "content": "Give me a one-line Go example."}
    ]
  }'

Keep the full history on your side (database, session store, etc.) and replay it on every request. There's no server-side thread concept to rely on.

Streaming responses

For chat UIs or anything latency-sensitive, stream tokens instead of waiting for the full response. Add "stream": true and use curl -N to disable output buffering so you see chunks as they arrive:

curl -N 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,
    "stream": true,
    "messages": [
      {"role": "user", "content": "Write a haiku about distributed systems."}
    ]
  }'

The response comes back as server-sent events — lines prefixed with event: and data:. Parse the content_block_delta events to assemble the text incrementally. Full event types are documented at /docs/streaming.

Sending an image

Claude's vision-capable models accept base64-encoded images inside a message's content array alongside text:

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": 500,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/png",
              "data": "'"$(base64 -w0 chart.png)"'"
            }
          },
          {"type": "text", "text": "What trend does this chart show?"}
        ]
      }
    ]
  }'

On macOS, swap base64 -w0 for base64 -i chart.png since the flag differs between GNU and BSD base64.

Tool use (function calling)

Tool use lets Claude return structured calls to functions you define, instead of (or in addition to) plain text. Define the tool schema and pass it in the tools array:

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": 500,
    "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 is the weather in Lisbon?"}
    ]
  }'

If Claude decides to call the tool, the response contains a tool_use content block with the tool name and input arguments. Your code executes the real logic and sends the result back in a follow-up request with a tool_result block. See /docs/tools for the complete round-trip pattern.

Debugging curl requests

A few practical tips that save time:

If you're building something that needs to go beyond ad-hoc testing — multiple team members, per-key usage limits, or streaming at production scale — a dashboard layer on top of raw curl calls is worth it. SubToAPI issues scoped sub_live_ keys per application, tracks token usage per key, and supports team seats starting at /pricing. Sign up at /signup to get a key and run the examples above against your own account.

questions

Do I need a different endpoint for streaming vs non-streaming requests? No. It's the same /v1/messages endpoint — you just add "stream": true to the request body and read the response as server-sent events instead of a single JSON blob.

Why does curl hang or show nothing when I use streaming? Curl buffers output by default. Use the -N flag to disable buffering so streamed chunks print as they arrive instead of all at once at the end.

Can I test tool use and images in the same request? Yes, the tools array and multimodal content blocks (text + image) are independent features and can be combined in a single request, as long as the model you're using supports both.

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 →