API 文件

API 文件

透過 API Key 呼叫全部 AI 能力,按量計費,幾行程式碼即可接入。

認證方式

所有 API 請求需在請求標頭中攜帶 Authorization 請求標頭。

Authorization: Bearer sk-xxxxxxxxxxxxxxxx

API Key 在控制台「API 金鑰」頁面取得,一個帳號對應一組金鑰。如需重設,可在控制台操作,重設後舊金鑰立即失效。

建立任務

POST https://nsfwrouter.xyz/api/v1/tasks/create

提交一個 AI 任務到 GPU 叢集執行。任務為非同步處理,提交後返回任務 ID,透過查詢介面或 Webhook 取得結果。

請求參數
參數 類型 必填 說明
tool_idint工具 ID,可從工具列表取得
paramsobject任務參數,JSON 物件,具體欄位取決於工具
webhook_urlstring任務完成後回呼通知網址
priorityint任務優先級 1-10,預設 5
idempotency_keystring冪等鍵,防止重複提交
execution_optionsobject執行選項,JSON 物件
請求範例
curl -X POST https://nsfwrouter.xyz/api/v1/tasks/create \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_id": 1,
    "params": {
      "image": "https://example.com/photo.jpg",
      "prompt": "a beautiful landscape"
    },
    "webhook_url": "https://your-server.com/webhook"
  }'
回應範例
{
  "code": 0,
  "message": "success",
  "data": {
    "task": {
      "id": 123,
      "status": 0,
      "consume_coins": 5
    },
    "message": "task_submitted"
  }
}

呼叫前會校驗帳戶餘額,餘額不足返回 insufficient_coins 錯誤。任務提交成功後金幣立即扣除。

查詢任務詳情

GET https://nsfwrouter.xyz/api/v1/tasks/query

透過任務 ID 查詢任務詳情和狀態。任務處理中時會同步拉取 GPU 叢集最新狀態,10 秒內重複查詢直接返回資料庫結果,不重複請求遠端。

查詢參數
參數 類型 必填 說明
idint任務 ID
回應範例
{
  "code": 0,
  "message": "success",
  "data": {
    "task": {
      "id": 123,
      "status": 1,
      "task_output_json": [
        {
          "type": "image",
          "url": "https://cdn.example.com/outputs/abc123.png",
          "file_name": "abc123.png",
          "file_size": 1024000,
          "mime_type": "image/png",
          "width": 1024,
          "height": 1024
        }
      ],
      "consume_coins": 5,
      "created_at": 1718200000,
      "completed_at": 1718200015
    }
  }
}
任務狀態值
status 含義
0處理中
1已完成
-1已取消
-2失敗

任務列表

GET https://nsfwrouter.xyz/api/v1/tasks

分頁查詢當前帳號的任務列表,支援按工具、狀態和時間範圍篩選。

查詢參數
參數 類型 必填 說明
pageint頁碼,預設 1
page_sizeint每頁數量,預設 20,最大 50
tool_idint按工具篩選
statusint按狀態篩選(0/1/-1/-2)
daysint查詢天數範圍,預設 30
回應範例
{
  "code": 0,
  "message": "success",
  "data": {
    "list": [
      {
        "id": 123,
        "tool_id": 1,
        "status": 1,
        "task_output_json": [
          {
            "type": "image",
            "url": "https://cdn.example.com/outputs/abc123.png",
            "file_name": "abc123.png",
            "file_size": 1024000,
            "mime_type": "image/png",
            "width": 1024,
            "height": 1024
          }
        ],
        "error": null,
        "consume_coins": 5,
        "created_at": 1718200000,
        "completed_at": 1718200015
      },
      {
        "id": 122,
        "tool_id": 3,
        "status": 0,
        "task_output_json": [],
        "error": null,
        "consume_coins": 10,
        "created_at": 1718199000,
        "completed_at": 0
      }
    ],
    "page": 1,
    "page_size": 20
  }
}
task_output_json item 欄位說明
欄位 類型 說明
typestring結果類型:image 或 video
urlstring結果檔案 CDN 網址
file_namestring檔案名稱
file_sizeint檔案大小(位元組)
mime_typestringMIME 類型,如 image/png、video/mp4
widthint寬度(像素)
heightint高度(像素)

帳戶餘額

GET https://nsfwrouter.xyz/api/v1/account/balance

查詢當前帳號的金幣餘額、累計儲值和累計消費。

回應範例
{
  "code": 0,
  "message": "success",
  "data": {
    "remain_coins": 5000,
    "total_coins": 10000,
    "used_coins": 5000
  }
}

Webhook 回呼

建立任務時若傳入 webhook_url,任務完成、失敗或取消後會向該網址發送 POST 請求,請求體為 JSON 格式。

回呼請求標頭
Content-Type: application/json
Accept: application/json
User-Agent: OpenAPI-Webhook/1.0
回呼請求體
{
  "event": "task.finished",
  "task": {
    "id": 123,
    "status": 1,
    "task_output_json": [
      {
        "type": "image",
        "url": "https://cdn.example.com/outputs/abc123.png",
        "file_name": "abc123.png",
        "file_size": 1024000,
        "mime_type": "image/png",
        "width": 1024,
        "height": 1024
      }
    ],
    "consume_coins": 5,
    "created_at": 1718200000,
    "completed_at": 1718200015
  }
}

你的伺服器需回傳 HTTP 2xx 狀態碼表示接收成功。若回傳非 2xx 或逾時(10 秒),系統會自動重試,最多 5 次,採用指數退避策略(60s → 120s → 240s → 480s → 960s)。

錯誤碼

所有錯誤回應統一格式:{"code": 錯誤碼, "message": "錯誤標識", "data": {}}

錯誤碼 message 說明
20001api_key_required未提供 API Key
20001invalid_api_keyAPI Key 無效
20001ip_not_allowedIP 不在白名單內
20001permission_denied無權存取該資源
30001tool_not_found工具不存在
30001task_not_found任務不存在
30001insufficient_coins金幣餘額不足
30001task_submit_failed任務提交失敗
30001task_query_failed任務查詢失敗
30001task_already_exists任務已存在(冪等命中)
40001tool_id_required缺少 tool_id 參數
40001param_error參數錯誤
40001webhook_url_invalidwebhook_url 格式不正確
40001params_must_be_json_object_or_arrayparams 必須是 JSON 物件或陣列
40001execution_options_must_be_json_object_or_arrayexecution_options 必須是 JSON 物件或陣列
錯誤碼範圍
範圍 分類
1xxxx系統級錯誤
2xxxx鑑權相關
3xxxx業務邏輯錯誤
4xxxx參數錯誤
5xxxx外部依賴錯誤