Back to docs

Integration guide

Image generation with Virouter

Generate images using gpt-image-2 (Imagen 2) through Virouter's OpenAI-compatible endpoint. Use the OpenAI SDK or direct HTTP calls with your Virouter API key.

Quick start

Endpoint

https://api.virouter.com/v1/images/generations

Model ID

gpt-image-2

Supported sizes

1024x1024, 1024x1536, 1536x1024

Quality levels

low, medium, high

Node.js example

Use the OpenAI SDK's images.generate() method with Virouter baseURL.

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.VIROUTER_API_KEY,
  baseURL: "https://api.virouter.com/v1",
});

const response = await client.images.generate({
  model: "gpt-image-2",
  prompt: "A cat sitting on a beach at sunset",
  n: 1,
  size: "1024x1024",
  quality: "low", // low, medium, or high
});

console.log(response.data[0].url);
// or save base64: response.data[0].b64_json

Python example

The Python SDK uses the same images.generate() interface.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["VIROUTER_API_KEY"],
    base_url="https://api.virouter.com/v1",
)

response = client.images.generate(
    model="gpt-image-2",
    prompt="A minimalist tech logo with circuit paths",
    n=1,
    size="1024x1024",
    quality="medium",
)

print(response.data[0].url)
# or decode base64: response.data[0].b64_json

cURL example

Direct HTTP POST for testing or server-side scripts.

curl https://api.virouter.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VIROUTER_API_KEY" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A red circle on white background",
    "n": 1,
    "size": "1024x1024",
    "quality": "low"
  }'

Response format

By default, Virouter returns base64-encoded images in the b64_json field. Decode and save as PNG.

{
  "created": 1781055119,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAAA...",
      "revised_prompt": "A simple red circle centered on a plain white background..."
    }
  ],
  "model": "gpt-image-2",
  "usage": {
    "input_tokens": 24,
    "output_tokens": 196,
    "total_tokens": 220
  }
}

Quality vs speed

QualityGeneration timeUse case
low~30 secondsPrototyping, previews, low-cost
medium~60-90 secondsBalanced quality and speed
high~90-120 secondsProduction, high-fidelity outputs

Set your HTTP client timeout to at least 150 seconds to avoid premature disconnects during image generation.

Troubleshooting

502 Bad Gateway

Check that the model name is exactly gpt-image-2 and the path is /v1/images/generations.

Timeout

Image generation takes 30-90s depending on quality. Use quality=low for faster results (~30s).

402 Payment Required

Image generation costs more than text. Ensure your wallet has sufficient Billing USD Quota.

No upstream provider

The gpt-image-2 channel may be temporarily unavailable. Contact support or retry later.

Need help with text generation? Read the OpenAI SDK baseURL guide. Want to understand billing? Read the Billing USD Quota docs.