Gemini Image API Integration Guide & Examples

Model (used in the path)PositioningAvailable imageSize
gemini-3-pro-imageQuality tier1K / 2K / 4K
gemini-3.1-flash-imageSpeed / cost balance512px / 1K / 2K / 4K
gemini-3.1-flash-lite-imageLowest cost1K 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_url at https://api.modelgo.ai and use your ModelGo API key as api_key. Both x-goog-api-key: $KEY and Authorization: Bearer $KEY are 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)

bash
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

json
{
  "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). When responseModalities includes TEXT, parts may also contain {"text": ...} segments.
  • If parts has no inlineData, check that responseModalities includes IMAGE and that finishReason is STOP.

Streaming

POST https://api.modelgo.ai/v1beta/models/{model}:streamGenerateContent is also supported, with the same body as generateContent:

  • With ?alt=sse it returns text/event-stream. Image models do not stream the image in chunks: the whole image (with usageMetadata) arrives in a single event.
  • Without alt=sse it returns a JSON array.
  • Billing is identical to generateContent.

2. Parameters

FieldDescription
contents[].roleuser (default) or model (a model turn in multi-turn editing)
contents[].parts[]text prompt; inlineData / fileData reference images
systemInstructionOptional system instruction
generationConfig.responseModalities["IMAGE"] image only; ["TEXT","IMAGE"] interleaved text and images
generationConfig.imageConfig.aspectRatioAll 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.imageSizeResolution 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.thinkingConfigthinkingLevel: 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.responseFormatSupported, forwarded to Google
toolsOnly [{"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:

bash
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:

json
{
  "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

json
{
  "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 the model_output step. steps may also contain a thought step (with a signature); in multi-turn editing send it back in input along with the rest.
  • status is normally completed; incomplete when the model stopped early (e.g. a safety block); failed, with errors, when there is no result.
  • id is 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_size values per model are the same as for generateContent (see section 2), e.g. 1K only on gemini-3.1-flash-lite-image; unsupported values return 400.
  • These return 400 naming the field: previous_interaction_id, store: true, background: true, tools, agent / agent_config, environment, webhook_config; audio / video types and delivery: "uri" in response_format; audio input, and video / document input given as an http(s) uri.
  • Interactions are not stored: GET / DELETE /v1beta/interactions/{id} and POST /v1beta/interactions/{id}/cancel all return 400.
  • Billing is identical to generateContent.

4. Full example (Python, Google GenAI SDK)

python
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.