Generate Images (OpenAI-compatible)
Use WorldRouter’s OpenAI-compatible POST /v1/images/generations endpoint to generate images with gpt-image-2 and the Grok Imagine image models.
Looking for Google’s native generateContent image route? See Gemini
multimodal image generation.
What you can do
- Generate images with
gpt-image-2throughPOST /v1/images/generations. - Generate images with the Grok Imagine image models (
grok-imagine-image,grok-imagine-image-quality) through the same endpoint.
Before you start
The same API key and base URL work for every WorldRouter image surface. Only the request body and response shape differ.
Start from Quickstart if you still need an API key, the base URL, or a connection test.
Quick start flow
- 1Send an image-generation request to POST /v1/images/generations.
- 2Save the JSON response.
- 3Extract the Base64 image field and decode it into a real image file.
- 4Verify where the output file was saved, then open it locally.
Endpoint
POST https://inference-api-pre-d80ca3.worldrouter.ai/v1/images/generationsRequest body
| Field | Required | Description |
|---|---|---|
model | yes | Fixed image model ID — gpt-image-2. |
prompt | yes | Natural-language description of the image you want to generate. |
n | no | Number of images to return per call. Defaults to 1. |
size | no | Output dimensions. auto (default) or <width>x<height>. See Supported sizes. |
quality | no | Quality tier; passed through to the upstream model (e.g. low, medium, high). |
output_format | no | Output image format, e.g. png or jpeg. |
Supported sizes for gpt-image-2
gpt-image-2 accepts any resolution in size that satisfies the constraints below. Square images are typically fastest to generate.
Popular sizes:
| Size | Aspect |
|---|---|
1024x1024 | Square |
1536x1024 | Landscape |
1024x1536 | Portrait |
2048x2048 | 2K square |
2048x1152 | 2K landscape |
3840x2160 | 4K landscape |
2160x3840 | 4K portrait |
auto | Default |
Constraints:
- Maximum edge length must be less than or equal to
3840px. - Both edges must be multiples of
16px. - Long edge to short edge ratio must not exceed
3:1. - Total pixels must be at least
655,360and no more than8,294,400.
Examples
curl -X POST "https://inference-api-pre-d80ca3.worldrouter.ai/v1/images/generations" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A small clean five-point star icon, centered on a white background.",
"n": 1,
"size": "2048x2048",
"quality": "low",
"output_format": "png"
}' \
-o openai-image-response.jsonThe JSON response is saved to openai-image-response.json.
from openai import OpenAI
client = OpenAI(
api_key="your_api_key",
base_url="https://inference-api-pre-d80ca3.worldrouter.ai/v1",
)
response = client.images.generate(
model="gpt-image-2",
prompt="A small clean five-point star icon, centered on a white background.",
n=1,
size="2048x2048",
quality="low",
output_format="png",
)
b64_image = response.data[0].b64_json
print(b64_image[:64], "...")import OpenAI from "openai";
const client = new OpenAI({
apiKey: "your_api_key",
baseURL: "https://inference-api-pre-d80ca3.worldrouter.ai/v1",
});
const response = await client.images.generate({
model: "gpt-image-2",
prompt: "A small clean five-point star icon, centered on a white background.",
n: 1,
size: "2048x2048",
quality: "low",
output_format: "png",
});
console.log(response.data[0].b64_json.slice(0, 64), "...");Response
Example response. Actual fields may vary slightly by model or upstream behavior.
{
"created": 1780501937,
"background": null,
"output_format": "png",
"quality": "low",
"size": "2048x2048",
"usage": {
"total_tokens": 453,
"input_tokens": 56,
"input_tokens_details": {
"image_tokens": 0,
"text_tokens": 56
},
"output_tokens": 397,
"output_tokens_details": {
"image_tokens": 397,
"text_tokens": 0
}
},
"data": [
{
"b64_json": "<base64 image>"
}
]
}In the Python and JavaScript SDK examples above, response is already available in memory:
import base64
with open("output.png", "wb") as f:
f.write(base64.b64decode(response.data[0].b64_json))For curl, decode the saved openai-image-response.json in a second step.
Decode openai-image-response.json
Read openai-image-response.json in Python and write the image to output.png.
import base64
import json
with open("openai-image-response.json", "r", encoding="utf-8") as f:
payload = json.load(f)
output_path = "output.png"
image_b64 = payload["data"][0]["b64_json"]
with open(output_path, "wb") as f:
f.write(base64.b64decode(image_b64))
print(f"Saved image to {output_path}")Default output location: current working directory. To save elsewhere, change output_path to an absolute path such as /tmp/output.png.
Verify:
ls -lh output.pngmacOS: open output.png. Other systems: open the file with a local image viewer.
Grok Imagine image
grok-imagine-image and grok-imagine-image-quality share the same
POST /v1/images/generations surface as gpt-image-2 (vendor: xAI). Send one
of those model ids and read images from data[].b64_json the same way.
| Field | Required | Description |
|---|---|---|
model | yes | grok-imagine-image or grok-imagine-image-quality. |
prompt | yes | Natural-language description of the image. |
n | no | Number of images to return (1–4). Defaults to 1. |
aspect_ratio | no | 1:1 (default), 16:9, 9:16, 3:2, or 2:3. |
resolution | no | 1k. grok-imagine-image-quality also supports 2k. |
curl -X POST "https://inference-api-pre-d80ca3.worldrouter.ai/v1/images/generations" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-image-quality",
"prompt": "A small clean five-point star icon, centered on a white background.",
"n": 1,
"aspect_ratio": "1:1",
"resolution": "2k"
}' \
-o grok-image-response.jsonThe response uses the same OpenAI images shape as gpt-image-2 (decode
data[0].b64_json). Grok Imagine image models are billed per generated
image, not by token.
Error codes
Image-generation requests share the same error codes as the rest of the WorldRouter API. See Error codes in the API reference for the full table.
See also
- Gemini multimodal image generation: Google’s native
generateContentimage route. - OpenAI images reference : WorldRouter mirrors this schema for image generation.
- Seedance video guide: async video generation with
/api/v3/contents/generations/tasks. - Grok video guide: async video generation with Grok Imagine.
- Kling video guide: async video generation with Kling.
- API reference: chat-completions docs for text models.
- Models: full catalog with live pricing.
- Quickstart: API key, base URL, and first call.