Notebooks
G
Google Gemini
Get Started Imagen Rest

Get Started Imagen Rest

quickstartsgemini-cookbookgemini-apigemini
Copyright 2025 Google LLC.
[ ]

Gemini API: Get started with image generation - REST API

The Imagen models are Google's highest quality text-to-image models, featuring a number of new and improved capabilities. Imagen models can do the following:

  • Generate images with fine detail, rich lighting, and few distracting artifact
  • Understand prompts written in natural language
  • Generate images in a wide range of formats and styles
  • Render text effectively

You can choose from a range of available Imagen models:

  • Imagen 4 (imagen-4.0-generate-001) is the new stadard model you should use to generate.
  • Imagen 4 Ultra (imagen-4.0-ultra-generate-001) is the best Imagen model, generating even finer images and is especially good at generating images with text. Note that it can only generate one image at a time.
  • Imagen 4 Fast (imagen-4.0-fast-generate-001) is a faster model and generates at a lower cost compared to Imagen 4 and Ultra. See the pricing page for details.
  • Imagen 3 (imagen-3.0-generate-002) is the previous generation model. It's still available in case you need to rerun old prompts, but it is recommended to use the 4th generation models now.

This notebook is using the REST API. For the Python SDK, check out the Get Started with Imagen Python guide.

⚠️

Image generation is a paid-only feature and won't work if you are on the free tier. Check the pricing page for more details.

Set up your API key

To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY.

[ ]
[ ]

Send Imagen request

In the next code block, you can change the model_name, prompt, and a bunch of parameters. Then the request will be sent by curl.

The model is trained on long captions and will provide best results for longer and more descriptive prompts. Short prompts may result in low adherence and a more random output.

Check the prompt guide for more advices.

The optional parameters are:

  • sampleCount: How many images will be generated. Default is 4, valid values are 1-4.
  • aspectRatio: aspect ratio of the images produced. Supported values are 1:1, 3:4, 4:3, 16:9, 9:16. Default is 1:1.
  • personGeneration: Lets the model generate images with adults. Kids are always blocked. Supported values are "dont_allow" and "allow_adult". Default is "allow_adult".
  • imageSize: The output resolution of the image, either 1k (default) or 2k, for supported models.

A non-visible digital SynthID watermark is always added to generated images.

[ ]
model_name
prompt
sampleCount
1
aspectRatio
personGeneration
sampleImageSize
[ ]
Request successful!
Response: {'predictions': [{'bytesBase64Encoded': 'iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAADwf7zUAAAgAElEQVR4nMy9W5IsSY4spgrAPPJUz8yVe39IfnAZXAb3v4grJIUi', 'mimeType': 'image/png'}]}

Examine the metadata in the response

[ ]
Number of predictions: 1
Positive prompt:
  mimeType: image/png

Check the generated images

[ ]
Output

Next Steps

Useful documentation references:

To improve your prompting skills, check the prompt guide for great advices on creating your prompts.

Check those cool Imagen examples:

These examples are in Python but they will still give you ideas on how to use Imagen in creative ways:

  • Illustrate a book: Use Gemini and Imagen to create illustration for an open-source book

Continue your discovery of the Gemini API

Gemini is not only good at generating images, but also at understanding them. Check the Spatial understanding guide for an introduction on those capabilities, and the Video understanding one for video examples.