Images API

Create images with ThAPI

Generate images via POST /v1/images/generations and edit reference images via POST /v1/images/edits (JSON payload).

Back to documentation

Connect to the API

Send requests to your site's API URL with Content-Type: application/json and Authorization: Bearer YOUR_API_KEY. x-api-key is also supported.

Keep your API key on the server. Examples use environment variables and do not send requests on your behalf.

Base URLhttps://thapi.cloud/v1

AuthorizationBearer $API_KEY

read -r -s -p "API_KEY: " API_KEY
export API_KEY
printf '\n'
curl -fsS https://thapi.cloud/v1/models -H "Authorization: Bearer ${API_KEY:?API_KEY}"
Authentication

Choose an image model

Use req/gpt-image-2 or req/gemini-3.0-pro-image. Always pass an explicit model for /v1/images/edits; only /v1/images/generations falls back to req/gpt-image-2 when the field is empty.

ModelPrice per request
req/gpt-image-2
GPT Image 2
1,000 ST
req/gpt-image-2.5
GPT Image 2.5
2,500 ST
req/gpt-image-2.6
GPT Image 2.6
3,500 ST
req/gpt-image-2.6-4k
GPT Image 2.6 4K
5,500 ST
req/banana-2
Banana 2
1,000 ST
req/banana-2-4k
Banana 2 4K
2,000 ST
req/gemini-3.0-pro-image
Gemini 3.0 Pro Image
1,000 ST
grok-image-2.0
Grok Imagine Image 2.0
1,000 ST

Call GET /v1/models to list available model IDs. Look up rates and billing units on the Pricing page. The API key must be authorized for the selected model.

View current pricing

Generate from a prompt

POST/v1/images/generationsapplication/json

Send POST /v1/images/generations with model and a descriptive prompt. The example starts with n: 1 and size: 1024x1024. The endpoint returns JSON when processing finishes. stream is forced to false; this endpoint does not return an SSE stream.

The cURL examples use Bash on macOS, Linux or WSL. For Python, install the dependency with pip install requests. For Node.js, use version 18 or later, save the code as an .mjs file and use the built-in fetch.

curl --fail-with-body --dump-header image-response-headers.txt "https://thapi.cloud/v1/images/generations" \
  -H "Authorization: Bearer ${API_KEY:?API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "req/gpt-image-2",
  "prompt": "A small botanical robot tending a seedling, studio product photography, soft natural light, clean ivory background, no text",
  "n": 1,
  "size": "1024x1024"
}'

Edit a reference image

POST/v1/images/editsapplication/json

Send POST /v1/images/edits as JSON with a nonempty prompt and image. The image can be a data URL, a base64 string, or a public http(s) URL. For cURL, replace https://example.com/reference.png with your actual public image URL. For Python or Node.js, place reference.png in the directory where you run the example.

Pass the source image via image (recommended), image_url, reference_image, or ref_image as a string (public URL / data URL / base64) or {url} object. Supports PNG, JPEG, WebP, GIF; private/internal URLs are blocked. Note: accepts application/json only — do not use OpenAI SDK's default multipart images.edit helper.

curl --fail-with-body --dump-header image-response-headers.txt "https://thapi.cloud/v1/images/edits" \
  -H "Authorization: Bearer ${API_KEY:?API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "req/gpt-image-2",
  "prompt": "Keep the subject and composition, change the background to a warm ivory studio",
  "image": "https://example.com/reference.png",
  "n": 1,
  "size": "1024x1024"
}'

Parameters and limits

The full JSON request is limited to 2047 MiB, including encoded images. Base64 increases payload size. Playground limits are separate.

ParameterRequiredMeaning
modelOptionalmodel: An image model ID. Defaults to req/gpt-image-2.
promptRequiredprompt: An image description or editing instructions. Edits require a nonempty string.
imageRequired for editsSend POST /v1/images/edits as JSON with a nonempty prompt and image. The image can be a data URL, a base64 string, or a public http(s) URL. Reference images support PNG, JPEG, WebP and GIF.
sizeOptionalsize: Examples use 1024x1024. Supported dimensions depend on the model and provider.
nOptionaln: Examples request 1 image. The value is forwarded to the provider; supported counts depend on the model.
quality / response_format / output_formatOptionalquality, response_format and additional parameters are forwarded. Send only parameters supported by the model; the gateway does not guarantee every option takes effect.

Read and save returned images

Read the data array in the JSON response. Depending on the provider, image content is in b64_json, b64Json or url. Additional fields can differ between models.

{
  "data": [
    {
      "b64_json": "<base64 image bytes>"
    }
  ]
}

For base64, decode it to an image file or create a data URL with the MIME type matching the actual format. Do not assume every output is PNG. For a URL, use it to download or display the image. URL lifetime and access depend on the provider.

Save x-request-id for lookups. Successful responses carry x-api-engine, and non-streaming calls also carry x-api-billed-usage. Look up final cost and rates in Usage, not in headers.

Pricing per request

Image models are billed per request. Check the per-request rate on the Pricing page before running; the API key balance or quota may block the request. Tokens in the response or headers are not a basis for recomputing image model pricing. A new request may incur a new charge.

Do not automatically resend after a timeout or disconnection: the earlier request may have been processed. Check Usage history and save the request ID for reference or support before deciding to call again.

Billing

Handle errors carefully

Gateway errors use {type: "error", error: {type, message, ...}}. Read the HTTP status and error.message, and save the request ID when available.

HTTPErrorAction
400invalid_request_errorCheck JSON, model, endpoint, prompt and reference image.
401authentication_errorCheck the API key.
402insufficient_balance / api_key_monthly_limit / outstanding_underchargeCheck balance, monthly limits or outstanding payments.
403model_not_allowedCheck model permissions.
413request_too_largeReduce the total request size, including base64 data. The provider or model may impose a lower limit than the gateway JSON limit.
429rate_limit_errorSlow down and follow Retry-After or retry_after_ms when present.
502 / 503api_error / service_unavailableThe provider or service may be temporarily unavailable.
Errors and retries

Before sending a request

  1. Check your API key and model permissions.
  2. Confirm the model and price in /v1/models.
  3. Check JSON, prompt and image data.
  4. Handle both base64 and URL output.
  5. Check Usage history; save the request ID for reference or support.