Create images with ThAPI
Generate images via POST /v1/images/generations and edit reference images via POST /v1/images/edits (JSON payload).
Back to documentationConnect 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}"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.
| Model | Price per request |
|---|---|
req/gpt-image-2GPT Image 2 | 1,000 ST |
req/gpt-image-2.5GPT Image 2.5 | 2,500 ST |
req/gpt-image-2.6GPT Image 2.6 | 3,500 ST |
req/gpt-image-2.6-4kGPT Image 2.6 4K | 5,500 ST |
req/banana-2Banana 2 | 1,000 ST |
req/banana-2-4kBanana 2 4K | 2,000 ST |
req/gemini-3.0-pro-imageGemini 3.0 Pro Image | 1,000 ST |
grok-image-2.0Grok 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 pricingGenerate from a prompt
/v1/images/generationsapplication/jsonSend 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
/v1/images/editsapplication/jsonSend 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.
| Parameter | Required | Meaning |
|---|---|---|
model | Optional | model: An image model ID. Defaults to req/gpt-image-2. |
prompt | Required | prompt: An image description or editing instructions. Edits require a nonempty string. |
image | Required for edits | 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. Reference images support PNG, JPEG, WebP and GIF. |
size | Optional | size: Examples use 1024x1024. Supported dimensions depend on the model and provider. |
n | Optional | n: Examples request 1 image. The value is forwarded to the provider; supported counts depend on the model. |
quality / response_format / output_format | Optional | quality, 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.
BillingHandle errors carefully
Gateway errors use {type: "error", error: {type, message, ...}}. Read the HTTP status and error.message, and save the request ID when available.
| HTTP | Error | Action |
|---|---|---|
| 400 | invalid_request_error | Check JSON, model, endpoint, prompt and reference image. |
| 401 | authentication_error | Check the API key. |
| 402 | insufficient_balance / api_key_monthly_limit / outstanding_undercharge | Check balance, monthly limits or outstanding payments. |
| 403 | model_not_allowed | Check model permissions. |
| 413 | request_too_large | Reduce the total request size, including base64 data. The provider or model may impose a lower limit than the gateway JSON limit. |
| 429 | rate_limit_error | Slow down and follow Retry-After or retry_after_ms when present. |
| 502 / 503 | api_error / service_unavailable | The provider or service may be temporarily unavailable. |
Before sending a request
- Check your API key and model permissions.
- Confirm the model and price in /v1/models.
- Check JSON, prompt and image data.
- Handle both base64 and URL output.
- Check Usage history; save the request ID for reference or support.