身分驗證
使用你的 API 金鑰,以 Bearer 權杖驗證 API 請求。
所有 API 請求都需要使用 Bearer 權杖進行驗證。請從你的儀表板產生一組 API 金鑰,並將其加入 Authorization 標頭。
注意:使用 API 需要 Hobby 方案以上,且帳單狀態為有效。
產生 API 金鑰
- 前往你的團隊工作區儀表板
- 在側邊欄點選 API 金鑰
- 點選 建立 API 金鑰
- 複製產生的金鑰
使用你的 API 金鑰
將金鑰加入 Authorization 標頭:
curl -X POST 'https://your-domain.com/api/v1/chat' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"chatbotId": "uuid", "messages": [{"role": "user", "content": "Hello"}]}'
標頭格式
Authorization: Bearer YOUR_API_KEY
測試你的金鑰
透過驗證端點確認你的 API 金鑰是否可用:
curl 'https://your-domain.com/api/v1/auth' \ -H 'Authorization: Bearer YOUR_API_KEY'
成功回應(200):
{
"id": "account-uuid",
"name": "Your Workspace",
"email": "[email protected]"
}
金鑰無效(401):
{
"message": "Missing or invalid Authorization header"
}
API 金鑰安全性
最佳做法
- 絕不在用戶端程式碼中暴露金鑰:API 金鑰只能用於伺服器端
- 使用環境變數:不要把金鑰寫死在程式碼中
- 定期輪換金鑰:刪除舊金鑰並建立新金鑰
- 使用具描述性的名稱:清楚掌握每組金鑰的用途
金鑰格式
金鑰是 32 個隨機位元組,以 base64url 編碼而成。
管理 API 金鑰
檢視所有金鑰
所有金鑰都會顯示在 工作區 > API 金鑰 頁面。
刪除金鑰
移除你不再需要的金鑰:
- 前往 API 金鑰
- 找到該金鑰
- 點選 刪除
- 確認刪除
已刪除的金鑰會立即失效。
錯誤回應
| 狀態碼 | 訊息 | 原因 |
|---|---|---|
| 401 | Missing or invalid Authorization header | 未提供標頭 |
| 401 | Invalid API key | 金鑰不正確 |
| 403 | API access requires a Hobby plan or above with active billing | 方案或帳單狀態不允許使用 API |
| 403 | Chatbot does not belong to this account | 代理程式 ID 不屬於該 API 金鑰所屬的帳戶 |
環境設定
Node.js
const API_KEY = process.env.AGENTKIT_API_KEY;
const response = await fetch('https://your-domain.com/api/v1/chat', {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
chatbotId: 'your-chatbot-id',
messages: [{ role: 'user', content: 'Hello' }],
}),
});
Python
import os
import requests
API_KEY = os.environ.get('AGENTKIT_API_KEY')
response = requests.post(
'https://your-domain.com/api/v1/chat',
headers={
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json',
},
json={
'chatbotId': 'your-chatbot-id',
'messages': [{'role': 'user', 'content': 'Hello'}],
},
)
cURL
export AGENTKIT_API_KEY="your-api-key"
curl -X POST 'https://your-domain.com/api/v1/chat' \
-H "Authorization: Bearer $AGENTKIT_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"chatbotId": "uuid", "messages": [{"role": "user", "content": "Hello"}]}'