# apiaiapi API reference

apiaiapi is an OpenAI-compatible API for GPT Image 2. Use any OpenAI SDK or plain HTTP: point the base URL at apiaiapi and use an apiaiapi key. Nothing else changes.

- Base URL: `https://api.apiaiapi.com/v1`
- Price: $0.02 (CNY 0.1) per image, any size or quality. You pay only for images that come back. Failed calls are refunded in full.
- Access: invite-only. Request access at https://app.apiaiapi.com; seats open in batches.
- MCP server for agents: `https://api.apiaiapi.com/mcp` (see [MCP](#mcp))
- Plain-text copy of this page: https://www.apiaiapi.com/docs.md

## Quick start

```bash
curl https://api.apiaiapi.com/v1/images/generations \
  -H "Authorization: Bearer $APIAIAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-image-2", "prompt": "a paper crane on a desk", "size": "1024x1024"}'
```

```python
from openai import OpenAI

client = OpenAI(base_url="https://api.apiaiapi.com/v1", api_key="sk-...", timeout=600)
result = client.images.generate(model="gpt-image-2", prompt="a paper crane on a desk", size="1024x1024")
```

```javascript
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.apiaiapi.com/v1", apiKey: "sk-...", timeout: 600_000 });
const result = await client.images.generate({ model: "gpt-image-2", prompt: "a paper crane on a desk", size: "1024x1024" });
```

An image usually takes 1.5 to 3 minutes. Give synchronous calls a client timeout of at least 7 minutes. If your platform cuts requests off sooner (serverless functions, proxies with idle timeouts), use the async endpoints.

## Authentication

Create keys in the workspace at https://app.apiaiapi.com/keys. Send the key on every request:

```
Authorization: Bearer sk-...
```

The key only works once your account is activated. Until then every call returns `403 account_not_active`.

## Models

`GET /v1/models` lists the models you can call. Today that is `gpt-image-2`.

## Generate images

`POST /v1/images/generations` with a JSON body. The call stays open until the images are ready.

| Parameter | Required | Values |
| --- | --- | --- |
| `model` | yes | `gpt-image-2` |
| `prompt` | yes | Text description of the image. |
| `size` | no | `1024x1024`, `1024x1536`, `1536x1024` or `auto` (default) |
| `quality` | no | `low`, `medium`, `high` or `auto` (default) |
| `n` | no | Number of images, 1 to 4. Default 1. |

Other OpenAI image parameters (for example `output_format` or `background`) are passed to the model unchanged.

Response:

```json
{
  "created": 1764900000,
  "data": [{ "b64_json": "iVBORw0...", "revised_prompt": "" }]
}
```

Each item in `data` has either `b64_json` or `url`. Handle both. URLs point at `img.apiaiapi.com` and stay up for 7 days.

Image calls return an `x-request-id` header. Quote it when you contact support.

## Edit images

`POST /v1/images/edits` with `multipart/form-data`. Same fields as generations, plus the source `image` file (and an optional `mask`), as in the OpenAI API. Each of `model`, `size`, `quality` and `n` may appear only once. The whole request body may be at most 20 MB.

## Async tasks

The async endpoints take exactly the same request body and answer at once with a task id. Use them when you cannot hold a request open for minutes.

```
POST /v1/images/generations/async   -> 202
POST /v1/images/edits/async         -> 202

{ "id": "task_...", "object": "image.task", "status": "queued", "created": 1764900000 }
```

Poll `GET /v1/tasks/{id}` every 5 to 10 seconds until `status` is `succeeded` or `failed`:

```json
{
  "id": "task_...",
  "object": "image.task",
  "status": "succeeded",
  "created": 1764900000,
  "data": [{ "url": "https://img.apiaiapi.com/..." }]
}
```

- `status` is one of `queued`, `running`, `succeeded`, `failed`.
- On success `data` always holds URLs (never base64). They stay up for 7 days.
- On failure `error.message` says what happened, and the hold is refunded in full.
- The price is held from your balance when you submit. If the balance is too low, the submit returns `402` right away.
- A task that has not finished within an hour fails and is refunded.
- You can only read tasks created with keys of your own account. Anything else returns `404 task_not_found`.

## MCP

Agents can generate images through the remote MCP server at `https://api.apiaiapi.com/mcp` (Streamable HTTP, stateless). It authenticates with the same API key, sent as a header, and bills exactly like the API.

Claude Code:

```bash
claude mcp add --transport http apiaiapi https://api.apiaiapi.com/mcp --header "Authorization: Bearer sk-..."
```

Cursor (`~/.cursor/mcp.json`):

```json
{
  "mcpServers": {
    "apiaiapi": {
      "url": "https://api.apiaiapi.com/mcp",
      "headers": { "Authorization": "Bearer sk-..." }
    }
  }
}
```

VS Code (`.vscode/mcp.json`):

```json
{
  "servers": {
    "apiaiapi": {
      "type": "http",
      "url": "https://api.apiaiapi.com/mcp",
      "headers": { "Authorization": "Bearer sk-..." }
    }
  }
}
```

Any other client that supports Streamable HTTP with a custom header works the same way.

| Tool | What it does |
| --- | --- |
| `generate_image` | Starts a generation (`prompt`, `size`, `quality`, `n`) and returns a `task_id` at once. |
| `edit_image` | Starts an edit from up to 4 public HTTPS image URLs (`image_urls`, optional `mask_url`) and returns a `task_id`. |
| `get_image` | Waits up to 45 seconds for a task. Returns the image URLs when it is done; otherwise call it again. |
| `get_balance` | Remaining credit and roughly how many images it buys. |

Images take 1.5 to 3 minutes, longer than many MCP clients let one tool call run, so generation is split into a submit and a wait. Results come back as URLs (and `resource_link` items), not inline image data, to keep tool output small.

## Balance

`GET /v1/balance` returns your remaining credit:

```json
{ "balance": 12.3, "currency": "CNY" }
```

`currency` is your account currency: the one you first topped up in (`CNY` until your first top-up). One image costs $0.02 or CNY 0.1, so `balance / 0.02` (USD) or `balance / 0.1` (CNY) is the number of images left. Top up at https://app.apiaiapi.com/billing.

## Pricing

- $0.02 (CNY 0.1) per image at every size and quality.
- The price is held when the call starts and settled when it ends: you pay for the images actually returned, and the rest is released.
- Upstream errors, timeouts and blocked prompts cost nothing.
- Prepaid credit, one-time top-ups, no subscription. Details: https://www.apiaiapi.com/pricing

## Limits

| Limit | Value |
| --- | --- |
| Submits (POST) per account | 60 per minute. Reads (GET) are not counted. |
| Images generating at once per account | 2. Up to 4 more wait in line; beyond that you get `429 server_busy`. |
| Images per call (`n`) | 1 to 4 |
| Request body | 20 MB |
| Single image in a response | 12 MB |
| Wait in line before giving up | 2 minutes, then `429 server_busy` |
| Image URLs | kept 7 days |

## Errors

Errors use the OpenAI format, so SDKs raise them as usual:

```json
{
  "error": {
    "message": "Insufficient balance. Top up at https://app.apiaiapi.com/billing and try again.",
    "type": "insufficient_quota",
    "code": "insufficient_quota",
    "doc_url": "https://www.apiaiapi.com/docs#errors"
  }
}
```

When waiting will help, the response has a `Retry-After` header in seconds. Retry after that long; without the header, retrying the same request will fail the same way. Apart from `settlement_unconfirmed`, a call that returns an error is never charged.

| Status | `code` | Meaning | What to do |
| --- | --- | --- | --- |
| 400 | `model_not_found` | Missing or unsupported `model`. | Use a model from `GET /v1/models`. |
| 400 | `moderation_blocked` | The prompt was rejected by content screening. | Change the prompt. Email support with the reference in the message if you think it is a mistake. |
| 400 | none | Invalid parameters (bad `size`, `quality`, `n`, body format). The message names the problem. | Fix the request. |
| 4xx | other | The image model refused the request, for example a content policy rejection. | Change the request; retrying it unchanged will not help. |
| 401 | `invalid_api_key` | The key is missing, wrong or revoked. | Check the `Authorization` header. |
| 402 | `insufficient_quota` | Not enough credit for this call. | Top up at https://app.apiaiapi.com/billing. |
| 403 | `account_not_active` | The account is still on the waitlist. | Wait for the activation email. |
| 404 | `task_not_found` | No such task for this account. | Check the task id. |
| 413 | `request_too_large` | Request body over 20 MB. | Send smaller images. |
| 429 | `rate_limit_exceeded` | Over 60 submits in a minute. | Wait `Retry-After` (60) seconds. |
| 429 | `server_busy` | Your line is full, or no slot opened within 2 minutes. | Wait `Retry-After` seconds, or use the async endpoints. |
| 500 | none | Internal error. | Retry later; contact support with the `x-request-id` if it persists. |
| 502 | `delivery_failed`, `storage_unavailable`, `invalid_upstream_response`, none | Generation or delivery failed. | Retry later. |
| 503 | `buffer_capacity`, `moderation_unavailable`, none | Temporarily overloaded or unavailable. | Wait `Retry-After` seconds. |
| 503 | `settlement_unconfirmed` | The image may have been made, but billing could not be confirmed. | Do not retry right away: a retry makes and bills a new image. Check `GET /v1/balance` or contact support with the `x-request-id`. |
| 504 | `timeout` | Generation took too long. | Retry later, or use the async endpoints. |

## Support

Email support@apiaiapi.com with the `x-request-id` (or task id) of the call.
