Skip to content

Gemini image generation ​

The Gemini image models run on the official Gemini channel and accept both the Gemini native format and the OpenAI chat format. Both formats hit the same models and bill per token.

Before you start ​

Use the Gemini group

When you create the API key, pick the Gemini group (Gemini). Gemini image and text models share this group, and image generation consumes both input and output tokens. Don't call these models through a reverse-engineered group.

Models and pricing ​

Prices below are for the Gemini group (multiplier 2, about 29% of the official list price), per 1M tokens.

ModelInputOutputFormats
gemini-3.1-flash-lite-image$0.071$8.57Gemini native / OpenAI chat
gemini-2.5-flash-image$0.086$8.57Gemini native / OpenAI chat
gemini-3.1-flash-image$0.14$17.14Gemini native / OpenAI chat
gemini-3.1-flash-image-preview$0.14$17.14Gemini native / OpenAI chat
gemini-3-pro-image$0.57$34.29Gemini native / OpenAI chat
gemini-3-pro-image-preview$0.57$34.29Gemini native / OpenAI chat

Models and prices may change. GET /v1/models, the console, and live pricing are the source of truth.

Gemini native format ​

Best for projects already on the Gemini SDK or Google's request shape.

ItemValue
MethodPOST
Path/v1beta/models/{model}:generateContent
Authx-goog-api-key: YOUR_API_KEY
Content-Typeapplication/json

Request example ​

bash
curl "https://ai.flashapi.top/v1beta/models/gemini-3.1-flash-image:generateContent" \
  -H "x-goog-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {"parts": [{"text": "A yellow lightning bolt icon on a pure white background"}]}
    ],
    "generationConfig": {
      "responseModalities": ["image"],
      "imageConfig": {
        "aspectRatio": "1:1",
        "imageSize": "1K"
      }
    }
  }'
ParameterDescription
modelFull model name in the URL, e.g. gemini-3.1-flash-image
contents[].parts[].textImage prompt
aspectRatioAspect ratio, e.g. 1:1, 9:16, 16:9
imageSizeResolution, e.g. 1K, 2K, 4K

Response format ​

The image comes back as a base64 string in inlineData.data.

json
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "inlineData": {
              "mimeType": "image/jpeg",
              "data": "<base64 string>"
            }
          }
        ]
      }
    }
  ]
}

Python example ​

python
import base64
import os

import requests

api_key = os.environ["FLASH_API_KEY"]
model = "gemini-3.1-flash-image"
response = requests.post(
    f"https://ai.flashapi.top/v1beta/models/{model}:generateContent",
    headers={"x-goog-api-key": api_key},
    json={
        "contents": [{"parts": [{"text": "A yellow lightning bolt icon on a pure white background"}]}],
        "generationConfig": {
            "responseModalities": ["image"],
            "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"},
        },
    },
    timeout=180,
)
response.raise_for_status()

parts = response.json()["candidates"][0]["content"]["parts"]
image = next(part["inlineData"] for part in parts if "inlineData" in part)
suffix = {"image/png": ".png", "image/jpeg": ".jpg"}.get(image["mimeType"], ".img")
with open(f"gemini-native{suffix}", "wb") as file:
    file.write(base64.b64decode(image["data"]))

OpenAI chat format ​

Best for projects already on the OpenAI SDK, chat.completions, or a compatible client.

ItemValue
MethodPOST
Path/v1/chat/completions
AuthAuthorization: Bearer YOUR_API_KEY
Content-Typeapplication/json

Request example ​

bash
curl https://ai.flashapi.top/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.1-flash-image",
    "messages": [
      {
        "role": "user",
        "content": [{"type": "text", "text": "A yellow lightning bolt icon on a pure white background"}]
      }
    ],
    "extra_body": {
      "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}
    },
    "max_tokens": 4096,
    "temperature": 0.8
  }'

The image is embedded in choices[0].message.content as Markdown image syntax:

json
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "![image](data:image/jpeg;base64,<base64 string>)"
      }
    }
  ]
}

Python example ​

python
import base64
import os
import re

import requests

api_key = os.environ["FLASH_API_KEY"]
response = requests.post(
    "https://ai.flashapi.top/v1/chat/completions",
    headers={"Authorization": f"Bearer {api_key}"},
    json={
        "model": "gemini-3.1-flash-image",
        "messages": [
            {"role": "user", "content": [{"type": "text", "text": "A yellow lightning bolt icon"}]}
        ],
        "extra_body": {"imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}},
        "max_tokens": 4096,
    },
    timeout=180,
)
response.raise_for_status()

content = response.json()["choices"][0]["message"]["content"]
match = re.search(r"data:(image/\w+);base64,([A-Za-z0-9+/=\n]+)", content)
if not match:
    raise ValueError("No image data in the response")

mime_type, encoded = match.groups()
encoded = encoded.replace("\n", "").strip()
encoded += "=" * (-len(encoded) % 4)
suffix = {"image/png": ".png", "image/jpeg": ".jpg"}.get(mime_type, ".img")
with open(f"gemini-openai{suffix}", "wb") as file:
    file.write(base64.b64decode(encoded))

Which format should you use? ​

Gemini native formatOpenAI chat format
Authx-goog-api-keyAuthorization: Bearer
Path/v1beta/models/{model}:generateContent/v1/chat/completions
Image configgenerationConfig.imageConfigextra_body.imageConfig
Image outputinlineData.dataMarkdown data URL
Best forGemini-ecosystem projectsOpenAI-compatible projects

Troubleshooting ​

SymptomFix
model_not_found or "no available channel"Use the full model name and make sure the API key belongs to the Gemini group
base64 decode fails with a padding errorAppend = until the string length is divisible by 4
Request times outTest with 1K and set your client timeout to 180 seconds or more
No streamed imageThe image endpoints don't stream. Wait for the full response

Next steps ​

One base URL for GPT, Claude and Gemini