API referenceReference / 07

Image generation

OpenAI-compatible image generation with gpt-image-2.

Follow updates

POST https://api.routerplex.com/v1/images/generations

OpenAI-compatible image generation. Images are returned base64-encoded and billed by token (prompt text in, image tokens out).

python
import base64
import os
from openai import OpenAI
 
client = OpenAI(
base_url="https://api.routerplex.com/v1",
api_key=os.environ["ROUTERPLEX_API_KEY"],
)
 
result = client.images.generate(
model="gpt-image-2",
prompt="A lighthouse on a cliff at dusk, watercolor",
size="1024x1024",
quality="medium", # low | medium | high
)
with open("lighthouse.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))
  • Sizes: 1024x1024, 1536x1024, 1024x1536.
  • Higher quality uses more output tokens.
  • Try it without code in the playground.

Response and storage #

The generated image arrives in the response as base64 data rather than a permanent hosted URL. Decode it and store the resulting file in your own application or object store. RouterPlex does not turn generation responses into a public media library.

Cost and failure checks #

Image cost depends on the output tokens used by the requested size and quality, so test the smallest acceptable settings before running a batch. Give image jobs a dedicated key budget to cap retries or loops. A 401 means the key is missing or invalid; a 400 usually points to an unsupported size, quality, or malformed prompt payload; a 429 should be retried with backoff.

Image generation — Docs