Skip to content

Claude format: Messages API

Call Claude models in the Anthropic Messages format (POST /v1/messages). Claude Code and the Anthropic SDK both use this endpoint. Includes examples of images, thinking, tool use and web search.

The native Claude API matches Anthropic's official Messages API. Claude Code, the official Anthropic SDKs and other "Anthropic-compatible" tools all use this endpoint. It serves the Claude models marked with the Anthropic endpoint type in Model Square; the key's group decides which of them you can use (see Groups).

POST https://codaapi.com/v1/messages

Request headers

Name Required Description
x-api-key This or the next one sk-…. The Anthropic SDK uses this header by default, and so does Claude Code's ANTHROPIC_API_KEY
Authorization This or the previous one Bearer sk-…. Claude Code's ANTHROPIC_AUTH_TOKEN uses this header. If both headers are present, x-api-key wins
anthropic-version No Set 2023-06-01 as Anthropic requires; the official SDKs add it automatically. Without it the gateway sends 2023-06-01 upstream
anthropic-beta No Turns on Anthropic beta features. The gateway forwards it unchanged (all of the values you send). Whether a beta feature works depends on the model and the channel behind it
content-type Yes application/json

Address and authentication

  • Base URL: https://codaapi.com, without /v1. The SDK and Claude Code append /v1/messages themselves. If the Base URL includes /v1, the request becomes /v1/v1/messages and returns 404.
  • Key: put it in the x-api-key header or in the Authorization: Bearer header. Either works. For the other authentication forms, see Overview and auth.

Request body

The request body matches Anthropic's official format. The gateway passes the fields upstream, and whether a parameter takes effect depends on the model itself. Only the common parameters are listed below; Anthropic's official documentation is authoritative for the full set.

Parameter Type Required Description
model string Yes Model name, for example claude-sonnet-4-6. For models with a -thinking suffix, see below
max_tokens integer No The most tokens to generate in this call. Anthropic's official API requires it; without it, the gateway fills in a default, so set it yourself for long answers or with thinking on. With thinking on, thinking tokens count toward it
messages array Yes The conversation, alternating user and assistant
messages[].role string Yes user or assistant
messages[].content string or array Yes Plain text can be a string. With images or tool results, use an array of content blocks such as text, image, tool_use and tool_result
system string or array No System prompt. The Messages format has no system role; the system prompt goes here
stream boolean No true returns an SSE stream; see Streaming
thinking object No Extended thinking. Newer models use adaptive thinking, {"type": "adaptive"}, with the strength in output_config.effort; older models use {"type": "enabled", "budget_tokens": 2048}, where budget_tokens must be less than max_tokens. Anthropic's documentation says which model supports which form; the gateway forwards it unchanged
tools array No Tool definitions, each with name, description and input_schema (JSON Schema); see Tool use. For server tools such as web search, see Web search
tool_choice object No Whether and how to use tools, for example {"type": "auto"} or {"type": "tool", "name": "get_weather"}
temperature, top_p, top_k number No Sampling parameters. Some models don't accept them with thinking on
stop_sequences array No Stop generating when one of these strings appears

Images

Images go in the content blocks of a user message, each image and each piece of text in its own block. The request body:

{
  "model": "claude-sonnet-4-6",
  "max_tokens": 1024,
  "messages": [{
    "role": "user",
    "content": [
      {"type": "text", "text": "What is in this image?"},
      {
        "type": "image",
        "source": {"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo…"}
      }
    ]
  }]
}

data is the image's base64 encoding without a data:image/png;base64, prefix, and media_type is the image's actual format (image/jpeg, image/png, image/gif or image/webp). An image can also be an address reachable from the public internet: write source as {"type": "url", "url": "https://example.com/photo.jpg"}. One message can hold several images. Images count toward input tokens.

Request examples

curl

curl https://codaapi.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": 1024,
    "messages": [{"role": "user", "content": "Hello, Claude"}]
  }'

Python SDK

pip install anthropic

import anthropic

# You can also pass no arguments and set the ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY environment variables instead
client = anthropic.Anthropic(
    base_url="https://codaapi.com",
    api_key="sk-your-key",
)

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello, Claude"}],
)
print(message.content[0].text)

Set base_url to the root address without /v1. Otherwise the request becomes /v1/v1/messages and returns 404.

Node.js SDK

npm install @anthropic-ai/sdk

import Anthropic from '@anthropic-ai/sdk';

// Without baseURL and apiKey, the SDK reads the ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY environment variables
const client = new Anthropic({
  baseURL: 'https://codaapi.com',
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const message = await client.messages.create({
  model: 'claude-sonnet-4-6',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello, Claude' }],
});
console.log(message.content[0].text);

baseURL is also the root address, without /v1.

Response

{
  "id": "msg_01…",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-4-6",
  "content": [{"type": "text", "text": "Hello! How can I help?"}],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 15,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0
  }
}
Field Description
content Array of content blocks. A normal answer is text; with thinking on, thinking blocks come first; to call a tool there is a tool_use block (with id, name and input)
stop_reason Why generation stopped: end_turn (finished normally), max_tokens (cut off at the limit), tool_use (waiting for you to run the tool and send back the result), stop_sequence and so on
usage.input_tokens, usage.output_tokens Input and output token counts
usage.cache_creation_input_tokens, usage.cache_read_input_tokens Tokens written to and read from the prompt cache (non-zero only when the request uses cache_control)

Streaming

Add "stream": true to the request body, and the response arrives piece by piece in Anthropic's event format:

event: message_start
data: {"type":"message_start","message":{"id":"msg_01…","role":"assistant","content":[],…}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":15}}

event: message_stop
data: {"type":"message_stop"}

Text is in the text_delta of content_block_delta events, and thinking is in thinking_delta. The SDK's streaming method is easier:

with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a four-line poem"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Tool use

Describe your functions in tools (name, description, input_schema). When the model needs one, it returns a tool_use block, and you run the function:

curl https://codaapi.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": 1024,
    "tools": [{
      "name": "get_weather",
      "description": "Get the current weather in a given location",
      "input_schema": {
        "type": "object",
        "properties": {"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}},
        "required": ["location"]
      }
    }],
    "messages": [{"role": "user", "content": "What is the weather like in San Francisco?"}]
  }'

When the model wants to call a tool, stop_reason is tool_use and content holds a tool_use block:

{
  "content": [
    {"type": "tool_use", "id": "toolu_01…", "name": "get_weather", "input": {"location": "San Francisco, CA"}}
  ],
  "stop_reason": "tool_use"
}

After running the function, add the model's reply unchanged to messages as an assistant message, then add a user message whose content is a tool_result block (with tool_use_id matching the id of the tool_use block), and request again with the same tools:

{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_01…", "content": "Sunny, 18 °C"}]}

To make the model call a tool, or a specific one, use tool_choice; see Request body.

Web search is a Claude server tool: add a web_search item to tools, and the search runs upstream, so you don't implement it yourself. max_uses limits how many searches one request may run.

curl https://codaapi.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": 1024,
    "messages": [{"role": "user", "content": "How do I update a web app to TypeScript 5.5?"}],
    "tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}]
  }'

The returned content holds, in order, server_tool_use blocks (the searches the model started), web_search_tool_result blocks (the pages found) and text blocks with citations; usage.server_tool_use.web_search_requests is the number of searches. Whether it works depends on the model and the channel behind it. Besides the tokens, each search is billed separately; see Pricing and multipliers.

Available models

Claude model names look like claude-sonnet-4-6, claude-opus-4-7 and claude-haiku-5-5. Older versions carry a date, for example claude-haiku-4-5-20251001. For the full list, use the models marked with the Anthropic endpoint type in Model Square. Different groups offer different models; see Groups.

Models with a -thinking suffix

Some Claude model names in Model Square end in -thinking, for example claude-sonnet-4-6-thinking and claude-opus-4-6-thinking. This is a form the gateway provides, not an official Anthropic model name. It means "this model, with extended thinking on":

  • The gateway strips the suffix, sends the base model name (for example claude-sonnet-4-6) upstream, and fills in the thinking parameters for you: {"type": "adaptive"} on models that support adaptive thinking, and {"type": "enabled"} on the others, with budget_tokens set to a share of max_tokens (a max_tokens that is too small is raised).
  • The response includes the thinking (thinking blocks).
  • If a thinking you set in the request conflicts with the suffix, the model name wins. Sampling parameters the model doesn't accept with thinking on (for example temperature and top_k) are removed.
  • Billing uses the price listed for that name in Model Square.

For example, the request below doesn't set thinking; the suffixed model name turns thinking on. It also leaves out max_tokens, which the gateway fills in:

curl https://codaapi.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-thinking",
    "messages": [{"role": "user", "content": "How many r letters are in the word strawberry?"}]
  }'

The returned content starts with the thinking block, followed by the answer:

{
  "content": [
    {"type": "thinking", "thinking": "Let me spell it out: s-t-r-a-w-b-e-r-r-y…", "signature": "…"},
    {"type": "text", "text": "There are 3 r's in strawberry."}
  ],
  "stop_reason": "end_turn"
}

Models with the -thinking suffix are only in some groups (Claude-官转, Claude-AWS); see Groups.

To control the thinking mode and strength yourself, use the model name without the suffix and set thinking in the request. These suffixes also work in the OpenAI format; see Claude via the OpenAI format.

Errors

Errors that happen after the request passes the entry checks and enters the relay flow come back in the Claude format. Besides the upstream rejecting the request, this includes failures before anything is sent upstream, such as a balance too low for the pre-charge (403), the key's own quota running short (403), a model whose price isn't configured yet (400), or a missing required field such as messages (400):

{
  "type": "error",
  "error": {"type": "invalid_request_error", "message": "… (request id: 20261130…)"}
}

Errors stopped at the entry checks come back as an OpenAI-format error body ({"error": {"message": …}}), for example an invalid key, a source IP that isn't on the whitelist, a model this key can't use, an unavailable group, a model that isn't in the group, or no available channel. The Anthropic SDK raises both formats as exceptions. For both formats, see Error formats; for what to do about each status code, see Troubleshooting.

Billing

Charged at this model's price in Model Square, based on the usage in the response; prompt-cache writes and reads are priced separately, and when web search is used, the searches are billed separately as well. For the rules, see Pricing and multipliers; for each group's prices, see the price list.

What isn't supported yet

  • POST /v1/messages/count_tokens (token counting) isn't supported yet. When Claude Code can't reach this endpoint, it falls back to estimating by character count. This doesn't affect use; the numbers shown by /context are just estimates.
  • Anthropic's official Files, Batches and similar endpoints aren't provided on the gateway.

You can also call Claude models in the OpenAI format; see Claude via the OpenAI format.

Updated