What you will build
You will call the Gemini imagegenerateContent 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-previewas the text-to-image example.
API key, base URL, authentication
Use the Gemini image route through CometAPI:x-goog-api-key:
Code examples
Use the tabs below for copyable examples in cURL, Python, and Node.js.Flow explanation
Gemini image generation is synchronous on this route. The response uses Gemini nativecandidates[].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
The response contains thought images
The response contains thought images
Do not save parts where
thought is true. These are intermediate images, not the final output.The request returns text only
The request returns text only
Check that
generationConfig.responseModalities includes IMAGE, and that the selected model supports image output.I need image-to-image
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.Next steps
- Read the Gemini image API reference.
- See more examples in Use Gemini image models.
- Find available image models in Models.
- Estimate cost with Estimate request cost before calling a model.