# 建立 Claude 訊息

**API:** Model API

**Endpoint:** `POST /api/v1/messages`

相容於 Claude Messages API 的端點。設定 stream=true 即可接收 Claude 風格的訊息串流事件。請求、回應與驗證錯誤欄位皆遵循 Claude Messages 協定；若發生基礎設施故障，Router 也可能傳回經過清理的閘道錯誤封裝。

## Base URL

- `https://openapi.felo.ai` — 正式環境

## Authentication

- `FeloBearerAuth` (http bearer) — 以 `Authorization: Bearer <key>` 形式傳送的 Felo API 金鑰。
- `FeloApiKey` (apiKey in header `X-API-Key`) — 以 `X-API-Key: <key>` 形式傳送的 Felo API 金鑰。

## Parameters

| Name | In | Required | Type | Description |
| --- | --- | --- | --- | --- |
| `X-Felo-Channel` | header | no | string, default: "default" | 依序排列、以逗號分隔的公開頻道代碼。省略此標頭即使用 default；default 必須單獨使用。在錢包模式下，default 會反向掃描模型實際生效的公開頻道順序，略過未發布或不可用的頻道，並選用第一個可用頻道所屬的供應商。它會納入該供應商底下具有相同上游模型 ID 與服務層級的可用端點。明確指定的頻道會依用戶端指定的順序驗證；只會提交第一個頻道所屬的供應商群組。不會自動回退至其他供應商。舊版的供應商標頭無法覆寫頻道路由。 |

## Request body

Required: **yes**

### application/json

Schema: `ClaudeMessagesRequest` (`#/components/schemas/ClaudeMessagesRequest`)

```json
{
  "type": "object",
  "required": [
    "model",
    "max_tokens",
    "messages"
  ],
  "properties": {
    "model": {
      "type": "string"
    },
    "max_tokens": {
      "type": "integer",
      "minimum": 1
    },
    "messages": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/components/schemas/ClaudeMessage"
      },
      "description": "Use a document block for a PDF or an image block for a picture. Set source to {type: 'file', file_id: 'felo_file_...'} for an uploaded Felo attachment, {type: 'url', url: 'https://...'} for an HTTPS URL, or provide type, media_type and data for a base64 source. PDF and image URL support are enabled per model."
    },
    "system": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true
          }
        }
      ]
    },
    "stream": {
      "type": "boolean",
      "default": false
    },
    "temperature": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    },
    "top_p": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    },
    "top_k": {
      "type": "integer",
      "minimum": 0
    },
    "stop_sequences": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "tools": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": true
      }
    },
    "tool_choice": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "additionalProperties": true
        }
      ]
    },
    "metadata": {
      "type": "object",
      "additionalProperties": true
    },
    "thinking": {
      "type": "object",
      "additionalProperties": true
    },
    "service_tier": {
      "type": [
        "string",
        "null"
      ],
      "default": "default",
      "description": "目前僅支援 default（標準）。省略、null、別名以及不受支援的字串都會回退為 default；其他類型則傳回 HTTP 400。已設定的頻道與價格維持不變。文字 API 會傳送供應商的標準值；Decisions 與生成 API 則會省略上游欄位。"
    }
  },
  "additionalProperties": true
}
```

Example:

```json
{
  "model": "claude-sonnet-4-5",
  "max_tokens": 256,
  "messages": [
    {
      "role": "user",
      "content": "Write a one-sentence greeting."
    }
  ],
  "stream": false
}
```

## Responses

### 200 — Claude 訊息或串流回應

**application/json**

Schema: `ClaudeMessageResponse` (`#/components/schemas/ClaudeMessageResponse`)

**text/event-stream**

Schema: string

```json
{
  "type": "string",
  "description": "Claude Messages 的 SSE 事件。"
}
```

### 400 — Claude 的 invalid_request_error 或 Router 請求驗證錯誤

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

### 401 — Felo API 金鑰無效或未提供

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

### 402 — 餘額不足

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

### 403 — 此帳戶無權使用該模型。已停用的模型同樣會在上游呼叫前遭到拒絕。已受理的請求與任務將繼續執行。

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

### 429 — 已達消費或請求上限

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

### 502 — 上游供應商請求失敗

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

### 503 — 供應商或計費服務暫時無法使用

**application/json**

Schema: `ClaudeErrorResponse` (`#/components/schemas/ClaudeErrorResponse`)

## Schema definitions

### ClaudeContentBlock

`#/components/schemas/ClaudeContentBlock`

```json
{
  "type": "object",
  "required": [
    "type"
  ],
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "text",
        "image",
        "document",
        "tool_use",
        "tool_result",
        "thinking"
      ]
    },
    "text": {
      "type": "string"
    },
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "input": {
      "type": "object",
      "additionalProperties": true
    },
    "content": {
      "type": [
        "string",
        "array"
      ]
    },
    "source": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
```

### ClaudeErrorResponse

`#/components/schemas/ClaudeErrorResponse`

```json
{
  "type": "object",
  "description": "用於 Messages 請求、相容於 Claude Errors API 的錯誤封裝。",
  "required": [
    "type",
    "error"
  ],
  "properties": {
    "type": {
      "type": "string",
      "const": "error"
    },
    "error": {
      "type": "object",
      "required": [
        "type",
        "message"
      ],
      "properties": {
        "type": {
          "type": "string"
        },
        "message": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "request_id": {
      "type": "string"
    }
  },
  "additionalProperties": true
}
```

### ClaudeMessage

`#/components/schemas/ClaudeMessage`

```json
{
  "type": "object",
  "required": [
    "role",
    "content"
  ],
  "properties": {
    "role": {
      "type": "string",
      "enum": [
        "user",
        "assistant"
      ]
    },
    "content": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/components/schemas/ClaudeContentBlock"
          }
        }
      ]
    }
  },
  "additionalProperties": true
}
```

### ClaudeMessageResponse

`#/components/schemas/ClaudeMessageResponse`

```json
{
  "type": "object",
  "required": [
    "id",
    "type",
    "role",
    "content",
    "model",
    "stop_reason"
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "type": {
      "type": "string",
      "const": "message"
    },
    "role": {
      "type": "string",
      "const": "assistant"
    },
    "content": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/ClaudeContentBlock"
      }
    },
    "model": {
      "type": "string"
    },
    "stop_reason": {
      "type": [
        "string",
        "null"
      ]
    },
    "stop_sequence": {
      "type": [
        "string",
        "null"
      ]
    },
    "usage": {
      "$ref": "#/components/schemas/Usage"
    }
  },
  "additionalProperties": true
}
```

### Usage

`#/components/schemas/Usage`

```json
{
  "type": "object",
  "description": "供應商用量物件。欄位會因端點與供應商而異。",
  "additionalProperties": true
}
```
