Skip to content

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

ParameterRequiredDescription
modelRequiredModel name, for example gpt-image-2
promptRequiredText description of the image, up to 32,000 characters
nOptionalNumber of images, default 1; the maximum is in the model specs
sizeOptionalSize, for example 1024x1024, 1536x1024, 1024x1536 or auto; allowed values depend on the model
qualityOptionallow / medium / high / auto. Higher quality uses more image output tokens and takes longer
background / output_format / …Optionalbackground, output_format, output_compression, moderation and user are passed to the model unchanged, with the same values as OpenAI
response_formatOptionalOnly 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.

  1. 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.
  2. After a successful generation the request settles on the returned usage, rounded up to the pricing step, and the difference is released.
  3. Requests that produce no image (invalid parameters, rejected content, model temporarily unavailable) are not charged.
Billing itemusage field
Text inputusage.input_tokens_details.text_tokens
Image inputusage.input_tokens_details.image_tokens
Image outputusage.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

HTTPcodeWhen
400invalid_parameterInvalid parameters: prompt is missing, n / size / quality is outside what the model supports, or stream was sent
400invalid_request_errorThe 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
400content_filterThe prompt or the result was rejected by the content safety policy
402quota_exceeded / key_limit_exceededAccount quota or key quota is not enough
403permission_deniedThe key lacks images:create, or the model is not enabled / not on the key's model allowlist
404not_foundThe model does not exist or is not listed (the image endpoint only accepts image models)
429rate_limitedToo many requests or too many in progress; retry after Retry-After
503no_available_channelThe 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