對話訊息 (Anthropic)

Anthropic 相容路徑:POST /v1/messages,適合已串接 Anthropic SDK 或原生協定的業務。Messages API

  • 路徑: 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. 純文字

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, 前綴)。

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:

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. 常用參數

參數型別必填說明
modelstring是Claude 模型 ID
messagesarray是content:字串,或 [text / image(base64)]
max_tokensinteger是最大生成長度
streamboolean否預設 false
temperaturenumber否0~1
systemstring否系統提示(頂層欄位,不在 messages 內)

SDK:官方 anthropic,base_url="https://www.tokenpad.ai"(會自動拼接 /v1),api_key 填寫本平台 Token。

OpenAI 相容聊天(/v1/chat/completions)請見選單「聊天 Chat」。

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

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

👉 閘道常見錯誤碼與排解

速記(詳情見專頁):

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

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

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

Raw Markdown (.md) · llms.txt