Anthropic Messages API
Chat with Claude models using the native Anthropic Messages API format
中国本土のユーザー向け: 中国本土のユーザーは https://toapis.cn を API エンドポイント(Base URL)としてご利用ください。本ドキュメント内の例では https://toapis.com を https://toapis.cn に置き換えてください。
- Native Anthropic Messages API format
- Drop-in compatible with the official Anthropic SDK (Python / JavaScript) — just change
base_url - Supports streaming (SSE)
- Supports multi-turn conversations, system prompts, vision input, and tool use
If you are already using the OpenAI SDK, use the OpenAI-compatible endpoint instead. If you are using the Anthropic SDK or Claude Code, this endpoint is recommended.
Authorizations
AuthorizationstringヘッダーBearer token authentication for direct HTTP calls
Authorization: Bearer YOUR_API_KEYx-api-keystringヘッダーAPI key authentication, compatible with the Anthropic SDK
x-api-key: YOUR_API_KEYanthropic-versionstringヘッダーデフォルト: 2023-06-01Anthropic API version. The Anthropic SDK sets this automatically.
Recommended: 2023-06-01
Body
modelstringボディ必須モデル name
All Claude models are supported, for example:
claude-opus-4-6claude-sonnet-4-6claude-haiku-4-5
messagesobject[]ボディ必須Conversation messages in chronological order. Only user and assistant roles are allowed here — use the top-level system field for system prompts.
messages[n]
rolestringボディ必須Message role: user or assistant
contentstring | object[]ボディ必須Message content as a string or an array of content blocks
Content block
typestringボディ必須Content type: text or image
textstringボディText content. 必須 when type is text.
sourceobjectボディImage source. 必須 when type is image.
source
typestringボディ必須Source type: base64 or url
media_typestringボディMIME type, required for base64. E.g. image/jpeg, image/png
datastringボディBase64-encoded image data. 必須 when type is base64.
urlstringボディImage URL. 必須 when type is url.
max_tokensintegerボディ必須Maximum number of tokens to generate
- Claude Sonnet 4-6: up to
64000 - Claude Opus 4-6: up to
32000
systemstring | object[]ボディSystem prompt, set at the top level (not inside messages)
Accepts a plain string or an array of content blocks.
streambooleanボディデフォルト: falseEnable streaming output (Server-Sent Events)
true: tokens streamed incrementally following the Anthropic SSE event formatfalse: full response returned at once
temperaturenumberボディデフォルト: 1Sampling temperature controlling output randomness
Range: 0 – 1
top_pnumberボディNucleus sampling threshold
Range: 0 – 1. Avoid setting both temperature and top_p simultaneously.
stop_sequencesstring[]ボディStop sequences — generation stops when any of these strings is produced
レスポンス
idstringUnique identifier for the request, prefixed with msg_
typestringObject type, always message
rolestringレスポンス role, always assistant
contentobject[]List of generated content blocks
content[].type: content type, typicallytextcontent[].text: generated text
modelstringThe model that handled the request
stop_reasonstringReason generation stopped
end_turn: model finished naturallymax_tokens:max_tokenslimit reachedstop_sequence: a stop sequence was triggered
usageobjectToken usage for this request
usage.input_tokens: input token countusage.output_tokens: output token count
リクエスト例
curl --request POST \
--url https://toapis.com/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello, tell me about yourself"
}
]
}'curl --request POST \
--url https://toapis.com/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "You are a professional coding assistant specializing in Python and Go.",
"messages": [
{
"role": "user",
"content": "Write a quicksort in Python"
}
]
}'curl --request POST \
--url https://toapis.com/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": "Explain what recursion is"
}
]
}'import anthropic
client = anthropic.Anthropic(
api_key="your-ToAPIs-key",
base_url="https://toapis.com"
)
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, tell me about yourself"}
]
)
print(message.content[0].text)import anthropic
client = anthropic.Anthropic(
api_key="your-ToAPIs-key",
base_url="https://toapis.com"
)
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system="You are a professional coding assistant specializing in Python and Go.",
messages=[
{"role": "user", "content": "Write a quicksort in Python"}
]
)
print(message.content[0].text)import anthropic
client = anthropic.Anthropic(
api_key="your-ToAPIs-key",
base_url="https://toapis.com"
)
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain what recursion is"}
]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "your-ToAPIs-key",
baseURL: "https://toapis.com",
});
const message = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, tell me about yourself" }],
});
console.log(message.content[0].text);import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "your-ToAPIs-key",
baseURL: "https://toapis.com",
});
const stream = await client.messages.stream({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages: [{ role: "user", content: "Explain what recursion is" }],
});
for await (const chunk of stream) {
if (
chunk.type === "content_block_delta" &&
chunk.delta.type === "text_delta"
) {
process.stdout.write(chunk.delta.text);
}
}レスポンス例
{
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! I'm Claude, an AI assistant made by Anthropic. I can help with questions, analysis, coding, writing, and more. How can I help you today?"
}
],
"model": "claude-sonnet-4-6",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 12,
"output_tokens": 35
}
}data: {"type":"message_start","message":{"id":"msg_01abc","type":"message","role":"assistant","content":[],"model":"claude-sonnet-4-6","stop_reason":null,"usage":{"input_tokens":12,"output_tokens":0}}}
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"! I'm Claude"}}
data: {"type":"content_block_stop","index":0}
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":35}}
data: {"type":"message_stop"}{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "messages: field required"
}
}{
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
}{
"type": "error",
"error": {
"type": "rate_limit_error",
"message": "Rate limit exceeded"
}
}