Gemini Image API Integration Guide & Examples
| Model (used in the path) | Positioning | Available imageSize |
|---|---|---|
gemini-3-pro-image | Quality tier | 1K / 2K / 4K |
gemini-3.1-flash-image | Speed / cost balance | 512px / 1K / 2K / 4K |
gemini-3.1-flash-lite-image | Lowest cost | 1K only |
ModelGo accepts image generation requests on the Gemini API generateContent protocol: contents + generationConfig, returning candidates[].content.parts[].inlineData synchronously. The model name in the URL path is authoritative — the body does not carry a model.
With the Google GenAI SDK, point
http_options.base_urlathttps://api.modelgo.aiand use your ModelGo API key asapi_key. Bothx-goog-api-key: $KEYandAuthorization: Bearer $KEYare accepted.
>
Google's recommended newer Interactions API (
POST /v1beta/interactions) is also supported; see section 3.
1. Create an image
POST https://api.modelgo.ai/v1beta/models/{model}:generateContent (/v1/models/... is also accepted)
curl "https://api.modelgo.ai/v1beta/models/gemini-3.1-flash-image:generateContent" \
-H "x-goog-api-key: $MODELGO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"role": "user", "parts": [{"text": "A fluffy ginger cat on a sunlit windowsill, film grain"}]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"aspectRatio": "16:9", "imageSize": "2K"}
}
}'For image to image, put reference images in parts: {"inlineData": {"mimeType": "image/png", "data": "<base64>"}}, or {"fileData": {"fileUri": "https://..."}} (a public direct link the upstream can GET).
Response
{
"candidates": [{
"content": {"role": "model", "parts": [{"inlineData": {"mimeType": "image/png", "data": "iVBORw0KGgo..."}}]},
"finishReason": "STOP"
}],
"usageMetadata": {"promptTokenCount": 18, "candidatesTokenCount": 1680, "totalTokenCount": 1698}
}- The image is in
parts[].inlineData.data(base64). WhenresponseModalitiesincludesTEXT,partsmay also contain{"text": ...}segments. - If
partshas noinlineData, check thatresponseModalitiesincludesIMAGEand thatfinishReasonisSTOP.
Streaming
POST https://api.modelgo.ai/v1beta/models/{model}:streamGenerateContent is also supported, with the same body as generateContent:
- With
?alt=sseit returnstext/event-stream. Image models do not stream the image in chunks: the whole image (withusageMetadata) arrives in a single event. - Without
alt=sseit returns a JSON array. - Billing is identical to
generateContent.
2. Parameters
| Field | Description |
|---|---|
contents[].role | user (default) or model (a model turn in multi-turn editing) |
contents[].parts[] | text prompt; inlineData / fileData reference images |
systemInstruction | Optional system instruction |
generationConfig.responseModalities | ["IMAGE"] image only; ["TEXT","IMAGE"] interleaved text and images |
generationConfig.imageConfig.aspectRatio | All three models accept 1:1 / 2:3 / 3:2 / 3:4 / 4:3 / 4:5 / 5:4 / 9:16 / 16:9 / 21:9; gemini-3.1-flash-image also accepts 1:4 / 4:1 / 1:8 / 8:1 / 9:21, gemini-3.1-flash-lite-image also accepts 1:4 / 4:1 / 1:8 / 8:1 (not 9:21), and gemini-3-pro-image accepts only these ten (not 9:21). Other values return 400 |
generationConfig.imageConfig.imageSize | Resolution tier, per-model values in the first table, default 1K; 1k, 1024 and 1024px are aliases of 1K, with 1024 / 1024px normalized to 1K before forwarding. The tier sets the pixel scale and aspectRatio sets the exact width and height. A tier the model does not support returns 400 (e.g. 512px on gemini-3-pro-image, 512px / 2K / 4K on gemini-3.1-flash-lite-image) |
generationConfig.thinkingConfig | thinkingLevel: minimal / low / medium / high; when omitted, Google's documented per-model default applies (e.g. minimal on gemini-3.1-flash-lite-image). Thinking tokens are reported in usageMetadata.thoughtsTokenCount and billed as output tokens |
generationConfig.mediaResolution / generationConfig.responseFormat | Supported, forwarded to Google |
tools | Only [{"google_search": {}}] (Search Grounding). Supported on gemini-3-pro-image and gemini-3.1-flash-image, not on gemini-3.1-flash-lite-image; returns 400 provider_unsupported when not available. When enabled, the response carries candidates[0].groundingMetadata |
These return 400: candidateCount > 1 (one image per request), cachedContent, and responseSchema or a responseMimeType other than text/plain.
3. Interactions API (Google's recommended newer API)
POST https://api.modelgo.ai/v1beta/interactions, for gemini-3-pro-image, gemini-3.1-flash-image and gemini-3.1-flash-lite-image. The model goes in the body's model field and the auth headers are the same as for generateContent; whether a model offers this endpoint follows the protocols listed in the Model Catalog.
Text to image:
curl "https://api.modelgo.ai/v1beta/interactions" \
-H "x-goog-api-key: $MODELGO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image",
"input": "A fluffy ginger cat on a sunlit windowsill, film grain",
"response_format": {"type": "image", "aspect_ratio": "1:1", "image_size": "1K"}
}'Image editing: pass input as an array of contents, with the reference image as {"type": "image", "data": "<base64>", "mime_type": "image/png"}, or a public direct link in uri:
{
"model": "gemini-3.1-flash-image",
"input": [
{"type": "text", "text": "Replace the background with a neon night street, keep the cat's pose"},
{"type": "image", "data": "<base64>", "mime_type": "image/png"}
],
"response_format": {"type": "image", "aspect_ratio": "1:1", "image_size": "1K"}
}Multi-turn editing is stateless: put the previous response's steps as-is into the next request's input, then append a user_input step such as {"type": "user_input", "content": [{"type": "text", "text": "Add a small hat"}]}.
Response
{
"id": "<request ID>",
"object": "interaction",
"model": "gemini-3.1-flash-image",
"status": "completed",
"created": "2026-11-12T08:00:00Z",
"updated": "2026-11-12T08:00:00Z",
"steps": [
{"type": "model_output", "content": [{"type": "image", "mime_type": "image/png", "data": "iVBORw0KGgo..."}]}
],
"usage": {"total_input_tokens": 18, "total_cached_tokens": 0, "total_output_tokens": 1680, "total_thought_tokens": 0, "total_tool_use_tokens": 0, "total_tokens": 1698}
}- The image is in
content[].data(base64) of themodel_outputstep.stepsmay also contain athoughtstep (with asignature); in multi-turn editing send it back ininputalong with the rest. statusis normallycompleted;incompletewhen the model stopped early (e.g. a safety block);failed, witherrors, when there is no result.idis the request ID, for troubleshooting only. Interactions are not stored and cannot be retrieved later.
Streaming
With "stream": true the response is text/event-stream: interaction.created, then step.start / step.delta / step.stop for each step, then interaction.completed (with the final status and usage), then done. As with streamGenerateContent, the whole image arrives in one step.delta, not in chunks.
Limits
aspect_ratio/image_sizevalues per model are the same as forgenerateContent(see section 2), e.g.1Konly ongemini-3.1-flash-lite-image; unsupported values return400.- These return
400naming the field:previous_interaction_id,store: true,background: true,tools,agent/agent_config,environment,webhook_config; audio / video types anddelivery: "uri"inresponse_format;audioinput, and video / document input given as an http(s)uri. - Interactions are not stored:
GET/DELETE/v1beta/interactions/{id}andPOST /v1beta/interactions/{id}/cancelall return400. - Billing is identical to
generateContent.
4. Full example (Python, Google GenAI SDK)
import os
from google import genai
from google.genai import types
client = genai.Client(api_key=os.environ["MODELGO_API_KEY"],
http_options={"base_url": "https://api.modelgo.ai"})
resp = client.models.generate_content(
model="gemini-3.1-flash-image",
contents="A fluffy ginger cat on a sunlit windowsill, film grain",
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio="16:9", image_size="2K"),
),
)
for part in resp.candidates[0].content.parts:
if part.inline_data:
open("cat.png", "wb").write(part.inline_data.data)5. Billing
Billed per token (text and image input and output priced separately), from the response usageMetadata (usage on the Interactions API); calls with Search Grounding enabled add a charge per actual search query. Unit prices are in the console's model list. Error handling: Errors & Rate Limits.