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

# Messages

> PipeLLM 上兼容 Anthropic 的 Messages API

认证和模型列表请参考 [概述](/api-reference/anthropic/overview)。

## Endpoint

```
POST https://api.pipellm.ai/v1/messages
```

<Note>
  这条 free route 只接受 Anthropic Messages 格式请求，并只路由到 Anthropic
  兼容平台。如果你想保留 Anthropic 格式去调用 OpenAI 或 Gemini 模型，请使用
  [Anthropic Format Converter](/converter/anthropic-format.zh)。
</Note>

## 安装 SDK

<Tabs>
  <Tab title="Python">
    ```bash theme={null}
    pip install anthropic
    ```
  </Tab>

  <Tab title="Node.js">
    ```bash theme={null}
    npm install @anthropic-ai/sdk
    ```
  </Tab>

  <Tab title="Go">
    ```bash theme={null}
    go get github.com/anthropics/anthropic-sdk-go
    ```
  </Tab>
</Tabs>

## 代码示例

<Tabs>
  <Tab title="cURL">
    ```bash theme={null}
    curl https://api.pipellm.ai/v1/messages \
      -H "Content-Type: application/json" \
      -H "x-api-key: $PIPELLM_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -d @- << 'EOF'
    {
      "model": "claude-sonnet-4-5-20250929",
      "max_tokens": 1024,
      "messages": [
        {"role": "user", "content": "Why is the sky blue?"}
      ]
    }
    EOF
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    import os
    import anthropic

    client = anthropic.Anthropic(
      api_key=os.getenv('PIPELLM_API_KEY'),
      base_url='https://api.pipellm.ai'
    )

    message = client.messages.create(
      model='claude-sonnet-4-5-20250929',
      max_tokens=1024,
      messages=[
        {'role': 'user', 'content': 'Why is the sky blue?'}
      ]
    )

    print(message.content[0].text)
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    import Anthropic from '@anthropic-ai/sdk';

    const client = new Anthropic({
      apiKey: process.env.PIPELLM_API_KEY,
      baseURL: 'https://api.pipellm.ai'
    });

    const message = await client.messages.create({
      model: 'claude-sonnet-4-5-20250929',
      max_tokens: 1024,
      messages: [
        { role: 'user', content: 'Why is the sky blue?' }
      ]
    });

    console.log(message.content[0].text);
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    package main

    import (
      "context"
      "os"
      "github.com/anthropics/anthropic-sdk-go"
      "github.com/anthropics/anthropic-sdk-go/option"
    )

    func main() {
      client := anthropic.NewClient(
        option.WithAPIKey(os.Getenv("PIPELLM_API_KEY")),
        option.WithBaseURL("https://api.pipellm.ai"),
      )

      message, _ := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
        Model:     anthropic.F("claude-sonnet-4-5-20250929"),
        MaxTokens: anthropic.Int(1024),
        Messages: anthropic.F([]anthropic.MessageParam{
          anthropic.NewUserMessage(anthropic.NewTextBlock("Why is the sky blue?")),
        }),
      })
    }
    ```
  </Tab>
</Tabs>

## 请求参数

| 参数            | 类型      | 必需 | 描述                                    |
| ------------- | ------- | -- | ------------------------------------- |
| `model`       | string  | 是  | 模型 ID（如 `claude-sonnet-4-5-20250929`） |
| `messages`    | array   | 是  | 消息对象数组                                |
| `max_tokens`  | integer | 是  | 最大生成 token 数                          |
| `temperature` | number  | 否  | 采样温度（0-1）                             |
| `stream`      | boolean | 否  | 启用流式响应                                |

## 响应格式

```json theme={null}
{
  "id": "msg_xxx",
  "type": "message",
  "role": "assistant",
  "content": [
    {"type": "text", "text": "The sky appears blue because..."}
  ],
  "model": "claude-sonnet-4-5-20250929",
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 10,
    "output_tokens": 50
  }
}
```

## 相关文档

<Columns cols={3}>
  <Card title="路由与协议" icon="route" href="/guides/routing-protocols.zh">
    查看原生路由约束和 converter 路由
  </Card>

  <Card title="Anthropic 概述" icon="message" href="/api-reference/anthropic/overview.zh">
    查看 Header、模型和路由族说明
  </Card>

  <Card title="Anthropic Format Converter" icon="shuffle" href="/converter/anthropic-format.zh">
    保留 Anthropic 格式，调用其他协议族模型
  </Card>
</Columns>
