88API88API
使用指南AI 應用API 文件幫助支援
介面模組使用指南

Token 管理模組

功能說明

介面字首統一為 http(s)://<your-domain>

生產環境應使用 HTTPS 以保證認證令牌。 HTTP 僅建議用於開發環境。

使用者 API Token 的完整管理系統 。支援 Token 建立、更新、刪除、批次操作等功能。包含模型限制、IP 限制、配額管理、過期時間等精細化控制。前端 Token 頁面的核心資料來源。

🔐 使用者鑑權

獲取全部 Token

  • 介面名稱:獲取全部 Token
  • HTTP 方法:GET
  • 路徑/api/token/
  • 鑑權要求:使用者
  • 功能簡介:分頁獲取當前使用者的所有 Token 列表

💡 請求示例:

const response = await fetch('/api/token/?p=1&size=20', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  }
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": "",
  "data": {
    "items": [
      {
        "id": 1,
        "name": "API Token",
        "key": "`<YOUR_API_KEY>`",
        "status": 1,
        "remain_quota": 1000000,
        "unlimited_quota": false,
        "expired_time": 1640995200,
        "created_time": 1640908800,
        "accessed_time": 1640995000
      }
    ],
    "total": 5,
    "page": 1,
    "page_size": 20
  }
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "獲取Token列表失敗"
}

🧾 欄位說明:

  • p (數字): 頁碼,預設為 1
  • size (數字): 每頁數量,預設為 20
  • items (陣列): Token 資訊列表
  • total (數字): Token 總數
  • page (數字): 當前頁碼
  • page_size (數字): 每頁數量

搜尋 Token

  • 介面名稱:搜尋 Token
  • HTTP 方法:GET
  • 路徑/api/token/search
  • 鑑權要求:使用者
  • 功能簡介:根據關鍵詞和 Token 值搜尋使用者的 Token

💡 請求示例:

const response = await fetch('/api/token/search?keyword=api&token=sk-123', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  }
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": "",
  "data": [
    {
      "id": 1,
      "name": "API Token",
      "key": "sk-your-token-placeholder",
      "status": 1,
      "remain_quota": 1000000
    }
  ]
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "搜尋Token失敗"
}

🧾 欄位說明:

  • keyword (字串): 搜尋關鍵詞,匹配 Token 名稱
  • token (字串): Token 值搜尋,支援部分匹配

獲取單個 Token

  • 介面名稱:獲取單個 Token
  • HTTP 方法:GET
  • 路徑/api/token/:id
  • 鑑權要求:使用者
  • 功能簡介:獲取指定 Token 的詳細資訊

💡 請求示例:

const response = await fetch('/api/token/123', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  }
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": "",
  "data": {
    "id": 123,
    "name": "API Token",
    "key": "sk-your-token-placeholder",
    "status": 1,
    "remain_quota": 1000000,
    "unlimited_quota": false,
    "model_limits_enabled": true,
    "model_limits": "gpt-3.5-turbo,gpt-4",
    "allow_ips": "192.168.1.1,10.0.0.1",
    "group": "default",
    "expired_time": 1640995200,
    "created_time": 1640908800,
    "accessed_time": 1640995000
  }
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "Token不存在"
}

🧾 欄位說明:

id (數字): Token ID,透過 URL 路徑傳遞

建立 Token

  • 介面名稱:建立 Token
  • HTTP 方法:POST
  • 路徑/api/token/
  • 鑑權要求:使用者
  • 功能簡介:建立新的 API Token,支援批次建立

💡 請求示例:

const response = await fetch('/api/token/', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  },
  body: JSON.stringify({
    name: "My API Token",
    expired_time: 1640995200,
    remain_quota: 1000000,
    unlimited_quota: false,
    model_limits_enabled: true,
    model_limits: ["gpt-3.5-turbo", "gpt-4"],
    allow_ips: "192.168.1.1,10.0.0.1",
    group: "default"
  })
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": ""
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "令牌名稱過長"
}

🧾 欄位說明:

  • name (字串): Token 名稱,最大長度 30 個字元
  • expired_time (數字): 過期時間戳,-1 表示永不過期
  • remain_quota (數字): 剩餘配額
  • unlimited_quota (布林型): 是否無限配額
  • model_limits_enabled (布林型): 是否啟用模型限制
  • model_limits (陣列): 允許使用的模型列表
  • allow_ips (字串): 允許的 IP 地址,逗號分隔
  • group (字串): 所屬分組

更新 Token

  • 介面名稱:更新 Token
  • HTTP 方法:PUT
  • 路徑/api/token/
  • 鑑權要求:使用者
  • 功能簡介:更新 Token 配置,支援狀態切換和完整更新

💡 請求示例(完整更新):

const response = await fetch('/api/token/', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  },
  body: JSON.stringify({
    id: 123,
    name: "Updated Token",
    expired_time: 1640995200,
    remain_quota: 2000000,
    unlimited_quota: false,
    model_limits_enabled: true,
    model_limits: ["gpt-3.5-turbo", "gpt-4"],
    allow_ips: "192.168.1.1",
    group: "vip"
  })
});
const data = await response.json();

💡 請求示例(僅更新狀態):

const response = await fetch('/api/token/?status_only=true', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  },
  body: JSON.stringify({
    id: 123,
    status: 1
  })
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": "",
  "data": {
    "id": 123,
    "name": "Updated Token",
    "status": 1
  }
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "令牌已過期,無法啟用,請先修改令牌過期時間,或者設定為永不過期"
}

🧾 欄位說明:

  • id (數字): Token ID,必填
  • status_only (查詢引數): 是否僅更新狀態
  • 其他欄位與建立 Token 介面相同,均為可選

刪除 Token

  • 介面名稱:刪除 Token
  • HTTP 方法:DELETE
  • 路徑/api/token/:id
  • 鑑權要求:使用者
  • 功能簡介:刪除指定的 Token

💡 請求示例:

const response = await fetch('/api/token/123', {
  method: 'DELETE',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  }
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": ""
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "Token不存在"
}

🧾 欄位說明:

id (數字): Token ID,透過 URL 路徑傳遞

批次刪除 Token

  • 介面名稱:批次刪除 Token
  • HTTP 方法:POST
  • 路徑/api/token/batch
  • 鑑權要求:使用者
  • 功能簡介:批次刪除多個 Token

💡 請求示例:

const response = await fetch('/api/token/batch', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_user_token',
    'New-Api-User': 'your_user_id'
  },
  body: JSON.stringify({
    ids: [1, 2, 3, 4, 5]
  })
});
const data = await response.json();

✅ 成功響應示例:

{
  "success": true,
  "message": "",
  "data": 5
}

❗ 失敗響應示例:

{
  "success": false,
  "message": "引數錯誤"
}

🧾 欄位說明:

  • ids (陣列): 要刪除的 Token ID 列表,必填且不能為空
  • data (數字): 成功刪除的 Token 數量