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

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

  • Make sure that your development environment and Flutter app meet the following 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 Build with Gemini 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

  1. Sign into the Firebase console.

  2. Click Create project, and then use either of the following options:

    • Option 1: Create a wholly new Firebase project (and its underlying Google Cloud project automatically) by entering a new project name in the first step of the "Create project" workflow.

    • Option 2: "Add Firebase" to an existing Google Cloud project by selecting your Google Cloud project name from the drop-down menu in the first step of the "Create project" workflow.

    Note that when prompted, you do not need to set up Google Analytics to use the Vertex AI in Firebase SDKs.

  3. In the Firebase console, go to the Build with Gemini page.

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

  1. Install the required command line tools:

    1. If you haven't already, install the Firebase CLI.

    2. Log into Firebase using your Google Account by running the following command:

      firebase login
      
    3. Install the FlutterFire CLI by running the following command from any directory:

      dart pub global activate flutterfire_cli
      
  2. Configure your apps to use Firebase:

    Use the FlutterFire CLI to configure your Flutter apps to connect to Firebase.

    From your Flutter project directory, run the following command to start the app configuration workflow:

    flutterfire configure
    

    The flutterfire configure workflow does the following:

    • Asks you to select the platforms (iOS, Android, Web) supported in your Flutter app. For each selected platform, the FlutterFire CLI creates a new Firebase app in your Firebase project.

      You can select either to use an existing Firebase project or to create a new Firebase project. If you already have apps registered in an existing Firebase project, the FlutterFire CLI will attempt to match them based on your current Flutter project configuration.

    • Creates a Firebase configuration file (firebase_options.dart) and adds it to the lib/ directory of your Flutter app.

  3. In the next steps of this guide, you'll add the Vertex AI in Firebase SDK to your app and complete the required initialization specific to using the SDK and the Gemini API.


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.

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 the generative model

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

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 the generative model
// 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:

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 the generative model
// 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 of the Gemini API

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