API docs · Chat & images
Image generation API
POST /v1/images/generations: text-to-image with models such as GPT Image, OpenAI-compatible; parameters, response, billing and errors.
Generate images from a text description with models such as GPT Image. The endpoint matches the OpenAI Images API, so the official SDKs work once you change the base URL and key. Only text-to-image is supported for now; image editing and streaming are not.
Before you start
- Create or edit a key on the console "API Keys" page and tick images:create (existing keys do not have it by default).
- Image models must be enabled for your account first. See the price list below for available models, or call GET /v1/models (entries whose kind is image).
Request parameters
| Parameter | Required | Description |
|---|---|---|
model | Required | Model name, for example gpt-image-2 |
prompt | Required | Text description of the image, up to 32,000 characters |
n | Optional | Number of images, default 1; the maximum is in the model specs |
size | Optional | Size, for example 1024x1024, 1536x1024, 1024x1536 or auto; allowed values depend on the model |
quality | Optional | low / medium / high / auto. Higher quality uses more image output tokens and takes longer |
background / output_format / … | Optional | background, output_format, output_compression, moderation and user are passed to the model unchanged, with the same values as OpenAI |
response_format | Optional | Only b64_json is supported (it can be omitted) |
stream | — | Not supported yet; true returns 400 |
Response
The data array holds one object per image; b64_json is the base64 content of the image, so decode it and save it to a file. Images are not stored on the platform and no image URL is provided.
usage is the token usage of the request and is what billing is based on.
Billing
Priced per token, per million tokens in USD, with separate prices for text input, image input and image output.
- Before the request starts a ceiling is held: the prompt is estimated conservatively from its byte length and image output is "images × per-image ceiling" (a lower share for low and medium quality). If the quota is not enough the request returns 402 and generation does not start.
- After a successful generation the request settles on the returned usage, rounded up to the pricing step, and the difference is released.
- Requests that produce no image (invalid parameters, rejected content, model temporarily unavailable) are not charged.
| Billing item | usage field |
|---|---|
| Text input | usage.input_tokens_details.text_tokens |
| Image input | usage.input_tokens_details.image_tokens |
| Image output | usage.output_tokens |
Prices
Latency and timeouts
The response returns only after the whole image is generated: low quality usually takes ten to a few dozen seconds, high quality can take one to two minutes. Set the client timeout to 5 minutes; use low or medium when latency matters.
Errors
| HTTP | code | When |
|---|---|---|
| 400 | invalid_parameter | Invalid parameters: prompt is missing, n / size / quality is outside what the model supports, or stream was sent |
| 400 | invalid_request_error | The model rejected the request parameters; message is an explanation written by the platform in the request language. For common cases it states the specific reason (e.g. "parameter size has an invalid value; allowed values: 1024x1024, auto"): fix the request accordingly and resend. Otherwise it only asks you to check the request parameters against the parameter reference |
| 400 | content_filter | The prompt or the result was rejected by the content safety policy |
| 402 | quota_exceeded / key_limit_exceeded | Account quota or key quota is not enough |
| 403 | permission_denied | The key lacks images:create, or the model is not enabled / not on the key's model allowlist |
| 404 | not_found | The model does not exist or is not listed (the image endpoint only accepts image models) |
| 429 | rate_limited | Too many requests or too many in progress; retry after Retry-After |
| 503 | no_available_channel | The model is temporarily unavailable or has no route right now: retry later or use another model |
curl https://api.aitoken.tools/v1/images/generations \
--max-time 300 \
-H "Authorization: Bearer $AITOKEN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A small red apple on a plain white background, product photo",
"size": "1024x1024",
"quality": "low"
}' | jq -r '.data[0].b64_json' | base64 --decode > apple.png