# 對話訊息 (Anthropic)

Anthropic 相容路徑：`POST /v1/messages`，適合已串接 Anthropic SDK 或原生協定的業務。[Messages API](https://docs.anthropic.com/en/api/messages)

* **路徑**: `https://www.tokenpad.ai/v1/messages`
* **驗證**: `x-api-key: sk-your_token_here`
* **必填 Header**: `anthropic-version: 2023-06-01`
* **`max_tokens`**: 必填

多模態時 `content` 為陣列；圖片使用 `type: image`，**僅支援 base64**（`source.type = base64`），不支援 URL。

---

## 1. 純文字

```bash
curl -X POST https://www.tokenpad.ai/v1/messages \
  -H "x-api-key: sk-your_token_here" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-6",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "你好，請用一句話介紹你自己。"}
    ]
  }'
```

---

## 2. 多模態：圖片 + 文字（Base64）

`data` 為**純 base64**（請勿帶有 `data:image/...;base64,` 前綴）。

```bash
curl -X POST https://www.tokenpad.ai/v1/messages \
  -H "x-api-key: sk-your_token_here" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-6",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/jpeg",
              "data": "<raw-base64>"
            }
          },
          { "type": "text", "text": "這張圖裡有什麼？簡要說明。" }
        ]
      }
    ]
  }'
```

`media_type`：`image/jpeg`、`image/png`、`image/gif`、`image/webp`。

---

## 3. 多模態：多圖對比

繼續追加多個 base64 `image`，最後接續 `text`：

```bash
curl -X POST https://www.tokenpad.ai/v1/messages \
  -H "x-api-key: sk-your_token_here" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-6",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/png",
              "data": "<raw-base64-a>"
            }
          },
          {
            "type": "image",
            "source": {
              "type": "base64",
              "media_type": "image/png",
              "data": "<raw-base64-b>"
            }
          },
          { "type": "text", "text": "對比這兩張圖的差異。" }
        ]
      }
    ]
  }'
```

---

## 4. 常用參數

| 參數 | 型別 | 必填 | 說明 |
| :--- | :--- | :--- | :--- |
| `model` | string | 是 | Claude 模型 ID |
| `messages` | array | 是 | `content`：字串，或 `[text / image(base64)]` |
| `max_tokens` | integer | 是 | 最大生成長度 |
| `stream` | boolean | 否 | 預設 `false` |
| `temperature` | number | 否 | 0~1 |
| `system` | string | 否 | 系統提示（頂層欄位，不在 messages 內） |

SDK：官方 `anthropic`，`base_url="https://www.tokenpad.ai"`（會自動拼接 `/v1`），`api_key` 填寫本平台 Token。

> OpenAI 相容聊天（`/v1/chat/completions`）請見選單「聊天 Chat」。



### 5. 常見錯誤碼與問題排解

HTTP 狀態碼、錯誤主體格式及分情境排解已 **統一維護** 於專頁，避免與各呼叫範例重複導致不同步。

👉 **[閘道常見錯誤碼與排解](/docs/zh-TW/errors/error-codes)**

**速記**（詳情見專頁）：

| 碼 | 含義 |
| :---: | :--- |
| **400** | 參數無效 / 內容風控或版權攔截 |
| **402** | 帳戶餘額不足（**不是** 429） |
| **429** | 權杖 RPS/RPM 頻率限制，或低餘額在途任務過多 |
| **403** | 權杖/IP/模型權限（餘額見 402；內容攔截見 400） |
| **200 + `status: failed`** | 非同步影片等長任務的業務失敗（輪詢時常見） |

HTTP 回應碼與控制台呼叫日誌 `status_code` 一致，可依 `log_id` 追溯。

> **維護約定**：新增或調整錯誤說明時，請只更新專頁 `error-codes`；本附錄透過連結自動指向最新內容。
