API Documentation

API Docs

Call all AI capabilities via API Key, pay-as-you-go, integrate with just a few lines of code.

Authentication

All API requests must include an Authorization header.

Authorization: Bearer sk-xxxxxxxxxxxxxxxx

Get your API Key from the Console "API Keys" page. One key per account. To reset, use the console; the old key is immediately invalidated after reset.

Create Task

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

Submit an AI task to the GPU cluster for execution. Tasks are processed asynchronously, returning a task ID. Get results via the query API or Webhook.

Request Parameters
Parameter Type Required Description
tool_idintYesTool ID, obtained from the tools list
paramsobjectYesTask parameters, JSON object, fields depend on the tool
webhook_urlstringNoCallback URL for task completion notification
priorityintNoTask priority 1-10, default 5
idempotency_keystringNoIdempotency key, prevents duplicate submissions
execution_optionsobjectNoExecution options, JSON object
Request Example
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"
  }'
Response Example
{
  "code": 0,
  "message": "success",
  "data": {
    "task": {
      "id": 123,
      "status": 0,
      "consume_coins": 5
    },
    "message": "task_submitted"
  }
}

Account balance is verified before calling. Insufficient balance returns insufficient_coins error. Coins are deducted immediately upon successful task submission.

Query Task Detail

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

Query task details and status by task ID. When the task is processing, the latest status is fetched from the GPU cluster. Repeated queries within 10 seconds return cached database results without remote requests.

Query Parameters
Parameter Type Required Description
idintYesTask ID
Response Example
{
  "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
    }
  }
}
Task Status Values
status Meaning
0Processing
1Completed
-1Cancelled
-2Failed

Task List

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

Paginated query of the current account's task list, supports filtering by tool, status and time range.

Query Parameters
Parameter Type Required Description
pageintNoPage number, default 1
page_sizeintNoPage size, default 20, max 50
tool_idintNoFilter by tool
statusintNoFilter by status (0/1/-1/-2)
daysintNoQuery time range in days, default 30
Response Example
{
  "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 field descriptions
Field Type Description
typestringResult type: image or video
urlstringResult file CDN URL
file_namestringFile name
file_sizeintFile size (bytes)
mime_typestringMIME type, e.g. image/png, video/mp4
widthintWidth (pixels)
heightintHeight (pixels)

Account Balance

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

Query the current account's coin balance, total recharge and total consumption.

Response Example
{
  "code": 0,
  "message": "success",
  "data": {
    "remain_coins": 5000,
    "total_coins": 10000,
    "used_coins": 5000
  }
}

Webhook Callback

If webhook_url is provided when creating a task, a POST request is sent to that URL when the task completes, fails or is cancelled. The request body is JSON format.

Callback Request Headers
Content-Type: application/json
Accept: application/json
User-Agent: OpenAPI-Webhook/1.0
Callback Request Body
{
  "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
  }
}

Your server must return an HTTP 2xx status code to indicate successful receipt. If a non-2xx response or timeout (10 seconds) occurs, the system will automatically retry up to 5 times with exponential backoff (60s → 120s → 240s → 480s → 960s).

Error Codes

All error responses use a unified format: {"code": error_code, "message": "error_identifier", "data": {}}

Error Code message Description
20001api_key_requiredAPI Key not provided
20001invalid_api_keyInvalid API Key
20001ip_not_allowedIP not in whitelist
20001permission_deniedPermission denied
30001tool_not_foundTool not found
30001task_not_foundTask not found
30001insufficient_coinsInsufficient coin balance
30001task_submit_failedTask submission failed
30001task_query_failedTask query failed
30001task_already_existsTask already exists (idempotency hit)
40001tool_id_requiredMissing tool_id parameter
40001param_errorParameter error
40001webhook_url_invalidInvalid webhook_url format
40001params_must_be_json_object_or_arrayparams must be a JSON object or array
40001execution_options_must_be_json_object_or_arrayexecution_options must be a JSON object or array
Error Code Ranges
Range Category
1xxxxSystem-level error
2xxxxAuthentication error
3xxxxBusiness logic error
4xxxxParameter error
5xxxxExternal dependency error