身份驗證

身份驗證

Lawbot AI API 使用 API 金鑰進行身份驗證。每個請求都必須在 HTTP Header 中 附帶有效的 Bearer Token。

驗證方式

所有 API 請求均需在 Authorization Header 中附帶 API 金鑰,格式為 Bearer Token:

Authorization: Bearer YOUR_API_KEY

cURL 範例

terminal
curl http://127.0.0.1:8000/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "model": "lawbot-pro", "messages": [...] }'

Python 安全實踐

強烈建議透過環境變數傳遞 API 金鑰,避免金鑰外洩至版本控制系統:

.env
# .env
LAWBOT_API_KEY=your_api_key_here
client.py
import os
from openai import OpenAI

# 建議從環境變數讀取,避免硬編碼金鑰
client = OpenAI(
    api_key=os.environ.get("LAWBOT_API_KEY"),
    base_url="http://127.0.0.1:8000",
)

Node.js / TypeScript 安全實踐

client.ts
import OpenAI from "openai";

const client = new OpenAI({
  // 建議從環境變數讀取
  apiKey: process.env.LAWBOT_API_KEY,
  baseURL: "http://127.0.0.1:8000",
});

驗證失敗錯誤

若 API 金鑰無效或遺失,API 將回傳 HTTP 401 狀態碼:

error_response.json
{
  "error": {
    "message": "Invalid API key provided.",
    "type": "authentication_error",
    "code": "invalid_api_key"
  }
}

安全建議

  • 絕對不要在前端程式碼或公開的程式庫中暴露 API 金鑰
  • 使用環境變數(.env 檔案)儲存金鑰,並將 .env 加入 .gitignore
  • 定期輪換 API 金鑰,降低洩漏風險
  • 若金鑰意外洩漏,請立即聯絡 Lawbot 團隊進行停用