Get started with the Gemini API using the Vertex AI in Firebase SDKs

This guide shows you how to get started making calls to the Vertex AI Gemini API directly from your app using the Vertex AI in Firebase SDK for your chosen platform.

Note that you can also use this guide to get started with accessing Imagen models using the Vertex AI in Firebase SDKs.

Prerequisites

Swift

This guide assumes that you're familiar with using Xcode to develop apps for Apple platforms (like iOS).

  • Make sure that your development environment and Apple platforms app meet these requirements:

    • Xcode 16.2 or higher
    • Your app targets iOS 15 or higher, or macOS 12 or higher
  • (Optional) Check out the sample app.

    Download the quickstart app

    You can try out the SDK quickly, see a complete implementation of various use cases, or use the sample app if don't have your own Apple platforms app. To use the sample app, you'll need to connect it to a Firebase project.

Kotlin

This guide assumes that you're familiar with using Android Studio to develop apps for Android.

  • Make sure that your development environment and Android app meet these requirements:

    • Android Studio (latest version)
    • Your app targets API level 21 or higher
  • (Optional) Check out the sample app.

    Download the sample app

    You can try out the SDK quickly, see a complete implementation of various use cases, or use the sample app if don't have your own Android app. To use the sample app, you'll need to connect it to a Firebase project.

Java

This guide assumes that you're familiar with using Android Studio to develop apps for Android.

  • Make sure that your development environment and Android app meet these requirements:

    • Android Studio (latest version)
    • Your app targets API level 21 or higher
  • (Optional) Check out the sample app.

    Download the sample app

    You can try out the SDK quickly, see a complete implementation of various use cases, or use the sample app if don't have your own Android app. To use the sample app, you'll need to connect it to a Firebase project.

Web

This guide assumes that you're familiar with using JavaScript to develop web apps. This guide is framework-independent.

  • Make sure that your development environment and web app meet these requirements:

    • (Optional) Node.js
    • Modern web browser
  • (Optional) Check out the sample app.

    Download the sample app

    You can try out the SDK quickly, see a complete implementation of various use cases, or use the sample app if don't have your own web app. To use the sample app, you'll need to connect it to a Firebase project.

Dart

This guide assumes that you're familiar with developing apps with Flutter.

  • Make sure that your development environment and Flutter app meet these requirements:

    • Dart 3.2.0+
  • (Optional) Check out the sample app.

    Download the sample app

    You can try out the SDK quickly, see a complete implementation of various use cases, or use the sample app if don't have your own Flutter app. To use the sample app, you'll need to connect it to a Firebase project.

Step 1: Set up a Firebase project and connect your app to Firebase

If you already have a Firebase project and an app connected to Firebase

  1. In the Firebase console, go to the Vertex AI page.

  2. Click the Vertex AI in Firebase card to launch a workflow that helps you complete the following tasks:

  3. Continue to the next step in this guide to add the SDK to your app.

If you do not already have a Firebase project and an app connected to Firebase


Step 2: Add the SDK

With your Firebase project set up and your app connected to Firebase (see previous step), you can now add the Vertex AI in Firebase SDK to your app.

Swift

Use Swift Package Manager to install and manage Firebase dependencies.

The Vertex AI in Firebase library provides access to the APIs for interacting with Gemini and Imagen models. The library is included as part of the Firebase SDK for Apple platforms (firebase-ios-sdk).

  1. In Xcode, with your app project open, navigate to File > Add Packages.

  2. When prompted, add the Firebase Apple platforms SDK repository:

    https://github.com/firebase/firebase-ios-sdk
    
  3. Select the latest SDK version.

  4. Select the FirebaseVertexAI library.

When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.

Kotlin

The Vertex AI in Firebase SDK for Android (firebase-vertexai) provides access to the APIs for interacting with Gemini and Imagen models.

In your module (app-level) Gradle file (like <project>/<app-module>/build.gradle.kts), add the dependency for the Vertex AI in Firebase library for Android. We recommend using the Firebase Android BoM to control library versioning.

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:33.13.0"))

  // Add the dependency for the Vertex AI in Firebase library
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-vertexai")
}

By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.

Java

The Vertex AI in Firebase SDK for Android (firebase-vertexai) provides access to the APIs for interacting with Gemini and Imagen models.

In your module (app-level) Gradle file (like <project>/<app-module>/build.gradle.kts), add the dependency for the Vertex AI in Firebase library for Android. We recommend using the Firebase Android BoM to control library versioning.

For Java, you need to add two additional libraries.

dependencies {
  // ... other androidx dependencies

  // Import the BoM for the Firebase platform
  implementation(platform("com.google.firebase:firebase-bom:33.13.0"))

  // Add the dependency for the Vertex AI in Firebase library
  // When using the BoM, you don't specify versions in Firebase library dependencies
  implementation("com.google.firebase:firebase-vertexai")

  // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
  implementation("com.google.guava:guava:31.0.1-android")

  // Required for streaming operations (to use `Publisher` from Reactive Streams)
  implementation("org.reactivestreams:reactive-streams:1.0.4")
}

By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.

Web

The Vertex AI in Firebase library provides access to the APIs for interacting with Gemini and Imagen models. The library is included as part of the Firebase JavaScript SDK for Web.

  1. Install the Firebase JS SDK for Web using npm:

    npm install firebase
    
  2. Initialize Firebase in your app:

    import { initializeApp } from "firebase/app";
    
    // TODO(developer) Replace the following with your app's Firebase configuration
    // See: https://firebase.google.com/docs/web/learn-more#config-object
    const firebaseConfig = {
      // ...
    };
    
    // Initialize FirebaseApp
    const firebaseApp = initializeApp(firebaseConfig);
    

Dart

The Vertex AI in Firebase plugin for Flutter (firebase_vertexai) provides access to the APIs for interacting with Gemini and Imagen models.

  1. From your Flutter project directory, run the following command to install the core plugin and the Vertex AI in Firebase plugin:

    flutter pub add firebase_core && flutter pub add firebase_vertexai
    
  2. In your lib/main.dart file, import the Firebase core plugin, the Vertex AI in Firebase plugin, and the configuration file you generated earlier:

    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_vertexai/firebase_vertexai.dart';
    import 'firebase_options.dart';
    
  3. Also in your lib/main.dart file, initialize Firebase using the DefaultFirebaseOptions object exported by the configuration file:

    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
    
  4. Rebuild your Flutter application:

    flutter run
    

Step 3: Initialize the Vertex AI service and create a GenerativeModel instance

Before you can make any API calls and send a prompt to a Gemini model, you need to initialize the Vertex AI service and create a GenerativeModel instance.

Swift

import FirebaseVertexAI

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")

Kotlin

For Kotlin, the methods in this SDK are suspend functions and need to be called from a Coroutine scope.
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

Java

For Java, the streaming methods in this SDK return a Publisher type from the Reactive Streams library.
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Web

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });

Dart

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
      FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');

After you finish this getting started guide, learn how to choose a model and (optionally) a location appropriate for your use case and app.

Step 4: Send a prompt request to a model

Now that you've connected your app to Firebase, added the SDK, and initialized the Vertex AI service and the generative model, you're ready to send a prompt request to a Gemini model.

You can use generateContent() to generate text from a text-only prompt request:

Swift

import FirebaseVertexAI

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")

// Provide a prompt that contains text
let prompt = "Write a story about a magic backpack."

// To generate text output, call generateContent with the text input
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")

Kotlin

For Kotlin, the methods in this SDK are suspend functions and need to be called from a Coroutine scope.
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")

// Provide a prompt that contains text
val prompt = "Write a story about a magic backpack."

// To generate text output, call generateContent with the text input
val response = generativeModel.generateContent(prompt)
print(response.text)

Java

For Java, the methods in this SDK return a ListenableFuture.
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
        .generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
Content prompt = new Content.Builder()
    .addText("Write a story about a magic backpack.")
    .build();

// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });

// Wrap in an async function so you can use await
async function run() {
  // Provide a prompt that contains text
  const prompt = "Write a story about a magic backpack."

  // To generate text output, call generateContent with the text input
  const result = await model.generateContent(prompt);

  const response = result.response;
  const text = response.text();
  console.log(text);
}

run();

Dart

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
      FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');

// Provide a prompt that contains text
final prompt = [Content.text('Write a story about a magic backpack.')];

// To generate text output, call generateContent with the text input
final response = await model.generateContent(prompt);
print(response.text);

What else can you do?

Learn more about the supported models

Learn about the models available for various use cases and their quotas and pricing.

Try out other capabilities

Learn how to control content generation

You can also experiment with prompts and model configurations using Vertex AI Studio.


Give feedback about your experience with Vertex AI in Firebase