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.
| Model | Input | Output | Formats |
|---|---|---|---|
gemini-3.1-flash-lite-image | $0.071 | $8.57 | Gemini native / OpenAI chat |
gemini-2.5-flash-image | $0.086 | $8.57 | Gemini native / OpenAI chat |
gemini-3.1-flash-image | $0.14 | $17.14 | Gemini native / OpenAI chat |
gemini-3.1-flash-image-preview | $0.14 | $17.14 | Gemini native / OpenAI chat |
gemini-3-pro-image | $0.57 | $34.29 | Gemini native / OpenAI chat |
gemini-3-pro-image-preview | $0.57 | $34.29 | Gemini 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.
| Item | Value |
|---|---|
| Method | POST |
| Path | /v1beta/models/{model}:generateContent |
| Auth | x-goog-api-key: YOUR_API_KEY |
| Content-Type | application/json |
Request example
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"
}
}
}'| Parameter | Description |
|---|---|
model | Full model name in the URL, e.g. gemini-3.1-flash-image |
contents[].parts[].text | Image prompt |
aspectRatio | Aspect ratio, e.g. 1:1, 9:16, 16:9 |
imageSize | Resolution, e.g. 1K, 2K, 4K |
Response format
The image comes back as a base64 string in inlineData.data.
{
"candidates": [
{
"content": {
"parts": [
{
"inlineData": {
"mimeType": "image/jpeg",
"data": "<base64 string>"
}
}
]
}
}
]
}Python example
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.
| Item | Value |
|---|---|
| Method | POST |
| Path | /v1/chat/completions |
| Auth | Authorization: Bearer YOUR_API_KEY |
| Content-Type | application/json |
Request example
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:
{
"choices": [
{
"message": {
"role": "assistant",
"content": ""
}
}
]
}Python example
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 format | OpenAI chat format | |
|---|---|---|
| Auth | x-goog-api-key | Authorization: Bearer |
| Path | /v1beta/models/{model}:generateContent | /v1/chat/completions |
| Image config | generationConfig.imageConfig | extra_body.imageConfig |
| Image output | inlineData.data | Markdown data URL |
| Best for | Gemini-ecosystem projects | OpenAI-compatible projects |
Troubleshooting
| Symptom | Fix |
|---|---|
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 error | Append = until the string length is divisible by 4 |
| Request times out | Test with 1K and set your client timeout to 180 seconds or more |
| No streamed image | The image endpoints don't stream. Wait for the full response |