跳转到主要内容
Endpoint: POST /messages 通过 Messages API 向 Claude 发送消息。
大多数集成仍应优先使用 Create Chat Completion。只有当你明确需要 Anthropic 原生 Messages 请求格式时,才使用这个接口。
import requests

response = requests.post(
    "https://api.yourouter.ai/v1/messages",
    headers={
        "Authorization": "Bearer YOUR_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())

参数

model
string
必填
目标模型 ID。
messages
array
必填
使用 Claude Messages 格式传入的对话历史。
stream
boolean
默认值:"false"
若为 true,则按 SSE 方式返回结果。

图片输入

对于 Claude 视觉模型,请在 messages 数组中发送图片内容块。
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>"
            }
          }
        ]
      }
    ]
  }'
更完整的集成方式请参考 多模态指南