Skip to main content

Gemini generateContent Format

In this page's examples, {BASE_URL} is https://api.uniall.ai.

Use this format with Gemini-style clients or when you need Base64 image input and output. The model is part of the URL, and image controls belong in generationConfig.imageConfig.

Endpoint

POST /v1beta/models/{model}:generateContent

Replace {model} with nano-banana-2, nano-banana-pro, or nano-banana-2-lite.

Authentication

x-api-key: sk-***
Content-Type: application/json

The same UniAll API key works for the OpenAI Images and Gemini formats.

Request Parameters

ParameterTypeRequiredDescription
contentsarrayYesConversation content containing one user message.
contents[].rolestringYesUse user.
contents[].parts[].textstringYesGeneration or editing instruction.
contents[].parts[].inlineDataobjectConditionalReference image for editing. Repeat the part for multiple images.
inlineData.mimeTypestringConditionalImage media type, such as image/png.
inlineData.datastringConditionalRaw Base64 data without a Data URL prefix.
generationConfig.responseModalitiesstring[]YesUse ["TEXT", "IMAGE"] when text and image output are acceptable.
generationConfig.imageConfig.imageSizestringNo512, 1K, 2K, or 4K; defaults to 1K. Model limits apply.
generationConfig.imageConfig.aspectRatiostringNoOutput ratio such as 1:1, 3:4, or 16:9.
ModelSupported imageSize values
nano-banana-2512, 1K, 2K, 4K
nano-banana-pro1K, 2K, 4K
nano-banana-2-lite1K

Text-To-Image Example

curl -X POST "{BASE_URL}/v1beta/models/nano-banana-2:generateContent" \
-H "x-api-key: sk-***" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "A cinematic product photo of a clear perfume bottle on black stone, blue mist, no text"
}
]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {
"imageSize": "512",
"aspectRatio": "1:1"
}
}
}'

Image Editing Example

Encode the image as raw Base64 before building the JSON request:

IMAGE_BASE64="$(base64 < ./reference.png | tr -d '\n')"
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Keep the subject and composition; replace the background with a rainy neon street"
},
{
"inlineData": {
"mimeType": "image/png",
"data": "BASE64_IMAGE_DATA"
}
}
]
}
],
"generationConfig": {
"responseModalities": [
"TEXT",
"IMAGE"
],
"imageConfig": {
"imageSize": "2K",
"aspectRatio": "3:4"
}
}
}

Add another inlineData part for each additional reference image. Keep the image order consistent with the prompt.

Response Example

{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"text": "Image generated."
},
{
"inlineData": {
"mimeType": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUgAA..."
}
}
]
},
"finishReason": "STOP"
}
]
}

Find the part containing inlineData, Base64-decode its data, and save it using the returned mimeType. Some clients may normalize the field names to inline_data and mime_type.

Python Example

import base64
import requests

response = requests.post(
"https://api.uniall.ai/v1beta/models/nano-banana-2:generateContent",
headers={"x-api-key": "sk-***", "Content-Type": "application/json"},
json={
"contents": [{
"role": "user",
"parts": [{"text": "An orange cat wearing an astronaut helmet"}],
}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {"imageSize": "512", "aspectRatio": "1:1"},
},
},
timeout=600,
)
response.raise_for_status()

for candidate in response.json().get("candidates", []):
for part in candidate.get("content", {}).get("parts", []):
image = part.get("inlineData") or part.get("inline_data")
if image and image.get("data"):
with open("generated.png", "wb") as output:
output.write(base64.b64decode(image["data"]))
raise SystemExit(0)

raise RuntimeError("response did not contain an image")

Legacy Model IDs

Earlier Gemini preview IDs that encode a fixed resolution may remain accepted for compatibility. They are deprecated and may be removed at any time. New integrations should use a unified Nano model in the URL and select imageSize in the request.

Billing Notes

Billing depends on the model and imageSize. Check the current model price available to your account. The response may be large because it contains Base64 image data.

Common Errors

  • Sending imageSize: "0.5k" or "512K"; use "512" for 0.5K.
  • Requesting 512 from nano-banana-pro or nano-banana-2-lite.
  • Including data:image/png;base64, in inlineData.data.
  • Looking only at the first response part instead of finding the part with inlineData.
  • Logging the full Base64 response or API key.