Nano Banana (Gemini image models)
Call Gemini's image models with the native generateContent endpoint or the OpenAI chat format, to generate images from text or change reference images, with a chosen aspect ratio and resolution.
Gemini's image models (which Google calls Nano Banana) are called with the native Gemini generateContent endpoint, and can also go through the OpenAI-format chat endpoint. The native endpoint lets you set the output types, aspect ratio and resolution directly, so prefer it; the chat endpoint suits clients that only speak the OpenAI format. Which models you can use depends on your key's group (see Groups), and each model's price is in Model Square.
POST https://codaapi.com/v1beta/models/{model}:generateContent
POST https://codaapi.com/v1/chat/completions
These models can't use /v1/images/generations: on Gemini channels the gateway turns only the Imagen models into image generation requests, and other models fail straight away. Requests are synchronous: the response comes back once the image is ready, and a high resolution can take over a minute, so don't set your client's read timeout too short.
Models
These are the Gemini image models on the live site when this page was written. Models come and go, and Model Square is authoritative:
| Model | Billing | Groups |
|---|---|---|
gemini-2.5-flash-image, gemini-2.5-flash-image-preview, gemini-3-pro-image, gemini-3-pro-image-preview, gemini-3.1-flash-image, gemini-3.1-flash-image-preview, gemini-3.1-flash-lite-image, gemini-nano-banana-2.1 |
Per Request | NanoBanana-官转, Gemini-官转 |
gemini-2.5-flash-image-token, gemini-3-pro-image-token, gemini-3.1-flash-image-token, gemini-3.1-flash-lite-image-token, gemini-nano-banana-2.1-token |
Token-based | Gemini-官转 |
Compared with the model of the same name without the suffix, a -token model differs in how it's billed (by tokens instead of per request) and in its group (only Gemini-官转). To use a model, create your key in a group that includes it, or the request fails.
How to choose between the series:
| Series | What it's like |
|---|---|
3 Pro (gemini-3-pro-image, gemini-3-pro-image-preview) |
The best quality, for complex pictures, accurate text and brand consistency; supports 1K, 2K and 4K |
3.1 Flash (gemini-3.1-flash-image, gemini-3.1-flash-image-preview) |
General purpose, balancing speed and quality, and good at keeping several reference images consistent; the most aspect ratios, plus a 512 resolution |
3.1 Flash Lite (gemini-3.1-flash-lite-image) |
The fastest and cheapest, 1K only; not suited to several reference images or many rounds of editing |
2.5 Flash (gemini-2.5-flash-image, gemini-2.5-flash-image-preview) |
The previous generation, 1K only |
Request headers
| Name | Required | Description |
|---|---|---|
Authorization |
Yes | Bearer sk-…, your API key. The native endpoint also takes x-goog-api-key: sk-…, or ?key=sk-… added to the URL |
Content-Type |
Yes | application/json |
Request body
The gateway passes the request body to the upstream Gemini API. The tables below list only the fields commonly used for images, with the same values as the official Gemini API documentation; whether they take effect depends on the model.
Native endpoint
| Parameter | Type | Required | Description |
|---|---|---|---|
contents[].role |
string | No | user or model, to tell the turns apart when you edit over several turns |
contents[].parts[].text |
string | Yes | A text description of the image, or what to change |
contents[].parts[].inlineData.mimeType |
string | No | The reference image type: image/png, image/jpeg, image/webp and so on |
contents[].parts[].inlineData.data |
string | No | The base64-encoded reference image, without a data: prefix. For several reference images, add several inlineData parts |
generationConfig.responseModalities |
string[] | Yes | Output types, which must include IMAGE or only text comes back: ["TEXT", "IMAGE"] for both text and images, ["IMAGE"] for images only |
generationConfig.imageConfig.aspectRatio |
string | No | Aspect ratio: 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9 or 21:9; the 3.1 Flash series also has 1:4, 4:1, 1:8 and 8:1. A ratio the model doesn't support has no effect |
generationConfig.imageConfig.imageSize |
string | No | Resolution: 512 (3.1 Flash series only), 1K (default), 2K or 4K. Use a capital K; 2k is ignored. gemini-2.5-flash-image and gemini-3.1-flash-lite-image support 1K only, and a level the model doesn't support falls back to 1K |
generationConfig.temperature |
number | No | 0 to 2, 1 by default; higher is more random |
generationConfig.candidateCount |
integer | No | How many candidates to generate, 1 by default |
safetySettings[] |
array | No | Each item is {"category": …, "threshold": …}. The categories are HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT and HARM_CATEGORY_CIVIC_INTEGRITY, and the thresholds are BLOCK_NONE, BLOCK_ONLY_HIGH, BLOCK_MEDIUM_AND_ABOVE and BLOCK_LOW_AND_ABOVE. Google's blocks on the image itself (IMAGE_SAFETY and others) can still trigger with everything set to BLOCK_NONE, and can't be turned off |
The fields in generationConfig can also be written in snake case (response_modalities, image_config); the gateway accepts both.
Output pixels at 1K for some aspect ratios: 1:1 is 1024×1024, 16:9 is 1344×768, 9:16 is 768×1344, 4:3 is 1184×864 and 21:9 is 1536×672. 2K and 4K scale up at the same ratios; for example, 16:9 at 2K is 2752×1536.
Chat endpoint
| Parameter | Type | Required | Description |
|---|---|---|---|
model |
string | Yes | Model name, see Models |
messages |
array | Yes | Put the text description in a user message. Without reference images, content can be a plain string; with them, content is an array, with text as {"type": "text", "text": …} and images as {"type": "image_url", "image_url": {"url": …}}, either an image URL or a base64 data URL, which the gateway turns into Gemini inlineData |
stream |
boolean | No | Set it to true for a streamed response |
extra_body.google.image_config.aspect_ratio |
string | No | Aspect ratio, with the same values as aspectRatio on the native endpoint |
extra_body.google.image_config.image_size |
string | No | Resolution, with the same values as imageSize on the native endpoint |
extra_body is a field in the request body, and the gateway turns its google.image_config into Gemini's generationConfig.imageConfig. The fields inside must be in snake case: the gateway rejects aspectRatio, imageSize or imageConfig.
The chat endpoint can't set the output types; the gateway decides by model whether to ask for images. If the chat endpoint returns only text with no image, switch to the native endpoint and set responseModalities.
Request examples
Native endpoint examples
curl
curl "https://codaapi.com/v1beta/models/gemini-3-pro-image-preview:generateContent" \
-H "Authorization: Bearer $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{"role": "user", "parts": [{"text": "A city rooftop after rain with neon signs in the distance, cinematic lighting, photorealistic"}]}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {"aspectRatio": "16:9", "imageSize": "2K"}
}
}'
Python (pip install google-genai)
from google import genai
from google.genai import types
client = genai.Client(
api_key="sk-your-key",
http_options=types.HttpOptions(base_url="https://codaapi.com", api_version="v1beta"),
)
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents="A city rooftop after rain with neon signs in the distance, cinematic lighting, photorealistic",
config=types.GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"],
image_config=types.ImageConfig(aspect_ratio="16:9", image_size="2K"),
),
)
for part in response.candidates[0].content.parts:
if part.inline_data:
with open("rooftop.png", "wb") as f:
f.write(part.inline_data.data) # the SDK has already decoded it to bytes
elif part.text:
print(part.text)
Node.js (npm install @google/genai)
import fs from 'node:fs';
import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY,
httpOptions: { baseUrl: 'https://codaapi.com', apiVersion: 'v1beta' },
});
const response = await ai.models.generateContent({
model: 'gemini-3-pro-image-preview',
contents: 'A city rooftop after rain with neon signs in the distance, cinematic lighting, photorealistic',
config: {
responseModalities: ['TEXT', 'IMAGE'],
imageConfig: { aspectRatio: '16:9', imageSize: '2K' },
},
});
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) fs.writeFileSync('rooftop.png', Buffer.from(part.inlineData.data, 'base64'));
else if (part.text) console.log(part.text);
}
To change a reference image, put its base64 and the text description together in parts; for images only, set responseModalities to ["IMAGE"]:
IMG_B64=$(base64 < car.png | tr -d '\n')
curl "https://codaapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
-H "Authorization: Bearer $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"role": "user",
"parts": [
{"inlineData": {"mimeType": "image/png", "data": "'"$IMG_B64"'"}},
{"text": "Paint the car dark green and make the sky overcast, keep everything else the same"}
]
}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "16:9"}
}
}'
In Python (continuing the example above):
photo = types.Part.from_bytes(data=open("car.png", "rb").read(), mime_type="image/png")
response = client.models.generate_content(
model="gemini-3.1-flash-image-preview",
contents=[photo, "Paint the car dark green and make the sky overcast, keep everything else the same"],
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio="16:9"),
),
)
Chat endpoint examples
curl (change a reference image, with an aspect ratio and resolution)
curl https://codaapi.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-pro-image-preview",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "Add a puppy running on the lawn"},
{"type": "image_url", "image_url": {"url": "https://example.com/garden.jpg"}}
]
}],
"extra_body": {"google": {"image_config": {"aspect_ratio": "16:9", "image_size": "2K"}}}
}'
Python (pip install openai)
import base64
import re
from openai import OpenAI
client = OpenAI(base_url="https://codaapi.com/v1") # the key is read from the OPENAI_API_KEY environment variable
response = client.chat.completions.create(
model="gemini-3-pro-image-preview",
messages=[{"role": "user", "content": "An orange cat wearing headphones, flat illustration style, light green background"}],
# The SDK merges extra_body into the request body, so wrap it in one more "extra_body"
extra_body={"extra_body": {"google": {"image_config": {"aspect_ratio": "1:1", "image_size": "1K"}}}},
)
content = response.choices[0].message.content
images = re.findall(r"!\[image\]\(data:image/(\w+);base64,([^)]+)\)", content)
for i, (ext, data) in enumerate(images):
with open(f"cat-{i}.{ext}", "wb") as f:
f.write(base64.b64decode(data))
Node.js (npm install openai)
import fs from 'node:fs';
import OpenAI from 'openai';
const client = new OpenAI({ baseURL: 'https://codaapi.com/v1' }); // the key is read from the OPENAI_API_KEY environment variable
const response = await client.chat.completions.create({
model: 'gemini-3-pro-image-preview',
messages: [{ role: 'user', content: 'An orange cat wearing headphones, flat illustration style, light green background' }],
extra_body: { google: { image_config: { aspect_ratio: '1:1', image_size: '1K' } } },
});
const content = response.choices[0].message.content;
const images = [...content.matchAll(/!\[image\]\(data:image\/(\w+);base64,([^)]+)\)/g)];
images.forEach(([, ext, data], i) => fs.writeFileSync(`cat-${i}.${ext}`, Buffer.from(data, 'base64')));
With "stream": true the response is streamed, and the image arrives the same way, as a Markdown image in one of the delta.content pieces; join the pieces and extract it as above.
To edit over several turns, put the previous turn's content back into messages as it is, as an assistant message, then add a new user message saying what to change. The gateway turns the images in it back into Gemini image input.
Response
The native endpoint returns the upstream response as it is. The image is the item with inlineData in candidates[0].content.parts, and any text is in the items with text:
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{ "text": "Here is the city rooftop after rain." },
{ "inlineData": { "mimeType": "image/png", "data": "iVBORw0KGgoAAAANSUhEUgAA…" } }
]
},
"finishReason": "STOP"
}
],
"usageMetadata": { "promptTokenCount": 18, "candidatesTokenCount": 1300, "totalTokenCount": 1318 },
"modelVersion": "gemini-3-pro-image-preview"
}
| Field | Description |
|---|---|
candidates[0].content.parts[].inlineData.data |
The base64-encoded image; decode it and save it to a file |
candidates[0].content.parts[].inlineData.mimeType |
The image format, usually image/png |
candidates[0].content.parts[].text |
Text that came with the image |
candidates[0].finishReason |
STOP means it finished normally; MAX_TOKENS means it hit the output limit; SAFETY, IMAGE_SAFETY, PROHIBITED_CONTENT and OTHER mean it was blocked, with no image |
usageMetadata |
Token counts. Token-based models are charged by these |
promptFeedback.blockReason |
Present when the upstream blocked the prompt, in which case there are no candidates |
The chat endpoint writes the image as a Markdown image in choices[0].message.content, separated from any text by a line break; when blocked, finish_reason is content_filter:
{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Here is an orange cat wearing headphones.\n"
},
"finish_reason": "stop"
}
]
}
Errors
Errors from the gateway come back as an OpenAI-style body, {"error": {"message": …, "type": …, "code": …}}, for both endpoints, where error.message holds the reason and the ID of the request.
When a request succeeds but has no image:
| What you see | Cause and fix |
|---|---|
| Only text comes back | responseModalities on the native endpoint doesn't include IMAGE; with the chat endpoint, switch to the native endpoint |
You asked for 4K but got 1K |
The model doesn't support that level; use the 3 Pro series |
2k has no effect |
Use a capital K |
| The aspect ratio has no effect | The model doesn't support that ratio, see Request body |
finishReason is IMAGE_SAFETY or PROHIBITED_CONTENT |
Google's content policy blocked it; rewrite the prompt or use another reference image. safetySettings can't turn these blocks off |
For what each status code means and what to do, see Error codes.
Billing
Per-request models are charged once per request; -token models are charged by the tokens in the response's usageMetadata. For the rules, see Pricing and multipliers; for prices, see Model Square and the price list. The charge for each request is in the console under General → Usage Logs (open it directly).