Image Generation API
OpenAI-compatible image generation endpoint, supporting image models like gpt-image-2.
Request
POST /v1/images/generations
Headers
Authorization: Bearer pk_live_xxxxxxxxxxxxxxxx
Content-Type: application/json
You can also use the x-api-key header (Anthropic SDK style):
x-api-key: pk_live_xxxxxxxxxxxxxxxx
Content-Type: application/json
Request Body
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | ✅ | Model name, e.g. gpt-image-2 |
prompt | string | ✅ | Image description, up to 32,000 characters |
n | integer | ❌ | Number of images, default 1, max depends on model config |
size | string | ❌ | Image dimensions, see supported list below |
quality | string | ❌ | Quality level, e.g. auto, high, medium, low |
background | string | ❌ | Background setting, e.g. auto, transparent, opaque |
output_format | string | ❌ | Output format, e.g. png, jpeg, webp |
response_format | string | ❌ | Return format, always b64_json. Passing url still returns base64 data (no external URLs exposed) |
aspect_ratio | string | ❌ | Aspect ratio, e.g. 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3. When set, the generated image will match the specified ratio |
Supported Sizes
Tokensmart does not restrict image sizes — any size the image model supports will work. We do not maintain a static whitelist.
Format constraints (lightweight Tokensmart-side validation):
- Must be a string, ≤ 16 characters
- Must be
autoorWIDTHxHEIGHTformat (digits only, lowercasexseparator, e.g.1024x1024,2048x2048) - The following are rejected with
400 invalid_request: keyword strings (big,large), values with spaces (1024 x 1024), non-string values (numbers, objects), oversized strings
Verified working sizes (against gpt-image-2, as of 2026-04-25):
| Size | Type | Notes |
|---|---|---|
auto | Auto | Model decides output size |
1024x1024 | Square | gpt-image-1 / DALL-E native |
1024x1536, 1536x1024 | 3:2 | gpt-image-1 native (portrait / landscape) |
1024x1792, 1792x1024 | 7:4 | DALL-E 3 style (tall / wide) |
1280x1280 | Non-standard square | Supported, verified working |
2048x2048 | High-res square | Accepted; output ~3 MB, ~90 s latency |
256x256, 512x512 | Small | DALL-E 2 compatibility |
Unsupported sizes (e.g. 4096x4096): the request returns 502 upstream_error and the pre-deduction is automatically refunded — no charge to your balance.
Request Example
{
"model": "gpt-image-2",
"prompt": "a cute orange cat wearing a wizard hat, sitting on a rooftop under moonlight, watercolor style",
"n": 1,
"size": "1024x1024",
"quality": "auto"
}
Response
{
"created": 1776864985,
"data": [
{
"b64_json": "iVBORw0KGgo..."
}
]
}
Each item in the data array contains:
| Field | Description |
|---|---|
b64_json | Base64-encoded image data (always returned; no external URLs are exposed) |
revised_prompt | Model's revised prompt (if available) |
Billing
Image models use per-call billing, not token-based:
- Fixed price per image — see Pricing
- Pre-deduction freezes balance for the requested
n, settlement charges for actually returned images - If fewer images than requested are returned, the difference is automatically refunded
Python Example
from openai import OpenAI
import base64
client = OpenAI(
api_key="pk_live_xxxxxxxxxxxxxxxx",
base_url="https://api.tokensmart.ai/v1",
)
result = client.images.generate(
model="gpt-image-2",
prompt="a cute cat wearing a wizard hat on a rooftop under moonlight, watercolor style",
n=1,
size="1024x1024",
)
# Save image
image_data = base64.b64decode(result.data[0].b64_json)
with open("cat.png", "wb") as f:
f.write(image_data)
cURL Example
curl https://api.tokensmart.ai/v1/images/generations \
-H "Authorization: Bearer pk_live_xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "a cute cat wearing a wizard hat on a rooftop under moonlight, watercolor style",
"n": 1,
"size": "1024x1024"
}'
Node.js Example
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "pk_live_xxxxxxxxxxxxxxxx",
baseURL: "https://api.tokensmart.ai/v1",
});
const result = await client.images.generate({
model: "gpt-image-2",
prompt: "a cute cat wearing a wizard hat",
n: 1,
size: "1024x1024",
});
// Save image
const imageData = Buffer.from(result.data[0].b64_json, "base64");
fs.writeFileSync("cat.png", imageData);
Error Codes
| HTTP Status | Code | Description |
|---|---|---|
| 400 | invalid_request | Validation failed (invalid n, size, prompt, etc.) |
| 400 | content_policy_violation | Prompt hit content moderation (pre-deduction auto-refunded) |
| 400 | no_image_generated | Zero images returned for non-policy reasons — vague prompt, model refusal, etc. (pre-deduction refunded) |
| 401 | invalid_api_key | Invalid or missing API key |
| 402 | insufficient_balance | Insufficient balance |
| 403 | MODEL_ACCESS_DENIED | No access to this model |
| 404 | model_not_found | Model not found or not available |
| 429 | rate_limit_exceeded | Rate limit exceeded |
| 502 | upstream_error | Service error |
Important Notes
- Image generation typically takes 30-90 seconds — set a long enough timeout
- Base64 image data is large (1-5 MB per image) — watch client memory
gpt-image-2currently supports max 1 image per request- For non-image models (gpt-5.5, Claude, etc.), use
/v1/chat/completionsor/v1/messages
Related Endpoints
- Image-to-image editing (edit an existing image): see Image Edits API
- Multi-image compose (blend several reference images): see Image Compose API
- Upload external images (prerequisite for image-to-image): see File Upload API