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

# Claude 消息接口

> 使用 Anthropic Messages API 协议调用 Claude 模型。

GregAPI 兼容 Anthropic Messages API，可直接使用官方 SDK 或 cURL 调用。

请求需携带 `anthropic-version` 头：

```http theme={null}
Authorization: Bearer <TOKEN>
anthropic-version: 2023-06-01
Content-Type: application/json
```

## 请求示例

### cURL

```bash theme={null}
curl -X POST "https://api.gregapi.com/v1/messages" \
  -H "Authorization: Bearer $TOKEN" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-6",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "你好，请用一句话自我介绍。"}
    ]
  }'
```

### Python SDK

```bash theme={null}
pip install anthropic
```

```python theme={null}
import os
import anthropic

client = anthropic.Anthropic(
    api_key=os.environ["TOKEN"],
    base_url="https://api.gregapi.com",
)

message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "你好，请用一句话自我介绍。"}],
)
print(message)
```

### TypeScript SDK

```bash theme={null}
npm install @anthropic-ai/sdk
```

```typescript theme={null}
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.TOKEN!,
  baseURL: "https://api.gregapi.com",
});

const message = await client.messages.create({
  model: "claude-opus-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "你好，请用一句话自我介绍。" }],
});
console.log(message);
```

## 构建方式

| | Messages API | Claude Managed Agents |
| - | - | - |
| **是什么** | 直接调用模型、自行编排对话与工具 | 托管基础设施上的可配置 Agent 形态 |
| **更适合** | 自定义 Agent 循环与细粒度控制每次调用 | 长时运行、异步类任务 |

## 推荐开发路径

1. **完成第一次调用**：配置环境变量，用 cURL 或 SDK 发出首条请求。
2. **理解 Messages API**：掌握 `messages` 结构、多轮对话与 `stop_reason` 等。
3. **选择模型**：通过 `GET /v1/models` 或控制台确认可用模型。
4. **扩展能力**：工具调用、视觉、结构化输出等以 Anthropic 官方文档为准。

## 开发资源

* **GregAPI 控制台**：创建与管理 API 密钥、查看调用与配额。
* **GregAPI 用户文档**：平台能力说明与多产品线导航。

网关行为以 GregAPI 控制台说明为准；Anthropic 原生能力以官方文档为准。


## OpenAPI

````yaml openapi/llm.yaml POST /v1/messages
openapi: 3.0.3
info:
  title: GregAPI 大语言模型
  version: 1.0.0
  description: GregAPI 统一网关下的大语言模型（LLM）接口，覆盖通用对话补全、多模态响应，以及各家原生消息与生成协议。
servers:
  - url: https://api.gregapi.com
security:
  - bearerAuth: []
paths:
  /v1/messages:
    post:
      summary: Claude 消息接口
      description: 使用 Anthropic Messages API 协议调用 Claude 模型，可直接使用官方 SDK 或 cURL 调用。
      parameters:
        - name: anthropic-version
          in: header
          required: true
          schema:
            type: string
            example: '2023-06-01'
          description: Anthropic API 版本
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - model
                - max_tokens
                - messages
              properties:
                model:
                  type: string
                  description: 模型名称
                max_tokens:
                  type: integer
                  description: 最大输出 token 数
                messages:
                  type: array
                  description: 对话消息数组
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                        enum:
                          - user
                          - assistant
                      content:
                        type: string
            example:
              model: claude-opus-4-6
              max_tokens: 1024
              messages:
                - role: user
                  content: 你好，请用一句话自我介绍。
      responses:
        '200':
          description: 成功返回消息结果
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.