ToAPIsToAPIs
Anthropic 형식

Anthropic Messages API

Chat with Claude models using the native Anthropic Messages API format

POST
/v1/messages

중국 본토 사용자 안내: 중국 본토 사용자는 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_KEY
x-api-keystring헤더

API key authentication, compatible with the Anthropic SDK

x-api-key: YOUR_API_KEY
anthropic-versionstring헤더기본값: 2023-06-01

Anthropic 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-6
  • claude-sonnet-4-6
  • claude-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본문기본값: false

Enable streaming output (Server-Sent Events)

  • true: tokens streamed incrementally following the Anthropic SSE event format
  • false: full response returned at once
temperaturenumber본문기본값: 1

Sampling 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

응답

idstring

Unique identifier for the request, prefixed with msg_

typestring

Object type, always message

rolestring

응답 role, always assistant

contentobject[]

List of generated content blocks

  • content[].type: content type, typically text
  • content[].text: generated text
modelstring

The model that handled the request

stop_reasonstring

Reason generation stopped

  • end_turn: model finished naturally
  • max_tokens: max_tokens limit reached
  • stop_sequence: a stop sequence was triggered
usageobject

Token usage for this request

  • usage.input_tokens: input token count
  • usage.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"
  }
}