APAI

API reference

APAI speaks the OpenAI protocol, so existing SDKs work unchanged.

Quickstart

Your base URL is this server's address followed by /v1:

—

Create an API key on the dashboard, then make a request:

—

Python (official OpenAI SDK)

from openai import OpenAI

client = OpenAI(
    base_url="…",
    api_key="apai_YOUR_KEY",
)

response = client.chat.completions.create(
    model="gpt-5.4-nano",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)

JavaScript

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "…",
  apiKey: "apai_YOUR_KEY",
});

const response = await client.chat.completions.create({
  model: "gpt-5.4-nano",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);
Using the API is covered by the Terms of Service, and the Privacy Policy explains what this server keeps: your usage records and costs, but not the text of your API requests.

Authentication

Every /v1 request needs an API key in the Authorization header. Keys start with apai_.

Authorization: Bearer apai_YOUR_KEY
Keys spend your account balance. Each key can also carry its own budget cap, which stops that key from spending more than the cap even if the account still has money.

Not the same thing as your access code. The access code signs you into this website; API keys are for programs.

List models

GET/v1/models

Returns every model you are allowed to use, with its pricing.

The same catalog is browsable in the browser on the models page — ids, owners and prices, no request needed.

curl …/v1/models \
  -H "Authorization: Bearer apai_YOUR_KEY"
{
  "object": "list",
  "data": [
    {
      "id": "gpt-5.4-nano",
      "object": "model",
      "owned_by": "OpenAI",
      "context_window": 64000,
      "pricing": {
        "input_usd_per_1m_tokens": 0.27,
        "output_usd_per_1m_tokens": 1.1,
        "per_image_usd": 0
      }
    }
  ]
}
A model is priced one way or the other: chat models charge per token through the two *_tokens fields, image models charge per_image_usd for every image they produce.
Use the id field as the model value in requests. The administrator can enable or disable models at any time.

Chat completions

POST/v1/chat/completions
FieldTypeDescription
modelstringRequired. A model id from /v1/models.
messagesarrayRequired. {role, content} objects; roles are system, user, assistant.
streambooleanServer-sent events instead of one JSON body.
max_tokensintegerCaps the reply length. Also caps the amount reserved from your balance.
temperaturenumber0–2.
top_pnumberNucleus sampling.
stopstring / arrayStop sequences.

Any other field you send is forwarded to the provider untouched, so provider-specific options work without a server change.

curl …/v1/chat/completions \
  -H "Authorization: Bearer apai_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-nano",
    "messages": [
      {"role": "system", "content": "You are concise."},
      {"role": "user", "content": "Explain DNS in one sentence."}
    ],
    "max_tokens": 100
  }'
{
  "id": "chatcmpl-…",
  "object": "chat.completion",
  "model": "gpt-5.4-nano",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "DNS maps names to IPs." },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 24, "completion_tokens": 9, "total_tokens": 33 }
}

Streaming

Set "stream": true to receive server-sent events. Each event is a data: line containing a JSON chunk; the stream ends with data: [DONE].

curl …/v1/chat/completions \
  -H "Authorization: Bearer apai_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-nano",
    "messages": [{"role": "user", "content": "Count to five."}],
    "stream": true
  }'
data: {"choices":[{"delta":{"content":"1"}}]}

data: {"choices":[{"delta":{"content":", 2"}}]}

data: {"choices":[{"delta":{},"finish_reason":"stop"}]}

data: {"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20}}

data: [DONE]
The final chunk before [DONE] carries real token usage. APAI bills from it, so your balance is charged for exactly what was generated.

Images

Image models are billed per generated image, not per token. The price is per image, so a request for n images costs n × the model's per_image_usd. You are only charged for images the provider actually returns.

Generate an image

POST/v1/images/generations
FieldTypeDescription
modelstringRequired. An image model id from /v1/models.
promptstringRequired. What to draw.
nintegerHow many images, 1–10. Defaults to 1 and is what your balance is reserved against.
sizestringe.g. 1024x1024. Which sizes exist is up to the model.
response_formatstringb64_json (often the default) or url. Whatever the model returns is passed straight back to you.

Other fields are forwarded to the model untouched, so provider-specific options (quality, background, output_format, …) work without a server change. Streaming images are not supported.

You can also just try an image model in the sandbox: pick one from the dropdown (they are marked · image), type the prompt as a message and the picture appears in the chat, ready to download. That path generates one image per message; for a specific n, size or an edit of your own picture, use these endpoints.
curl …/v1/images/generations \
  -H "Authorization: Bearer apai_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "flux",
    "prompt": "a red fox in tall grass, golden hour",
    "n": 1
  }'
{
  "created": 1735689600,
  "data": [
    { "b64_json": "iVBORw0KG…" }
  ],
  "usage": { "input_tokens": 12, "output_tokens": 1056, "total_tokens": 1068 }
}

Edit an image

POST/v1/images/edits

Send the picture you want changed as multipart/form-data. The file field is image, or image[] repeated for several pictures; a mask field is passed along too if the model supports one. Every other form field is forwarded as it is.

curl …/v1/images/edits \
  -H "Authorization: Bearer apai_YOUR_KEY" \
  -F "model=flux-kontext" \
  -F "prompt=turn the sky into a starry night" \
  -F "image=@photo.png"
Only models whose type is image work here. A chat model sent to an image endpoint is refused with 400 rather than billed at token prices, and vice versa.

Responses

POST/v1/responses

The newer OpenAI Responses shape, translated to a chat completion behind the scenes, so it works with every model on this server. Billing is identical to chat: per token, from the same prices.

FieldTypeDescription
modelstringRequired. A chat model id from /v1/models.
inputstring / arrayRequired. A plain string, or a list of message items with input_text / input_image parts.
instructionsstringBecomes the system message.
max_output_tokensintegerCaps the reply, and the amount reserved from your balance.
streambooleanResponses SSE events instead of one JSON body.
toolsarrayFunction tools only ({"type":"function","name":…,"parameters":…}).
text.formatobjecttext, json_object, or json_schema.
reasoning.effortstringForwarded as reasoning_effort.
curl …/v1/responses \
  -H "Authorization: Bearer apai_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-nano",
    "input": "Explain DNS in one sentence."
  }'
{
  "id": "resp_…",
  "object": "response",
  "status": "completed",
  "model": "gpt-5.4-nano",
  "output": [
    {
      "id": "msg_…",
      "type": "message",
      "status": "completed",
      "role": "assistant",
      "content": [
        { "type": "output_text", "text": "DNS maps names to IPs.", "annotations": [] }
      ]
    }
  ],
  "output_text": "DNS maps names to IPs.",
  "usage": { "input_tokens": 12, "output_tokens": 9, "total_tokens": 21 }
}

With "stream": true the stream follows the Responses event protocol: response.created, response.in_progress, response.output_item.added, response.content_part.added, then response.output_text.delta per chunk, and finally response.output_text.done, response.content_part.done, response.output_item.done and response.completed. Each event carries its name both in the SSE event: line and in the payload's type. A failure ends the stream with error and response.failed.

This endpoint is stateless. previous_response_id, conversation, prompt, background and store: true are refused with 400 rather than ignored, because they promise stored state that this server does not keep. Send the whole conversation in input.

Usage

GET/v1/usage

Spend for the key you are calling with, plus the account behind it.

curl …/v1/usage \
  -H "Authorization: Bearer apai_YOUR_KEY"
{
  "account": { "balance_usd": 42.5 },
  "key": {
    "name": "discord-bot",
    "budget_usd": 5.0,
    "spent_usd": 0.0142,
    "remaining_usd": 4.9858
  },
  "usage": {
    "requests": 18,
    "cost_usd": 0.0142,
    "prompt_tokens": 8421,
    "completion_tokens": 3097
  }
}

Billing

APAI charges per token, using the input and output prices of the model you call.

cost = (prompt_tokens     × input_price_per_1M  / 1,000,000)
     + (completion_tokens × output_price_per_1M / 1,000,000)

Image models are priced the other way instead — per image produced:

cost = images_returned × price_per_image
  • Money comes from your single account balance, shared by all your keys and the web sandbox.
  • Before a request runs, APAI reserves the worst case cost (your prompt plus max_tokens, or n images). If the balance cannot cover it, the request is refused with 402 and nothing is charged.
  • After the reply, the difference is refunded, so you only ever pay for tokens actually produced — or for the images actually returned.
  • A failed provider call is not charged.
  • Models priced at 0 are free and never touch your balance.
  • Setting a low max_tokens both shortens replies and lowers the reserved amount.
  • If a model carries a hidden system prompt set by the administrator, its tokens are not charged to you. See below.

Hidden system prompts cost you nothing

A model can be configured with instructions that are sent with every request and that you never see. Those tokens are the administrator's, not yours, so they are subtracted from the prompt count before your price is worked out, and the usage numbers reported back to you are adjusted the same way — what you read is what you were charged. Anything you write yourself, including your own system message, is billed normally.

Errors

StatusMeaning
400Malformed request body.
401Missing or invalid API key.
402Your APAI balance is empty, or this key's budget is exhausted. Only ever an APAI billing decision.
403Key revoked/expired, account disabled, or the model is not available to you.
404Unknown model id.
413An image upload for /v1/images/edits is larger than the 25 MB limit.
429Too many login attempts (website only).
502The upstream service behind the model failed or refused the request. The status tells you it is not an APAI billing problem.
Provider errors are always reported as 502, never with the provider's own status code. If a provider answers 402 because its account is out of credit, you get a 502 — a 402 from APAI always means your own balance.
// APAI's own billing refusal
{ "detail": "Insufficient balance. Ask the administrator to top up your account." }

// A provider problem, wrapped as 502
{ "detail": "The upstream service for this model returned HTTP 402 (the provider wants payment or credits on YOUR account with them). It said: {}" }

Which upstream provider serves a model is never named anywhere you can see it — not in errors, and not in your usage history. The wording is also scrubbed, so even the upstream's own message cannot name it. Ask the administrator if you need to know.