APAI speaks the OpenAI protocol, so existing SDKs work unchanged.
Your base URL is this server's address followed by /v1:
—Create an API key on the dashboard, then make a request:
—
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)
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);
Every /v1 request needs an API key in the
Authorization header. Keys start with apai_.
Authorization: Bearer apai_YOUR_KEY
Not the same thing as your access code. The access code signs you into this website; API keys are for programs.
/v1/modelsReturns 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
}
}
]
}
*_tokens fields, image models
charge per_image_usd for every image they produce.
id field as the model value in requests. The
administrator can enable or disable models at any time.
/v1/chat/completions| Field | Type | Description |
|---|---|---|
| model | string | Required. A model id from /v1/models. |
| messages | array | Required. {role, content} objects; roles are system, user, assistant. |
| stream | boolean | Server-sent events instead of one JSON body. |
| max_tokens | integer | Caps the reply length. Also caps the amount reserved from your balance. |
| temperature | number | 0–2. |
| top_p | number | Nucleus sampling. |
| stop | string / array | Stop 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 }
}
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]
[DONE] carries real token usage. APAI bills
from it, so your balance is charged for exactly what was generated.
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.
/v1/images/generations| Field | Type | Description |
|---|---|---|
| model | string | Required. An image model id from /v1/models. |
| prompt | string | Required. What to draw. |
| n | integer | How many images, 1–10. Defaults to 1 and is what your balance is reserved against. |
| size | string | e.g. 1024x1024. Which sizes exist is up to the model. |
| response_format | string | b64_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.
· 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 }
}
/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"
image work here. A chat model sent to an
image endpoint is refused with 400 rather than billed at token
prices, and vice versa.
/v1/responsesThe 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.
| Field | Type | Description |
|---|---|---|
| model | string | Required. A chat model id from /v1/models. |
| input | string / array | Required. A plain string, or a list of message items with input_text / input_image parts. |
| instructions | string | Becomes the system message. |
| max_output_tokens | integer | Caps the reply, and the amount reserved from your balance. |
| stream | boolean | Responses SSE events instead of one JSON body. |
| tools | array | Function tools only ({"type":"function","name":…,"parameters":…}). |
| text.format | object | text, json_object, or json_schema. |
| reasoning.effort | string | Forwarded 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.
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.
/v1/usageSpend 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
}
}
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
max_tokens, or n images). If the balance cannot cover it, the request is refused with 402 and nothing is charged.0 are free and never touch your balance.max_tokens both shortens replies and lowers the reserved amount.
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.
| Status | Meaning |
|---|---|
| 400 | Malformed request body. |
| 401 | Missing or invalid API key. |
| 402 | Your APAI balance is empty, or this key's budget is exhausted. Only ever an APAI billing decision. |
| 403 | Key revoked/expired, account disabled, or the model is not available to you. |
| 404 | Unknown model id. |
| 413 | An image upload for /v1/images/edits is larger than the 25 MB limit. |
| 429 | Too many login attempts (website only). |
| 502 | The upstream service behind the model failed or refused the request. The status tells you it is not an APAI billing problem. |
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.