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

# Create Message

> 通过 Anthropic 的 Messages API 与 Claude 交互。

**Endpoint:** `POST /messages`

通过 Messages API 向 Claude 发送消息。

<Note>
  大多数集成仍应优先使用 [Create Chat Completion](/zh/api-reference/chat/create)。只有当你明确需要 Anthropic 原生 Messages 请求格式时，才使用这个接口。
</Note>

<CodeGroup>
  ```python Python theme={null}
  import os
  import requests

  response = requests.post(
      "https://api.yourouter.ai/v1/messages",
      headers={
          "Authorization": f"Bearer {os.environ['YOUROUTER_API_KEY']}",
          "Content-Type": "application/json",
          "anthropic-version": "2023-06-01",
          "vendor": "anthropic"
      },
      json={
          "model": "claude-3-5-sonnet-20240620",
          "messages": [{"role": "user", "content": "Hello"}]
      }
  )
  print(response.json())
  ```

  ```bash cURL theme={null}
  curl https://api.yourouter.ai/v1/messages \
    -H "Authorization: Bearer $YOUROUTER_API_KEY" \
    -H "Content-Type: application/json" \
    -H "anthropic-version: 2023-06-01" \
    -H "vendor: anthropic" \
    -d '{"model": "claude-3-5-sonnet-20240620", "messages": [{"role": "user", "content": "Hello"}]}'
  ```
</CodeGroup>

## 参数

<ResponseField name="model" type="string" required>
  目标模型 ID。
</ResponseField>

<ResponseField name="messages" type="array" required>
  使用 Claude Messages 格式传入的对话历史。
</ResponseField>

<ResponseField name="stream" type="boolean" default="false">
  若为 true，则按 SSE 方式返回结果。
</ResponseField>

## 图片输入

对于 Claude 视觉模型，请在 `messages` 数组中发送图片内容块。

```bash theme={null}
curl https://api.yourouter.ai/v1/messages \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -H "vendor: anthropic" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 300,
    "messages": [
      {
        "role": "user",
        "content": [
          { "type": "text", "text": "Describe this image in one sentence." },
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/jpeg",
              "data": "<BASE64_IMAGE>"
            }
          }
        ]
      }
    ]
  }'
```

## PDF 输入

对于支持 PDF 的 Claude 模型，可以使用 `document` 内容块，并把 `source.media_type` 设置为 `application/pdf`。`data` 字段传入 PDF 原始字节的 base64 编码内容。

```bash theme={null}
curl https://api.yourouter.ai/v1/messages \
  -H "Authorization: Bearer $YOUROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -H "vendor: anthropic" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "base64",
              "media_type": "application/pdf",
              "data": "<BASE64_PDF>"
            }
          },
          {
            "type": "text",
            "text": "Summarize this PDF and list risks to watch."
          }
        ]
      }
    ]
  }'
```

<Note>
  Claude PDF 能力依赖具体模型。请使用标准、未加密的 PDF，并控制请求体大小和页数；大文档建议拆分后分批处理。
</Note>

更完整的集成方式请参考 [多模态指南](/zh/guides/multimodal)。
