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

# Claude 消息接口

> Claude Messages API 兼容格式，适用于 Claude Code 与 Claude SDK

使用 Claude Messages API 兼容格式调用 Claude 模型。Claude Code、Claude SDK 或直接 HTTP 请求都可以通过该接口接入使用 Claude Messages API 兼容格式调用 Claude 模型。Claude Code、Claude SDK 或直接 HTTP 请求都可以通过该接口接入。

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://gogogotoken.ai/v1/messages \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "anthropic-version: 2023-06-01" \
    -d '{
      "model": "claude-sonnet-4-20250514",
      "max_tokens": 1024,
      "messages": [
        { "role": "user", "content": "用三点说明 API 网关的作用。" }
      ]
    }'
  ```

  ```bash 流式 theme={null}
  curl -N -X POST https://gogogotoken.ai/v1/messages \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "anthropic-version: 2023-06-01" \
    -d '{
      "model": "claude-sonnet-4-20250514",
      "max_tokens": 1024,
      "messages": [
        { "role": "user", "content": "Write a haiku about the sea." }
      ],
      "stream": true
    }'
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "msg_abc123",
    "type": "message",
    "role": "assistant",
    "model": "claude-sonnet-4-20250514",
    "content": [
      {
        "type": "text",
        "text": "API 网关可以统一鉴权、稳定路由，并集中记录用量。"
      }
    ],
    "stop_reason": "end_turn",
    "usage": {
      "input_tokens": 24,
      "output_tokens": 18
    }
  }
  ```

  ```text 流式 theme={null}
  event: message_start
  data: {"type":"message_start","message":{"id":"msg_abc123","type":"message"}}

  event: content_block_delta
  data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"API 网关"}}

  event: message_stop
  data: {"type":"message_stop"}
  ```
</ResponseExample>

## 接口说明

<Steps>
  <Step title="创建 API Key">
    在控制台 [令牌管理](/billing/api-key-management) 中创建以 `sk-` 开头的令牌，并确保所选分组**包含目标 Claude 模型**。
  </Step>

  <Step title="设置环境变量">
    ```bash theme={null}
    export ANTHROPIC_BASE_URL="https://gogogotoken.ai"
    export ANTHROPIC_API_KEY="YOUR_API_KEY"
    ```
  </Step>

  <Step title="启动 Claude Code">
    在 IDE 插件 / CLI 中确认 Base URL 已生效，发送一次测试请求验证模型可用。
  </Step>
</Steps>

该接口会自动识别 `/v1/messages` 路径与 `claude-*` 模型，并路由到 Claude 专用通道。Claude Code 使用时，Base URL 只填写 `https://gogogotoken.ai`，客户端会自动拼接 `/v1/messages`。

<Info>
  完整模型列表以 `GET /v1/models` 返回结果和控制台分组配置为准。
</Info>

## 请求头

<ParamField header="Authorization" type="string" required>
  API Key 鉴权信息，格式为 `Bearer YOUR_API_KEY`。
</ParamField>

<ParamField header="Content-Type" type="string" required>
  固定为 `application/json`。
</ParamField>

<ParamField header="anthropic-version" type="string">
  Claude Messages API 版本，常用值为 `2023-06-01`。
</ParamField>

## 请求体

<ParamField body="model" default="claude-sonnet-4-20250514" type="string" required>
  Claude 模型 ID，例如 `claude-sonnet-4-20250514`。
</ParamField>

<ParamField body="messages" default={[{role:"user",content:"用三点说明 API 网关的作用。"}]} type="object[]" required>
  对话消息数组。

  <ParamField body="role" type="string" required>
    消息角色，通常为 `user` 或 `assistant`。
  </ParamField>

  <ParamField body="content" type="string" required>
    消息内容。文本可直接传字符串；多模态内容可按 Claude Messages 格式传数组。
  </ParamField>
</ParamField>

<ParamField body="max_tokens" default={1024} type="integer" required>
  最大输出 token 数。
</ParamField>

<ParamField body="stream" default={false} type="boolean">
  是否使用流式返回，默认 `false`。
</ParamField>

<ParamField body="temperature" type="number">
  采样温度。
</ParamField>

<ParamField body="metadata.user_id" type="string">
  可选的用户标识。网关可基于该字段保持同一会话的上游路由稳定。
</ParamField>

## 响应体

<ResponseField name="id" type="string">
  本次消息响应 ID。
</ResponseField>

<ResponseField name="type" type="string">
  对象类型，通常为 `message`。
</ResponseField>

<ResponseField name="content" type="object[]">
  模型输出内容数组，文本内容通常在 `content[0].text`。
</ResponseField>

<ResponseField name="stop_reason" type="string">
  生成停止原因，例如 `end_turn`、`max_tokens`。
</ResponseField>

<ResponseField name="usage" type="object">
  token 用量统计，通常包含 `input_tokens` 与 `output_tokens`。
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://gogogotoken.ai/v1/messages \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "anthropic-version: 2023-06-01" \
    -d '{
      "model": "claude-sonnet-4-20250514",
      "max_tokens": 1024,
      "messages": [
        { "role": "user", "content": "用三点说明 API 网关的作用。" }
      ]
    }'
  ```

  ```bash 流式 theme={null}
  curl -N -X POST https://gogogotoken.ai/v1/messages \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -H "anthropic-version: 2023-06-01" \
    -d '{
      "model": "claude-sonnet-4-20250514",
      "max_tokens": 1024,
      "messages": [
        { "role": "user", "content": "Write a haiku about the sea." }
      ],
      "stream": true
    }'
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "msg_abc123",
    "type": "message",
    "role": "assistant",
    "model": "claude-sonnet-4-20250514",
    "content": [
      {
        "type": "text",
        "text": "API 网关可以统一鉴权、稳定路由，并集中记录用量。"
      }
    ],
    "stop_reason": "end_turn",
    "usage": {
      "input_tokens": 24,
      "output_tokens": 18
    }
  }
  ```

  ```text 流式 theme={null}
  event: message_start
  data: {"type":"message_start","message":{"id":"msg_abc123","type":"message"}}

  event: content_block_delta
  data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"API 网关"}}

  event: message_stop
  data: {"type":"message_stop"}
  ```
</ResponseExample>

## 接口说明

<Steps>
  <Step title="创建 API Key">
    在控制台 [令牌管理](/billing/api-key-management) 中创建以 `sk-` 开头的令牌，并确保所选分组**包含目标 Claude 模型**。
  </Step>

  <Step title="设置环境变量">
    ```bash theme={null}
    export ANTHROPIC_BASE_URL="https://gogogotoken.ai"
    export ANTHROPIC_API_KEY="YOUR_API_KEY"
    ```
  </Step>

  <Step title="启动 Claude Code">
    在 IDE 插件 / CLI 中确认 Base URL 已生效，发送一次测试请求验证模型可用。
  </Step>
</Steps>

该接口会自动识别 `/v1/messages` 路径与 `claude-*` 模型，并路由到 Claude 专用通道。Claude Code 使用时，Base URL 只填写 `https://gogogotoken.ai`，客户端会自动拼接 `/v1/messages`。

<Info>
  完整模型列表以 `GET /v1/models` 返回结果和控制台分组配置为准。
</Info>

## 请求头

<ParamField header="Authorization" type="string" required>
  API Key 鉴权信息，格式为 `Bearer YOUR_API_KEY`。
</ParamField>

<ParamField header="Content-Type" type="string" required>
  固定为 `application/json`。
</ParamField>

<ParamField header="anthropic-version" type="string">
  Claude Messages API 版本，常用值为 `2023-06-01`。
</ParamField>

## 请求体

<ParamField body="model" default="claude-sonnet-4-20250514" type="string" required>
  Claude 模型 ID，例如 `claude-sonnet-4-20250514`。
</ParamField>

<ParamField body="messages" default={[{role:"user",content:"用三点说明 API 网关的作用。"}]} type="object[]" required>
  对话消息数组。

  <ParamField body="role" type="string" required>
    消息角色，通常为 `user` 或 `assistant`。
  </ParamField>

  <ParamField body="content" type="string" required>
    消息内容。文本可直接传字符串；多模态内容可按 Claude Messages 格式传数组。
  </ParamField>
</ParamField>

<ParamField body="max_tokens" default={1024} type="integer" required>
  最大输出 token 数。
</ParamField>

<ParamField body="stream" default={false} type="boolean">
  是否使用流式返回，默认 `false`。
</ParamField>

<ParamField body="temperature" type="number">
  采样温度。
</ParamField>

<ParamField body="metadata.user_id" type="string">
  可选的用户标识。网关可基于该字段保持同一会话的上游路由稳定。
</ParamField>

## 响应体

<ResponseField name="id" type="string">
  本次消息响应 ID。
</ResponseField>

<ResponseField name="type" type="string">
  对象类型，通常为 `message`。
</ResponseField>

<ResponseField name="content" type="object[]">
  模型输出内容数组，文本内容通常在 `content[0].text`。
</ResponseField>

<ResponseField name="stop_reason" type="string">
  生成停止原因，例如 `end_turn`、`max_tokens`。
</ResponseField>

<ResponseField name="usage" type="object">
  token 用量统计，通常包含 `input_tokens` 与 `output_tokens`。
</ResponseField>

## 错误码错误码

| 状态码   | 含义含义                       | 处理建议建议                     |
| ----- | -------------------------- | -------------------------- |
| `401` | API Key 无效或未传API Key 无效或未传 | 检查请求头与令牌状态请求头与令牌状态         |
| `403` | 无权限访问目标 Claude 模型          | 检查 API Key 分组权限            |
| `403` | 无权限访问目标 Claude 模型          | 检查 API Key 分组权限            |
| `429` | 触发限速                       | 降低请求频率或调整降低请求频率或调整套餐       |
| `5xx` | 上游或网关或网关异常                 | 稍后重试，并查看控制台日志稍后重试，并查看控制台日志 |
