Skip to content

Claude(Anthropic)接口 ​

YeahRouter 提供与 Anthropic Messages API 一致的原生接口:POST https://yeahrouter.com/v1/messages。

示例 ​

bash
curl https://yeahrouter.com/v1/messages \
  -H "x-api-key: sk-你的密钥" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<模型名>",
    "max_tokens": 1024,
    "messages": [{ "role": "user", "content": "你好" }]
  }'
python
import anthropic

client = anthropic.Anthropic(
    base_url="https://yeahrouter.com",
    api_key="sk-你的密钥",
)

msg = client.messages.create(
    model="<模型名>",
    max_tokens=1024,
    messages=[{"role": "user", "content": "你好"}],
)
print(msg.content[0].text)
ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  baseURL: 'https://yeahrouter.com',
  apiKey: process.env.YEAHROUTER_API_KEY,
})

const msg = await client.messages.create({
  model: '<模型名>',
  max_tokens: 1024,
  messages: [{ role: 'user', content: '你好' }],
})
console.log(msg.content[0].type === 'text' ? msg.content[0].text : msg.content)

TIP

Anthropic SDK 的 base_url 填 https://yeahrouter.com,不带 /v1,SDK 会自动拼接 /v1/messages。

流式输出 ​

python
with client.messages.stream(
    model="<模型名>",
    max_tokens=1024,
    messages=[{"role": "user", "content": "写一首短诗"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

扩展思考、工具调用 ​

thinking、tools、tool_choice、图片输入等参数会原样传给上游,是否支持取决于具体模型。