Image generation
Compatible with OpenAI Images. Generates images from text.
https://api.codingrouter.ai/v1/images/generations| Model | Token group | Billing |
|---|---|---|
gpt-image-2, gpt-image-2.5 | gpt_image | Per token |
grok-imagine-image-2.0 | grok | Per call |
Gemini image models generate through the Gemini API.
Authentication
Authorization: Bearer <token>
Request parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | See the table above |
prompt | string | Yes | Image description |
size | string | No | Size; the rules differ by model, see below |
quality | string | No | GPT Image: low, medium, high, auto; gpt-image-2.5 also has xhigh and max. Billed as low if omitted |
n | integer | No | Number of images GPT Image generates, 1 to 10; Grok generates 1 per call |
output_format | string | No | GPT Image: png (default), jpeg, webp |
output_compression | integer | No | 0 to 100, for jpeg and webp |
background | string | No | GPT Image: auto, opaque, transparent (transparent background needs png or webp) |
moderation | string | No | GPT Image: auto, low |
response_format | string | No | Only b64_json is supported; url returns 400 |
Results are returned only as base64 (b64_json). No image links are provided.
GPT Image sizes
size is auto (default) or widthxheight. widthxheight must meet all of these:
- Width and height are both multiples of 16, and each side is at most 3840;
- Total pixels between 655,360 and 8,294,400 (the maximum equals 3840x2160);
- Aspect ratio between 1:3 and 3:1.
Common values: 1024x1024, 1536x1024, 1024x1536, 2048x2048, 3840x2160.
Grok Imagine sizes and prices
size | Actual output | Price per image |
|---|---|---|
Omitted / 1024x1024 | 1024×1024 JPEG | $0.004 |
1536x1024 | 2496×1664 PNG | $0.005 |
1024x1536 | 1664×2496 PNG | $0.005 |
2048x2048 | 2048×2048 PNG | $0.006 |
2048x1152 | 2816×1584 PNG | $0.006 |
The default size returns JPEG and other sizes return PNG. Name the saved file by the actual format.
Request example
curl https://api.codingrouter.ai/v1/images/generations \
-H "Authorization: Bearer $CODINGROUTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A Shiba Inu running in the snow, cinematic lighting",
"size": "1024x1024",
"quality": "low"
}' | jq -r '.data[0].b64_json' | base64 --decode > out.pngimport base64
from openai import OpenAI
client = OpenAI(base_url="https://api.codingrouter.ai/v1", api_key="sk-your-token", timeout=300)
result = client.images.generate(
model="gpt-image-2",
prompt="A Shiba Inu running in the snow, cinematic lighting",
size="1024x1024",
quality="low",
)
with open("out.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))import fs from "node:fs";
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.codingrouter.ai/v1",
apiKey: process.env.CODINGROUTER_KEY,
timeout: 300_000,
});
const result = await client.images.generate({
model: "gpt-image-2",
prompt: "A Shiba Inu running in the snow, cinematic lighting",
size: "1024x1024",
quality: "low",
});
fs.writeFileSync("out.png", Buffer.from(result.data[0].b64_json, "base64"));Grok: change model to grok-imagine-image-2.0, use a token from the grok group, and remove quality.
Response example
{
"created": 1794806592,
"data": [{"b64_json": "<base64 image data>"}],
"background": "opaque",
"output_format": "png",
"size": "1024x1024",
"usage": {
"input_tokens": 22,
"input_tokens_details": {"image_tokens": 0, "text_tokens": 22},
"output_tokens": 196,
"output_tokens_details": {"image_tokens": 196, "text_tokens": 0},
"total_tokens": 218
}
}The Grok response has only created and data.
GPT Image billing
Billed per token. Official unit prices (per million tokens; the actual charge applies the group discount):
| Item | Price |
|---|---|
| Text input | $5 |
| Text input (cached) | $1.25 |
| Image input | $8 |
| Image input (cached) | $2 |
| Image output | $30 |
Output tokens depend on the size and quality. For example 1024x1024 low is about 196, and medium is about 1756.
Integration tips
- Set the client timeout to 300 seconds. High quality at large sizes can take more than 1 minute.
- For 400, fix the parameters before retrying. 429 and 5xx can be retried.
