WorldRouter

Skip to Content

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.

Tip:

Looking for Google’s native generateContent image route? See Gemini multimodal image generation.

What you can do

  • Generate images with gpt-image-2 through POST /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.

Tip:

Start from Quickstart if you still need an API key, the base URL, or a connection test.

Quick start flow

  1. 1Send an image-generation request to POST /v1/images/generations.
  2. 2Save the JSON response.
  3. 3Extract the Base64 image field and decode it into a real image file.
  4. 4Verify where the output file was saved, then open it locally.

Endpoint

Endpoint
POST https://inference-api-pre-d80ca3.worldrouter.ai/v1/images/generations

Request body

FieldRequiredDescription
modelyesFixed image model ID — gpt-image-2.
promptyesNatural-language description of the image you want to generate.
nnoNumber of images to return per call. Defaults to 1.
sizenoOutput dimensions. auto (default) or <width>x<height>. See Supported sizes.
qualitynoQuality tier; passed through to the upstream model (e.g. low, medium, high).
output_formatnoOutput 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:

SizeAspect
1024x1024Square
1536x1024Landscape
1024x1536Portrait
2048x20482K square
2048x11522K landscape
3840x21604K landscape
2160x38404K portrait
autoDefault

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,360 and no more than 8,294,400.

Examples

curl
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.json

The JSON response is saved to openai-image-response.json.

python
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], "...")
javascript
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.

200 OK
{
"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:

python
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.

python
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:

bash
ls -lh output.png

macOS: 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.

FieldRequiredDescription
modelyesgrok-imagine-image or grok-imagine-image-quality.
promptyesNatural-language description of the image.
nnoNumber of images to return (14). Defaults to 1.
aspect_rationo1:1 (default), 16:9, 9:16, 3:2, or 2:3.
resolutionno1k. grok-imagine-image-quality also supports 2k.
curl
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.json

The 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

Last updated on