Tài liệu API
Một API key, hai gateway: OpenAI và Anthropic
APIHub cung cấp gateway non-streaming cho Chat Completions (OpenAI) và Messages (Anthropic). Tài liệu và danh sách model bên dưới lấy trực tiếp từ database; key trong ví dụ chỉ là placeholder.
Base URL
OpenAI dùng Authorization: Bearer; Anthropic dùng x-api-key (Bearer cũng được chấp nhận). Không gửi key thật trong tài liệu hoặc mã nguồn công khai.
https://apihub-api.apihubvn.workers.devOpenAI — Chat Completions
Endpoint POST /v1/chat/completions chỉ phục vụ model có endpoint type OpenAI. Response giữ định dạng OpenAI. Thay ah_live_REPLACE_WITH_YOUR_KEY bằng key tạo trong dashboard.
curl -X POST "https://apihub-api.apihubvn.workers.dev/v1/chat/completions" \
-H "Authorization: Bearer ah_live_REPLACE_WITH_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "your-model",
"messages": [
{"role": "user", "content": "Xin chào APIHub"}
],
"stream": false
}'Anthropic — Messages
Endpoint POST /v1/messages chỉ phục vụ model có endpoint type Anthropic. Request và response giữ định dạng Anthropic Messages; usage đọc từ input_tokens / output_tokens.
- • Gửi key bằng
x-api-keyhoặcAuthorization: Bearer. - • Giữ header
anthropic-version: 2023-06-01. - • Bắt buộc non-streaming: bỏ
streamhoặc đặtstream: false. - • Có thể gửi
Idempotency-Keyđể retry không bị trừ tiền hai lần.
curl -X POST "https://apihub-api.apihubvn.workers.dev/v1/messages" \
-H "x-api-key: ah_live_REPLACE_WITH_YOUR_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-haiku",
"max_tokens": 512,
"messages": [
{"role": "user", "content": "Xin chào APIHub"}
],
"stream": false
}'Response mock và live
Mock mode
- • Nội dung có nhãn [MOCK].
- • Header
X-ApiHub-Mode: mock. - • Token ước lượng, lưu tokenSource ESTIMATED.
Live mode
- • Chuyển tiếp tới upstream bằng key server-side.
- • Header
X-ApiHub-Mode: live. - • Upstream lỗi trả 502, không fallback mock.
Danh sách model
Dùng đúng slug trong trường model. Giá chi tiết xem tại bảng giá.
Lỗi thường gặp
| HTTP | Code | Ý nghĩa |
|---|---|---|
| 400 | invalid_request / stream_not_supported | Body sai hoặc dùng streaming. |
| 400 | model_not_supported | Model không hỗ trợ endpoint bạn đang gọi. |
| 401 | missing_api_key / invalid_api_key | Thiếu, sai hoặc key đã thu hồi. |
| 402 | insufficient_balance | Số dư phải lớn hơn 0. |
| 403 | account_banned | Tài khoản đang bị khóa. |
| 404 | model_not_found | Model không tồn tại hoặc đang tắt. |
| 429 | rate_limit_exceeded | Vượt giới hạn mỗi phút của key. |
| 429 | daily_cost_limit_exceeded | Vượt hạn mức chi phí trong ngày của key. |
| 502 | upstream_error | Upstream lỗi; gateway không tự chuyển mock. |