Back to Firebase Js Sdk

ImagenModel class

docs-devsite/ai.imagenmodel.md

12.12.14.7 KB
Original Source

Project: /docs/reference/js/_project.yaml Book: /docs/reference/_book.yaml page_type: reference

{% comment %} DO NOT EDIT THIS FILE! This is generated by the JS SDK team, and any local changes will be overwritten. Changes should be made in the source code at https://github.com/firebase/firebase-js-sdk {% endcomment %}

ImagenModel class

Warning: This API is now obsolete.

All Imagen models are deprecated and will shut down as early as June 2026. As a replacement, you can migrate your apps to use Gemini Image models (the "Nano Banana" models)<!-- -->.

Class for Imagen model APIs.

This class provides methods for generating images using the Imagen model.

<b>Signature:</b>

typescript
export declare class ImagenModel extends AIModel 

<b>Extends:</b> AIModel

Constructors

ConstructorModifiersDescription
(constructor)(ai, modelParams, requestOptions)Constructs a new instance of the ImagenModel class.

Properties

PropertyModifiersTypeDescription
generationConfigImagenGenerationConfigThe Imagen generation configuration.
requestOptionsRequestOptions | undefined
safetySettingsImagenSafetySettingsSafety settings for filtering inappropriate content.

Methods

MethodModifiersDescription
generateImages(prompt, singleRequestOptions)Generates images using the Imagen model and returns them as base64-encoded strings.

ImagenModel.(constructor)

Constructs a new instance of the ImagenModel class.

<b>Signature:</b>

typescript
constructor(ai: AI, modelParams: ImagenModelParams, requestOptions?: RequestOptions | undefined);

Parameters

ParameterTypeDescription
aiAIan AI instance.
modelParamsImagenModelParamsParameters to use when making requests to Imagen.
requestOptionsRequestOptions | undefinedAdditional options to use when making requests.

Exceptions

If the apiKey or projectId fields are missing in your Firebase config.

ImagenModel.generationConfig

The Imagen generation configuration.

<b>Signature:</b>

typescript
generationConfig?: ImagenGenerationConfig;

ImagenModel.requestOptions

<b>Signature:</b>

typescript
requestOptions?: RequestOptions | undefined;

ImagenModel.safetySettings

Safety settings for filtering inappropriate content.

<b>Signature:</b>

typescript
safetySettings?: ImagenSafetySettings;

ImagenModel.generateImages()

Generates images using the Imagen model and returns them as base64-encoded strings.

If the prompt was not blocked, but one or more of the generated images were filtered, the returned object will have a filteredReason property. If all images are filtered, the images array will be empty.

<b>Signature:</b>

typescript
generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;

Parameters

ParameterTypeDescription
promptstringA text prompt describing the image(s) to generate.
singleRequestOptionsSingleRequestOptions

<b>Returns:</b>

Promise<ImagenGenerationResponse<!-- --><ImagenInlineImage<!-- -->>>

A promise that resolves to an ImagenGenerationResponse object containing the generated images.

Exceptions

If the request to generate images fails. This happens if the prompt is blocked.

Example

javascript
const imagen = new ImagenModel(
  ai,
  {
    model: 'imagen-3.0-generate-002'
  }
);

const response = await imagen.generateImages('A photo of a cat');
if (response.images.length > 0) {
  console.log(response.images[0].bytesBase64Encoded);
}