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.

Prerequisites

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 the following requirements:

    • Xcode 15.2 or higher
    • Your app must target iOS 15 or higher, or macOS 12 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 Apple platforms 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. Continue in the console's generative AI workflow to connect your app to Firebase, which includes these tasks:

    • Registering your app with your Firebase project.

    • Adding your Firebase configuration file (GoogleService-Info.plist) to your app.

  2. 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.

Use Swift Package Manager to install and manage Firebase dependencies.

The Vertex AI in Firebase library provides access to the Vertex AI Gemini API and 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.

Step 3: Initialize the Vertex AI service and the generative model

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

  1. Import the FirebaseVertexAI module:

    import FirebaseVertexAI
    
  2. Initialize the Vertex AI service and the generative model:

    import FirebaseVertexAI
    
    // Initialize the Vertex AI service
    let vertex = VertexAI.vertexAI()
    
    // Initialize the generative model with a model that supports your use case
    let model = vertex.generativeModel(modelName: "gemini-2.0-flash")
    

When you've finished the getting started guide, learn how to choose a Gemini model and (optionally) a location appropriate for your use case and app.

Step 4: Call the Vertex AI Gemini API

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 call the Vertex AI Gemini API.

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

import FirebaseVertexAI

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

// Initialize the generative model 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.")

What else can you do?

Learn more about the Gemini 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