> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini image API quickstart: Generate Nano Banana images with CometAPI

> Generate images with Gemini image models through CometAPI, including Nano Banana model families, and save inlineData output with curl, Python, or Node.js.

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

```text theme={null}
https://api.cometapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent
```

Authenticate with `x-goog-api-key`:

```text theme={null}
x-goog-api-key: $COMETAPI_KEY
```

## Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.

<CodeGroup>
  ```bash cURL theme={null}
  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"
        }
      }
    }'
  ```

  ```python Python theme={null}
  import base64
  import os
  import requests

  response = requests.post(
      "https://api.cometapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
      headers={
          "x-goog-api-key": os.environ["COMETAPI_KEY"],
          "Content-Type": "application/json",
      },
      json={
          "contents": [
              {
                  "parts": [
                      {
                          "text": "A Monarch butterfly anatomical sketch on textured parchment"
                      }
                  ]
              }
          ],
          "generationConfig": {
              "responseModalities": ["TEXT", "IMAGE"],
              "imageConfig": {"aspectRatio": "1:1", "imageSize": "4K"},
          },
      },
      timeout=120,
  )
  response.raise_for_status()

  parts = response.json()["candidates"][0]["content"]["parts"]
  for part in reversed(parts):
      if part.get("thought") is True:
          continue
      inline_data = part.get("inlineData")
      if inline_data:
          image_bytes = base64.b64decode(inline_data["data"])
          mime_type = inline_data.get("mimeType", "image/png")
          extension = "jpg" if mime_type == "image/jpeg" else "png"
          with open(f"nano-banana-result.{extension}", "wb") as file:
              file.write(image_bytes)
          break
  else:
      raise RuntimeError("No final image part found")
  ```

  ```javascript Node.js theme={null}
  import fs from "node:fs/promises";

  const response = await fetch(
    "https://api.cometapi.com/v1beta/models/gemini-3.1-flash-image-preview:generateContent",
    {
      method: "POST",
      headers: {
        "x-goog-api-key": process.env.COMETAPI_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        contents: [
          {
            parts: [
              {
                text: "A Monarch butterfly anatomical sketch on textured parchment",
              },
            ],
          },
        ],
        generationConfig: {
          responseModalities: ["TEXT", "IMAGE"],
          imageConfig: { aspectRatio: "1:1", imageSize: "4K" },
        },
      }),
    },
  );

  if (!response.ok) {
    throw new Error(await response.text());
  }

  const body = await response.json();
  const parts = body.candidates[0].content.parts;
  const finalImage = [...parts]
    .reverse()
    .find((part) => part.thought !== true && part.inlineData);

  if (!finalImage) {
    throw new Error("No final image part found");
  }

  const mimeType = finalImage.inlineData.mimeType || "image/png";
  const extension = mimeType === "image/jpeg" ? "jpg" : "png";
  await fs.writeFile(
    `nano-banana-result.${extension}`,
    Buffer.from(finalImage.inlineData.data, "base64"),
  );
  ```
</CodeGroup>

## 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

| Parameter                             | Use                                                                                     |
| ------------------------------------- | --------------------------------------------------------------------------------------- |
| `model` path segment                  | Gemini image model ID in the URL path.                                                  |
| `contents`                            | Prompt and optional input image parts.                                                  |
| `generationConfig.responseModalities` | Include `IMAGE` when you need image output.                                             |
| `generationConfig.imageConfig`        | Image options such as aspect ratio and image size when supported by the selected model. |
| `tools`                               | Optional Gemini tools. Add only when the API reference documents the selected workflow. |

## Troubleshooting / FAQ

<AccordionGroup>
  <Accordion title="The response contains thought images">
    Do not save parts where `thought` is `true`. These are intermediate images, not the final output.
  </Accordion>

  <Accordion title="The request returns text only">
    Check that `generationConfig.responseModalities` includes `IMAGE`, and that the selected model supports image output.
  </Accordion>

  <Accordion title="I need image-to-image">
    Use `inline_data` request parts as shown in the Gemini image guide, and keep the final-image parsing logic the same.
  </Accordion>
</AccordionGroup>

## Next steps

* Read the [Gemini image API reference](/api/image/gemini/gemini-generates-image).
* See more examples in [Use Gemini image models](/api/image/gemini/generate-image-guide).
* Find available image models in [Models](/overview/models).
* Estimate cost with [Estimate request cost before calling a model](/guides/how-to-estimate-cost-before-calling-a-model).
