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

参数

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>"
            }
          }
        ]
      }
    ]
  }'

PDF 输入

对于支持 PDF 的 Claude 模型,可以使用 document 内容块,并把 source.media_type 设置为 application/pdfdata 字段传入 PDF 原始字节的 base64 编码内容。
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."
          }
        ]
      }
    ]
  }'
Claude PDF 能力依赖具体模型。请使用标准、未加密的 PDF,并控制请求体大小和页数;大文档建议拆分后分批处理。
更完整的集成方式请参考 多模态指南