Skip to content

Image generation ​

Compatible with OpenAI Images. Generates images from text.

POSThttps://api.codingrouter.ai/v1/images/generations
ModelToken groupBilling
gpt-image-2, gpt-image-2.5gpt_imagePer token
grok-imagine-image-2.0grokPer call

Gemini image models generate through the Gemini API.

Authentication ​

Authorization: Bearer <token>

Request parameters ​

ParameterTypeRequiredDescription
modelstringYesSee the table above
promptstringYesImage description
sizestringNoSize; the rules differ by model, see below
qualitystringNoGPT Image: low, medium, high, auto; gpt-image-2.5 also has xhigh and max. Billed as low if omitted
nintegerNoNumber of images GPT Image generates, 1 to 10; Grok generates 1 per call
output_formatstringNoGPT Image: png (default), jpeg, webp
output_compressionintegerNo0 to 100, for jpeg and webp
backgroundstringNoGPT Image: auto, opaque, transparent (transparent background needs png or webp)
moderationstringNoGPT Image: auto, low
response_formatstringNoOnly 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 ​

sizeActual outputPrice per image
Omitted / 1024x10241024×1024 JPEG$0.004
1536x10242496×1664 PNG$0.005
1024x15361664×2496 PNG$0.005
2048x20482048×2048 PNG$0.006
2048x11522816×1584 PNG$0.006

The default size returns JPEG and other sizes return PNG. Name the saved file by the actual format.

Request example ​

bash
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.png
python
import 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))
js
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 ​

json
{
  "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):

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