XaporiaXAPORIA
← Xaporiaに戻る

DEVELOPER API

Developer APIガイド

XaporiaアカウントとUSDウォレットで、統一Gatewayを通じて有効なモデルを利用できます。

Base URL

API Base URL:https://xaporia.com/v1

APIキーの取得

ログイン後、Dashboard → Developer API Keysで作成します。完全なキーは一度のみ表示し、保存はSHA-256ハッシュのみです。サーバー環境変数で管理し、コードやブラウザーに置かないでください。

認証と冪等性

使用する認証:Authorization: Bearer YOUR_XAPORIA_API_KEY。同じ論理リクエストの再試行で課金リクエストを重複させないため、安定したキーを生成・再利用してください:Idempotency-Key。OpenAIクライアント互換性のため任意です。未指定の再試行は新規リクエストとして課金される場合があります。同一キーの再送は409となり、再呼び出し・再請求・元応答の再生は行いません。異なるパラメーターでの再利用も競合になります。

Chat Completionsリクエスト

POST /chat/completions。system/user/assistantのテキスト、temperature、top_p、max_tokens/max_completion_tokens、streamに対応します。1回64メッセージ・1 MiBまでです。現在stop、frequency_penalty、presence_penalty、ツール、多モーダル入力には非対応です。非対応のサンプリング指定には明示的なエラーを返します。

curl https://xaporia.com/v1/chat/completions   -H 'Authorization: Bearer xap_你的密钥'   -H 'Content-Type: application/json'   -H 'Idempotency-Key: order-123-attempt-1'   -d '{"model":"xaporia-mock","messages":[{"role":"user","content":"Hello, Xaporia!"}]}'

JavaScript / TypeScript(OpenAI SDK):

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAPORIA_API_KEY,
  baseURL: "https://xaporia.com/v1",
});

// Reuse this value when retrying the same logical request.
const idempotencyKey = crypto.randomUUID();
const completion = await client.chat.completions.create({
  model: "xaporia-mock",
  messages: [{ role: "user", content: "Hello, Xaporia!" }],
  stream: false,
}, { headers: { "Idempotency-Key": idempotencyKey } });
console.log(completion.choices[0]?.message.content, completion.usage);

Pythonの例

Python標準ライブラリで非ストリーミングリクエストを送信:

import json
import os
import urllib.request

payload = {"model": "xaporia-mock", "messages": [{"role": "user", "content": "你好"}]}
request = urllib.request.Request(
    "https://xaporia.com/v1/chat/completions",
    data=json.dumps(payload).encode(),
    headers={
        "Authorization": "Bearer " + os.environ["XAPORIA_API_KEY"],
        "Content-Type": "application/json",
        "Idempotency-Key": "unique-key-for-this-logical-request",
    },
)
with urllib.request.urlopen(request) as response:
    print(json.load(response))

同じリクエストの再試行では冪等キーを再利用し、新規リクエストには別のキーを使用します。例のモデルは形式説明用です。利用可能なモデルは/v1/modelsで取得してください。

非ストリーミング応答

{
  "id": "chatcmpl-…",
  "object": "chat.completion",
  "created": 1793500000,
  "model": "xaporia-mock",
  "choices": [{ "index": 0, "message": { "role": "assistant", "content": "Hello!" }, "finish_reason": "stop" }],
  "usage": { "prompt_tokens": 8, "completion_tokens": 3, "total_tokens": 11 }
}

ストリーミング応答

設定stream: true を指定すると返される形式:text/event-stream。内容はOpenAI Chat Completion chunk形式です。終了付近でusageを送り、最後に次を送信します:data: [DONE]。OpenAI SDKの例:

// Reuse this value when retrying the same logical request.
const idempotencyKey = crypto.randomUUID();
const stream = await client.chat.completions.create({
  model: "xaporia-mock",
  messages: [{ role: "user", content: "Count from one to three." }],
  stream: true,
  stream_options: { include_usage: true },
}, { headers: { "Idempotency-Key": idempotencyKey } });

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta.content ?? "");
  if (chunk.usage) console.log("\nUsage:", chunk.usage);
}

SSE転送形式の例:

data: {"object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":8,"completion_tokens":3,"total_tokens":11}}

data: [DONE]

モデル一覧

GET /v1/models は有効な価格を持つ公開モデルのみ表示し、提供者のルーティング情報を含みません。

curl https://xaporia.com/v1/models   -H 'Authorization: Bearer xap_你的密钥'

エラーコード

  • invalid_api_key(401):キーが未指定、無効、または無効化済みです。
  • insufficient_quota(402):ウォレットの利用可能残高が不足しています。
  • model_unavailable(404):モデルが存在しない、無効、または有効な価格がありません。
  • idempotency_conflict / idempotency_replay(409):冪等キーパラメーターの競合、または処理済みです。
  • rate_limit_exceeded(429):キーごとに1分間60リクエストまでです。
  • invalid_request(400):リクエスト形式またはパラメーターが非対応です。

課金ルール

Web ChatとDeveloper APIはUSDウォレット、AI Gateway、Billing Engineを共有します。価格記録に基づき見積額を予約し、検証済み使用量で精算して残額を解放します。残高不足時はモデルを呼び出しません。結果・使用量が不明な場合はRESERVEDのまま照合を待ち、無料とは判断しません。Mock価格はテスト専用です。