API dokumentasyon

API dokumentasyon

Tawagan ang lahat ng AI tampok sa pamamagitan ng API key, bayad per paggamit, integrasyon sa ilang linya ng code.

Autentikasyon

Lahat ng API request ay dapat maglaman ng header Authorization.

Authorization: Bearer sk-xxxxxxxxxxxxxxxx

Kunin ang API key sa pahina ng „Mga API Key" sa konsol. Isang key kada account. Reset sa konsol; ang luma ay agad na nawalang bisa.

Gumawa ng gawain

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

Magpadala ng AI gawain sa GPU cluster. Ang mga gawain ay async, nagbabalik ng ID. Resulta sa API o Webhook.

Mga parameter ng request
Parameter Uri Kinakailangan Deskripsyon
tool_idintOoID ng tool, mula sa listahan ng tool
paramsobjectOoMga parameter ng gawain, JSON object, field ay nakadepende sa tool
webhook_urlstringHindiCallback URL para sa notification ng pagkumpleto
priorityintHindiPrioridad ng gawain 1-10, default 5
idempotency_keystringHindiIdempotency key, pinipigilan ang duplicate
execution_optionsobjectHindiMga opsyon sa pagpapatupad, JSON object
Halimbawa ng request
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"
  }'
Halimbawa ng response
{
  "code": 0,
  "message": "success",
  "data": {
    "task": {
      "id": 123,
      "status": 0,
      "consume_coins": 5
    },
    "message": "task_submitted"
  }
}

Tinitingnan ang balanse bago tawag. Kulang = error insufficient_coins. Binabawasan ang coins sa matagumpay na pagpapadala.

Query ng gawain

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

Query ng detalye at status ayon sa ID. Habang pinoproseso, ang status ay mula sa GPU cluster. Paulit-ulit na query sa loob ng 10 segundo ay nagbabalik ng DB cache.

Mga parameter ng query
Parameter Uri Kinakailangan Deskripsyon
idintOoID ng gawain
Halimbawa ng response
{
  "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
    }
  }
}
Mga halaga ng status ng gawain
status Kahulugan
0Pinoproseso
1Tapos na
-1Nakansela
-2Nabigo

Listahan ng gawain

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

Paginated na query ng mga gawain ng account, na may filter sa tool, status at panahon.

Mga parameter ng query
Parameter Uri Kinakailangan Deskripsyon
pageintHindiNumero ng pahina, default 1
page_sizeintHindiLaki ng pahina, default 20, max 50
tool_idintHindiI-filter ayon sa tool
statusintHindiI-filter ayon sa status (0/1/-1/-2)
daysintHindiPanahon sa araw, default 30
Halimbawa ng response
{
  "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
  }
}
Deskripsyon ng task_output_json field
Field Uri Deskripsyon
typestringUri ng resulta: image o video
urlstringCDN URL para sa resulta file
file_namestringPangalan ng file
file_sizeintLaki ng file (bytes)
mime_typestringMIME type, hal. image/png, video/mp4
widthintLapad (pixels)
heightintTaas (pixels)

Balanse ng account

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

Suriin ang coin balanse, kabuuang load at kabuuang konsumo.

Halimbawa ng response
{
  "code": 0,
  "message": "success",
  "data": {
    "remain_coins": 5000,
    "total_coins": 10000,
    "used_coins": 5000
  }
}

Webhook callback

Kung tinukoy ang webhook_url, POST ay ipinapadala sa pagkumpleto, kabiguan o pagkansela. Body ay JSON.

Mga header ng callback request
Content-Type: application/json
Accept: application/json
User-Agent: OpenAPI-Webhook/1.0
Body ng callback request
{
  "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
  }
}

Dapat magbalik ng HTTP 2xx ang server mo. Sa non-2xx o timeout (10 sec), sinusubukan ng sistema hanggang 5 beses na may exponential backoff.

Mga error code

Lahat ng error response ay gumagamit ng pormal na format: {"code": code, "message": "identifier", "data": {}}

Error code message Deskripsyon
20001api_key_requiredHindi ibinigay ang API key
20001invalid_api_keyHindi wastong API key
20001ip_not_allowedIP ay hindi sa whitelist
20001permission_deniedTinanggihan ang access
30001tool_not_foundHindi nahanap ang tool
30001task_not_foundHindi nahanap ang gawain
30001insufficient_coinsHindi sapat na coin balanse
30001task_submit_failedNabigo ang pagpapadala ng gawain
30001task_query_failedNabigo ang query ng gawain
30001task_already_existsMayroon nang gawain (idempotency)
40001tool_id_requiredParameter tool_id ay nawawala
40001param_errorError sa parameter
40001webhook_url_invalidHindi wastong format ng webhook_url
40001params_must_be_json_object_or_arrayparams ay dapat na JSON object o array
40001execution_options_must_be_json_object_or_arrayexecution_options ay dapat na JSON object o array
Mga saklaw ng error code
Saklaw Kategorya
1xxxxError sa antas ng sistema
2xxxxError sa autentikasyon
3xxxxError sa lohika ng negosyo
4xxxxError sa parameter
5xxxxError sa panlabas na dependency