Back to blog

How to Generate Images with One API (2026 Guide)

A practical AI image generation API tutorial: call gpt-image-2, Gemini, and Seedream through one OpenAI-compatible endpoint, pick sizes and quality, edit existing images, and see the exact cost of every generation.

A glowing image frame being generated on a circuit board, representing an AI image generation API

Every image model ships behind a different API. OpenAI's gpt-image-2 wants one SDK, Gemini image models another, Seedream a third — each with its own auth, parameters, and billing. If you want to compare outputs or switch models later, you rewrite your integration every time.

LLM Gateway solves this with one OpenAI-compatible image generation API. POST /v1/images/generations reaches every image model in the catalog — browse them with the image filter — with one API key and one response format.

Generate your first image

Grab an API key from the dashboard, then:

1curl -X POST "https://api.llmgateway.io/v1/images/generations" \2  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \3  -H "Content-Type: application/json" \4  -d '{5    "model": "gemini-3-pro-image-preview",6    "prompt": "A cute cat wearing a tiny top hat",7    "n": 1,8    "size": "1024x1024"9  }'

The response contains base64-encoded images:

1{2  "created": 1753500000,3  "data": [{ "b64_json": "iVBORw0KGgo..." }]4}

Leave model out (or pass "auto") and the gateway picks a strong default image model for you.

Use your existing OpenAI SDK

Because the endpoint is OpenAI-compatible, the standard client works — change the base URL and nothing else:

1import OpenAI from "openai";2import { writeFileSync } from "fs";3
4const client = new OpenAI({5  baseURL: "https://api.llmgateway.io/v1",6  apiKey: process.env.LLM_GATEWAY_API_KEY,7});8
9const response = await client.images.generate({10  model: "gpt-image-2",11  prompt: "A futuristic city skyline at sunset with flying cars",12  n: 1,13  size: "1536x1024",14});15
16response.data.forEach((image, i) => {17  if (image.b64_json) {18    writeFileSync(`image-${i}.png`, Buffer.from(image.b64_json, "base64"));19  }20});

Switching from gpt-image-2 to a Gemini or Seedream model is a one-line change to model — same request, same response shape.

The parameters that matter

Parameter What it does
prompt Required. Text description of the image
model Any image model; defaults to auto
n 1–10 images per request
size Pixel dimensions like 1024x1024 or 1536x1024, model-dependent
quality low, medium, high, or auto on models that support it
aspect_ratio 1:1, 16:9, 4:3, … — takes precedence over size where supported
style vivid or natural

Two model-specific notes worth knowing: aspect_ratio works on Gemini image models while OpenAI's gpt-image-2 ignores it (use an exact size instead), and quality applies to gpt-image-2. The image generation docs list per-model behavior.

Edit existing images

POST /v1/images/edits takes one or more input images (HTTPS URLs or base64 data URLs) plus an edit prompt:

1curl -X POST "https://api.llmgateway.io/v1/images/edits" \2  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \3  -H "Content-Type: application/json" \4  -d '{5    "images": [{ "image_url": "https://example.com/product.png" }],6    "prompt": "Place the product on a marble table with soft window light",7    "quality": "high"8  }'

You can control background (transparent, opaque, auto), output_format (png, jpeg, webp), and output_compression for the encoded result.

Using the Vercel AI SDK

If you build with the AI SDK, the @llmgateway/ai-sdk-provider package supports generateImage directly:

1import { createLLMGateway } from "@llmgateway/ai-sdk-provider";2import { generateImage } from "ai";3
4const llmgateway = createLLMGateway({5  apiKey: process.env.LLM_GATEWAY_API_KEY,6});7
8const result = await generateImage({9  model: llmgateway.image("gemini-3-pro-image-preview"),10  prompt: "A cozy cabin in a snowy mountain landscape at night",11  size: "1024x1024",12});

See the AI SDK developer docs for chat-based image streaming with useChat.

Know what every image costs

Every generation is logged with its exact cost. The Activity page shows per-request image output costs, and the dashboard rolls them up by model and project — so you can compare not just output quality across models, but price per image. Set a spending limit on the API key if you want a hard cap.

Frequently Asked Questions

Which image generation models can I use through the API?

Every model on the models page with the image filter — including OpenAI, Google, ByteDance, Alibaba, xAI, and more — through the same endpoint. The list updates as new models ship.

Do I need separate API keys per provider?

No. One LLM Gateway key covers every provider. You can bring your own provider keys for free, or use pay-as-you-go credits with a flat 5% platform fee.

Can I generate images conversationally?

Yes. Multimodal models like gemini-3-pro-image-preview can return images through /v1/chat/completions, which is useful for iterative editing in a chat UI. The image generation docs cover the chat flow.

Is there a way to try models before writing code?

The Image Studio in Lounge generates 1, 2, or 4 images per prompt and lets you compare models side by side.


Get started: