API Overview
API 總覽
The Claude API is a RESTful API at https://api.anthropic.com that provides programmatic access to Claude models.
Claude API 是位於 https://api.anthropic.com 的 RESTful API,提供對 Claude 模型的程式化存取。
Prerequisites
先決條件
To use the Claude API, you'll need:
要使用 Claude API,你需要:
- An Anthropic Console account
- An API key
- 一個 Anthropic Console 帳戶
- 一個 API 金鑰
Available APIs
可用的 API
The Claude API includes the following APIs:
Claude API 包含以下 API:
General Availability:
正式版(GA):
- Messages API: Send messages to Claude for conversational interactions (POST /v1/messages)
- Message Batches API: Process large volumes of Messages requests asynchronously with 50% cost reduction
- Token Counting API: Count tokens in a message before sending to manage costs and rate limits
- Models API: List available Claude models and their details
- Messages API:向 Claude 發送訊息進行對話互動(POST /v1/messages)
- Message Batches API:非同步處理大量 Messages 請求,可節省 50% 成本
- Token Counting API:發送前計算訊息中的 token 數量,以管理成本和速率限制
- Models API:列出可用的 Claude 模型及其詳細資訊
Beta:
測試版(Beta):
- Files API: Upload and manage files for use across multiple API calls
- Skills API: Create and manage custom agent skills
- Files API:上傳和管理可跨多個 API 呼叫使用的檔案
- Skills API:建立和管理自訂 agent 技能
Authentication
驗證
All requests to the Claude API must include these headers:
所有對 Claude API 的請求都必須包含以下標頭:
| Header | Value | Required |
|---|---|---|
| x-api-key | Your API key from Console | Yes |
| anthropic-version | API version (e.g., 2023-06-01) | Yes |
| content-type | application/json | Yes |
| 標頭 | 值 | 必填 |
|---|---|---|
| x-api-key | 從 Console 取得的 API 金鑰 | 是 |
| anthropic-version | API 版本(例如 2023-06-01) | 是 |
| content-type | application/json | 是 |
Client SDKs
Client SDKs
Anthropic provides official SDKs that simplify API integration by handling authentication, request formatting, error handling, and more.
Anthropic 提供官方 SDK,透過處理驗證、請求格式化、錯誤處理等功能來簡化 API 整合。
Benefits:
優點:
- Automatic header management (x-api-key, anthropic-version, content-type)
- Type-safe request and response handling
- Built-in retry logic and error handling
- Streaming support
- Request timeouts and connection management
- 自動管理標頭(x-api-key、anthropic-version、content-type)
- 型別安全的請求和回應處理
- 內建重試邏輯和錯誤處理
- 串流支援
- 請求逾時和連線管理
from anthropic import Anthropic
client = Anthropic() # Reads ANTHROPIC_API_KEY from environment
message = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude"}]
)
Claude API vs Third-Party Platforms
Claude API vs 第三方平台
Claude is available through Anthropic's direct API and through partner platforms. Choose based on your infrastructure, compliance requirements, and pricing preferences.
Claude 可透過 Anthropic 的直接 API 和合作夥伴平台使用。請根據你的基礎架構、合規要求和定價偏好做選擇。
| Platform | Provider | Documentation |
|---|---|---|
| Amazon Bedrock | AWS | Claude on Amazon Bedrock |
| Vertex AI | Google Cloud | Claude on Vertex AI |
| Azure AI | Microsoft Azure | Claude on Azure AI |
| 平台 | 供應商 | 文件 |
|---|---|---|
| Amazon Bedrock | AWS | Claude on Amazon Bedrock |
| Vertex AI | Google Cloud | Claude on Vertex AI |
| Azure AI | Microsoft Azure | Claude on Azure AI |
Request Size Limits
請求大小限制
| Endpoint | Maximum Size |
|---|---|
| Standard endpoints (Messages, Token Counting) | 32 MB |
| Batch API | 256 MB |
| Files API | 500 MB |
| 端點 | 最大大小 |
|---|---|
| 標準端點(Messages、Token Counting) | 32 MB |
| Batch API | 256 MB |
| Files API | 500 MB |
Basic Example
基本範例
Here's a minimal request using the Messages API:
以下是使用 Messages API 的最小範例請求:
curl https://api.anthropic.com/v1/messages \
--header "x-api-key: $ANTHROPIC_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "content-type: application/json" \
--data '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Claude"}
]
}'