Skip to main content

What you will build

You will call the Gemini image generateContent route through CometAPI, request an image output from Gemini image models including Nano Banana model families, skip intermediate thought image parts, and save the final inlineData image.

Prerequisites

  • A CometAPI API key stored in COMETAPI_KEY
  • Python 3.10+ with requests, or Node.js 18+
  • A Gemini image model ID. The maintained API reference uses gemini-3.1-flash-image-preview as the text-to-image example.

API key, base URL, authentication

Use the Gemini image route through CometAPI:
https://api.cometapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent
Authenticate with x-goog-api-key:
x-goog-api-key: $COMETAPI_KEY

Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.
curl -s -X POST \
  "https://api.cometapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent" \
  -H "x-goog-api-key: $COMETAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "A Monarch butterfly anatomical sketch on textured parchment"
          }
        ]
      }
    ],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"],
      "imageConfig": {
        "aspectRatio": "1:1",
        "imageSize": "4K"
      }
    }
  }'

Flow explanation

Gemini image generation is synchronous on this route. The response uses Gemini native candidates[].content.parts[]. Parts can include text, generated images, and intermediate image parts where thought is true. When saving the result, iterate through image parts, ignore thought: true, and save the last remaining inlineData image.

Common parameters

ParameterUse
model path segmentGemini image model ID in the URL path.
contentsPrompt and optional input image parts.
generationConfig.responseModalitiesInclude IMAGE when you need image output.
generationConfig.imageConfigImage options such as aspect ratio and image size when supported by the selected model.
toolsOptional Gemini tools. Add only when the API reference documents the selected workflow.

Troubleshooting / FAQ

Do not save parts where thought is true. These are intermediate images, not the final output.
Check that generationConfig.responseModalities includes IMAGE, and that the selected model supports image output.
Use inline_data request parts as shown in the Gemini image guide, and keep the final-image parsing logic the same.

Next steps