跳轉到主要內容

概覽

此端點提供原生的 Anthropic Messages API 相容性。對於具備延伸思考等功能的 Claude 模型,請使用此端點。 此端點會保持 Anthropic 原生契約。messages 必須是 user / assistant 訊息陣列,system 應放在頂層 system 欄位,且 max_tokens 為必填。如果 payload 在 messages 中使用 OpenAI 的 systemdevelopertool role,請改送 /v1/chat/completions
Anthropic SDK 的 Base URL:https://api.tokenlab.sh(不含 /v1 後綴)

請求標頭

x-api-key
string
必填
您的 TokenLab API 金鑰。可作為 Bearer token 的替代方案。
anthropic-version
string
必填
Anthropic API 版本。請使用 2023-06-01

請求主體

model
string
必填
Claude 模型 ID(例如:claude-sonnet-4-6claude-opus-4-6)。
messages
array
必填
訊息物件陣列,包含 rolecontent
max_tokens
integer
必填
要生成的最大 token 數量。
system
string
System prompt(與 messages 陣列分開)。
temperature
number
預設值:"1"
取樣溫度(0-1)。
stream
boolean
預設值:"false"
啟用串流回應。
thinking
object
延伸思考設定(Claude Opus 4.5)。
  • type(string):設為 "enabled" 以啟用
  • budget_tokens(integer):思考可使用的 token 預算
tools
array
模型可用的工具。
tool_choice
object
模型應如何使用工具。選項:autoanytool(指定工具)。
top_p
number
Nucleus sampling 參數。請使用 temperature 或 top_p 其中之一,不要同時使用。
top_k
integer
每個 token 僅從前 K 個選項中取樣。
stop_sequences
array
自訂停止序列,當出現時模型將停止生成。
metadata
object
附加到請求上的中繼資料,用於追蹤目的。

回應

id
string
唯一的訊息識別碼。
type
string
一律為 message
role
string
一律為 assistant
content
array
內容區塊陣列(text、thinking、tool_use)。
model
string
所使用的模型。
stop_reason
string
生成停止的原因(end_turnmax_tokenstool_use)。
usage
object
Token 使用情況,包含 input_tokensoutput_tokens
curl -X POST "https://api.tokenlab.sh/v1/messages" \
  -H "x-api-key: sk-your-api-key" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 1024,
    "system": "You are a helpful assistant.",
    "messages": [
      {"role": "user", "content": "Hello, Claude!"}
    ]
  }'
from anthropic import Anthropic

client = Anthropic(
    api_key="sk-your-api-key",
    base_url="https://api.tokenlab.sh"
)

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system="You are a helpful assistant.",
    messages=[
        {"role": "user", "content": "Hello, Claude!"}
    ]
)

print(message.content[0].text)
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: 'sk-your-api-key',
  baseURL: 'https://api.tokenlab.sh'
});

const message = await client.messages.create({
  model: 'claude-sonnet-4-6',
  max_tokens: 1024,
  system: 'You are a helpful assistant.',
  messages: [
    { role: 'user', content: 'Hello, Claude!' }
  ]
});

console.log(message.content[0].text);
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    payload := map[string]interface{}{
        "model":      "claude-sonnet-4-6",
        "max_tokens": 1024,
        "system":     "You are a helpful assistant.",
        "messages": []map[string]string{
            {"role": "user", "content": "Hello, Claude!"},
        },
    }

    jsonData, _ := json.Marshal(payload)
    req, _ := http.NewRequest("POST", "https://api.tokenlab.sh/v1/messages", bytes.NewBuffer(jsonData))
    req.Header.Set("x-api-key", "sk-your-api-key")
    req.Header.Set("anthropic-version", "2023-06-01")
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, _ := client.Do(req)
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    fmt.Println(string(body))
}
<?php
$payload = [
    'model' => 'claude-sonnet-4-6',
    'max_tokens' => 1024,
    'system' => 'You are a helpful assistant.',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello, Claude!']
    ]
];

$ch = curl_init('https://api.tokenlab.sh/v1/messages');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'x-api-key: sk-your-api-key',
        'anthropic-version: 2023-06-01',
        'Content-Type: application/json'
    ],
    CURLOPT_POSTFIELDS => json_encode($payload)
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo $data['content'][0]['text'];
{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! How can I help you today?"
    }
  ],
  "model": "claude-sonnet-4-6",
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 15,
    "output_tokens": 10
  }
}

圖片輸入示例

對於支援視覺能力的 Claude 模型,請將圖片放在 messages[].content 中,作為結構化圖片內容區塊傳入。
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "請描述這張圖片。"
        },
        {
          "type": "image",
          "source": {
            "type": "url",
            "url": "https://example.com/demo.jpg"
          }
        }
      ]
    }
  ]
}
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "請描述這張圖片。"
        },
        {
          "type": "image",
          "source": {
            "type": "base64",
            "media_type": "image/jpeg",
            "data": "/9j/4AAQSkZJRgABAQ..."
          }
        }
      ]
    }
  ]
}

延伸思考範例

message = client.messages.create(
    model="claude-opus-4-6",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000
    },
    messages=[{"role": "user", "content": "Solve this math problem..."}]
)

for block in message.content:
    if block.type == "thinking":
        print(f"Thinking: {block.thinking}")
    elif block.type == "text":
        print(f"Response: {block.text}")

Anthropic 訊息批次

TokenLab 現在除了 /v1/messages 之外,也提供原生 Anthropic Message Batches 流程。 可用路由:
  • POST /v1/messages/batches
  • GET /v1/messages/batches
  • GET /v1/messages/batches/:message_batch_id
  • GET /v1/messages/batches/:message_batch_id/results
  • POST /v1/messages/batches/:message_batch_id/cancel
  • DELETE /v1/messages/batches/:message_batch_id
使用說明:
  • 使用同一把 TokenLab API key 與 Anthropic 原生請求標頭。
  • 如果 batch item 引用了 file_id,也要加上 anthropic-beta: files-api-2025-04-14
  • Batch job 會維持 Anthropic 原生請求/回應格式,同時 TokenLab 會追蹤其內部結算生命週期。