> ## Documentation Index
> Fetch the complete documentation index at: https://docs.toapis.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Messages API

> Chat with Claude models using the native Anthropic Messages API format

> [!NOTE]
>
> **中国本土のユーザー向け：** 中国本土のユーザーは `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

> [!NOTE]
>
> If you are already using the OpenAI SDK, use the [OpenAI-compatible endpoint](https://docs.toapis.com/docs/ja/api-reference/chat/chat.md) instead.
> If you are using the Anthropic SDK or Claude Code, this endpoint is recommended.

## Authorizations

**Authorization** `string`

Bearer token authentication for direct HTTP calls

```
Authorization: Bearer YOUR_API_KEY
```

**x-api-key** `string`

API key authentication, compatible with the Anthropic SDK

```
x-api-key: YOUR_API_KEY
```

**anthropic-version** `string` (default: 2023-06-01)

Anthropic API version. The Anthropic SDK sets this automatically.

Recommended: `2023-06-01`

## Body

**model** `string` *required*

モデル name

All Claude models are supported, for example:

* `claude-opus-4-6`
* `claude-sonnet-4-6`
* `claude-haiku-4-5`

**messages** `object[]` *required*

Conversation messages in chronological order. Only `user` and `assistant` roles are allowed here — use the top-level `system` field for system prompts.

**messages\[n]**

**role** `string` *required*

Message role: `user` or `assistant`

**content** `string | object[]` *required*

Message content as a string or an array of content blocks

**Content block**

**type** `string` *required*

Content type: `text` or `image`

**text** `string`

Text content. 必須 when `type` is `text`.

**source** `object`

Image source. 必須 when `type` is `image`.

**source**

**type** `string` *required*

Source type: `base64` or `url`

**media\_type** `string`

MIME type, required for `base64`. E.g. `image/jpeg`, `image/png`

**data** `string`

Base64-encoded image data. 必須 when `type` is `base64`.

**url** `string`

Image URL. 必須 when `type` is `url`.

**max\_tokens** `integer` *required*

Maximum number of tokens to generate

* Claude Sonnet 4-6: up to `64000`
* Claude Opus 4-6: up to `32000`

**system** `string | object[]`

System prompt, set at the top level (not inside `messages`)

Accepts a plain string or an array of content blocks.

**stream** `boolean` (default: false)

Enable streaming output (Server-Sent Events)

* `true`: tokens streamed incrementally following the Anthropic SSE event format
* `false`: full response returned at once

**temperature** `number` (default: 1)

Sampling temperature controlling output randomness

Range: `0` – `1`

**top\_p** `number`

Nucleus sampling threshold

Range: `0` – `1`. Avoid setting both `temperature` and `top_p` simultaneously.

**stop\_sequences** `string[]`

Stop sequences — generation stops when any of these strings is produced

## レスポンス

**id** `string`

Unique identifier for the request, prefixed with `msg_`

**type** `string`

Object type, always `message`

**role** `string`

レスポンス role, always `assistant`

**content** `object[]`

List of generated content blocks

* `content[].type`: content type, typically `text`
* `content[].text`: generated text

**model** `string`

The model that handled the request

**stop\_reason** `string`

Reason generation stopped

* `end_turn`: model finished naturally
* `max_tokens`: `max_tokens` limit reached
* `stop_sequence`: a stop sequence was triggered

**usage** `object`

Token usage for this request

* `usage.input_tokens`: input token count
* `usage.output_tokens`: output token count

```bash cURL theme={null}
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"
      }
    ]
  }'
```

```bash cURL (system prompt) theme={null}
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"
      }
    ]
  }'
```

```bash cURL (streaming) theme={null}
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"
      }
    ]
  }'
```

```python Python (Anthropic SDK) theme={null}
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)
```

```python Python (with system prompt) theme={null}
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)
```

```python Python (streaming) theme={null}
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)
```

```javascript JavaScript (Anthropic SDK) theme={null}
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);
```

```javascript JavaScript (streaming) theme={null}
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);
  }
}
```

```json 200 theme={null}
{
  "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
  }
}
```

```json 200 (streaming SSE) theme={null}
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"}
```

```json 400 theme={null}
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "messages: field required"
  }
}
```

```json 401 theme={null}
{
  "type": "error",
  "error": {
    "type": "authentication_error",
    "message": "Invalid API key"
  }
}
```

```json 429 theme={null}
{
  "type": "error",
  "error": {
    "type": "rate_limit_error",
    "message": "Rate limit exceeded"
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.