Docs / Image Series / Nano Banana / Sync - Native Format
Native nano-banana format (synchronous)
Synchronous image generation in Google Gemini Nano Banana's native generateContent format. Supports text-to-image, image-to-image, and aspect_ratio/image_size configuration, returning inline_data base64 directly.
Nano Banana is Google Gemini's native image generation capability, currently offered as gemini-3.1-flash-image (Nano Banana 2) / gemini-3-pro-image (Nano Banana Pro) / gemini-3.1-flash-lite-image (Nano Banana 2 Lite), gemini-nano-banana-2.1 (Nano Banana 2.1), plus gemini-2.5-flash-image (first-generation Nano Banana). This API uses the native Gemini `generateContent` format: the call is synchronous and returns the result directly (base64 inlined in the response), with no task polling needed.
- This API is in the native Gemini format (/v1beta/models/<model>:generateContent), which differs from the OpenAI-compatible format. For OpenAI-style async tasks (/v1/images/async-generationsreturning a task_id to poll), see Nano Banana 2 async generation. - The generated image is inlined directly in the responsepartsasinline_data(base64); decode and save it promptly. - This model generates only 1 image per request.
Authentication#
All endpoints require Bearer token authentication (the gateway accepts both x-goog-api-key and Authorization: Bearer). Create your key on the API Key management page and add it to the request header:
Authorization: Bearer YOUR_API_KEYCreate a task#
POST /v1beta/models/{model}:generateContent
- Set
{model}to any of the models below; for each model, thegemini-*name and itsnano-banana-*alias are equivalent: gemini-3.1-flash-image/nano-banana-2(Nano Banana 2)gemini-3-pro-image/nano-banana-pro(Nano Banana Pro)gemini-3.1-flash-lite-image/nano-banana-2-lite(Nano Banana 2 Lite)gemini-nano-banana-2.1/nano-banana-2.1(Nano Banana 2.1)gemini-2.5-flash-image(first-generation Nano Banana)- The request body is
application/jsonand structurally identical to Gemini generateContent.
The simplest text-to-image request only needs a text parts entry inside contents and declares image output in generationConfig.responseModalities:
{
"contents": [
{
"role": "user",
"parts": [{ "text": "A cat playing on the grass" }]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"]
}
}Specify aspect ratio and clarity (image_config uses snake_case; the gateway converts it to camelCase before passing it upstream):
{
"contents": [
{ "role": "user", "parts": [{ "text": "A cinematic wide-angle shot of a futuristic city skyline at dusk" }] }
],
"generationConfig": {
"responseModalities": ["IMAGE"],
"image_config": {
"aspect_ratio": "16:9",
"image_size": "2K"
}
}
}Image-to-image / image editing (pass reference images as inline_data inside parts):
{
"contents": [
{
"role": "user",
"parts": [
{ "text": "Add a cute kitten next to her" },
{
"inline_data": {
"mime_type": "image/png",
"data": "<BASE64_IMAGE_DATA>"
}
}
]
}
],
"generationConfig": {
"responseModalities": ["IMAGE"]
}
}Request parameters#
The top level of the request body consists of contents (the conversation) and generationConfig (generation settings).
contents#
| Field | Type | Required | Description |
|---|---|---|---|
contents | array | Yes | Array of conversation entries, each containing role and parts |
contents[].role | string | No | user / model |
contents[].parts[] | array | Yes | Content blocks, either text or inline_data |
parts (content blocks)#
| Field | Type | Description |
|---|---|---|
text | string | Text prompt describing the image to generate or edit |
inline_data | object | Reference image (image-to-image / editing), containing mime_type and data (base64) |
inline_data.mime_type | string | Image MIME type: image/png, image/jpeg, image/webp |
inline_data.data | string | Base64-encoded image (without the data: prefix) |
generationConfig#
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
responseModalities | array | Yes | — | Output modalities; use ["IMAGE"] or ["TEXT","IMAGE"] for image generation |
image_config | object | No | — | Image configuration (snake_case; the gateway converts it to camelCase) |
image_config.aspect_ratio | string | No | 1:1 | Aspect ratio; see the values below |
image_config.image_size | string | No | 1K | Clarity 1K/2K/4K (3.1 Flash Image also supports 512) |
Field naming: Native Gemini REST uses snake_case (inline_data,mime_type,image_config,aspect_ratio,image_size); the gateway accepts snake_case and converts it to camelCase before passing it upstream. Do not mix inimageConfig/aspectRatio(camelCase), or the request will fail.
aspect_ratio#
| Value | Description |
|---|---|
1:1 | Square (default) |
16:9, 9:16 | Landscape / portrait |
21:9, 9:21 | Ultrawide / ultratall |
4:3, 3:4, 3:2, 2:3 | Common ratios |
4:5, 5:4 | Portrait / landscape (social media) |
1:4, 4:1, 1:8, 8:1 | Ultra-flat ratios (3.1 Flash Image only) |
Request examples#
Text-to-image:
curl -s -X POST "https://api.apimax.ai/v1beta/models/gemini-3.1-flash-image:generateContent" -H "Authorization: Bearer <token>" -H "Content-Type: application/json" -d '{
"contents": [
{ "role": "user", "parts": [{ "text": "A cat playing on the grass" }] }
],
"generationConfig": {
"responseModalities": ["IMAGE"],
"image_config": { "aspect_ratio": "1:1", "image_size": "2K" }
}
}'Image-to-image (Python requests, converting a local image to base64):
import requests, base64
with open("input.png", "rb") as f:
img_b64 = base64.b64encode(f.read()).decode("utf-8")
url = "https://api.apimax.ai/v1beta/models/gemini-3.1-flash-image:generateContent"
headers = {"Authorization": "Bearer <token>", "Content-Type": "application/json"}
payload = {
"contents": [{
"role": "user",
"parts": [
{"text": "Add a cute kitten next to her"},
{"inline_data": {"mime_type": "image/png", "data": img_b64}},
],
}],
"generationConfig": {"responseModalities": ["IMAGE"]},
}
resp = requests.post(url, headers=headers, json=payload)
print(resp.json())Response example#
Returns the Gemini GenerateContentResponse structure synchronously; the generated image is inlined as inline_data inside candidates[].content.parts[]:
{
"candidates": [
{
"content": {
"parts": [
{
"inline_data": {
"mime_type": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUgAA..."
}
}
],
"role": "model"
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 12,
"candidatesTokenCount": 4160,
"totalTokenCount": 4172
}
}Base64-decode candidates[0].content.parts[].inline_data.data to get the image binary. If parts also contains text, it is explanatory text returned by the model.
Error codes#
| Status code | Meaning |
|---|---|
| 400 | Invalid request parameters (missing responseModalities, unreachable inline_data, unsupported MIME type, mistakenly using camelCase field names, etc.) |
| 401 | Authentication failed; check your API key |
| 403 | Insufficient account balance; top up and try again |
| 403 | The token does not have access to this model |
| 429 | Too many requests; try again later |
| 500 | Internal server error; try again later |
Common errors:
extra_body.google.imageConfig is not supported, use extra_body.google.image_config instead: The field mistakenly uses camelCase; use the snake_caseimage_configinstead.imageConfig.aspectRatio is not supported: Same as above; useaspect_ratioinstead.

