Get started: Access the Gemini API through Apple's Foundation Models framework  |  Firebase AI Logic

Get started: Access the Gemini API through Apple's Foundation Models framework

Prerequisites

Supported Gemini models

The integration with Apple's Foundation Models framework supports the following Gemini models.

Gemini Live API models are not supported. Note that Gemini 2.5 models are technically supported, but they're not recommended for new projects and require special configuration not covered in these guides.

Step 1: Create a Firebase project

We recommend starting with a new Firebase project to explore this integration.

  1. Sign into the Firebase console.
  2. Click Create a new Firebase project.
  3. Follow the on-screen instructions. You do not need to enable Google Analytics.

Step 2: Connect your app to Firebase

To connect your app to Firebase, you must register it with your Firebase project and add a configuration file to your codebase.

  1. In the center of the project overview page, click the iOS+ icon to launch the setup workflow.
  2. Register your app:
    1. Enter your app's bundle ID. Make sure it matches the bundle ID of the project you're building in Xcode.
    2. Click Register app.
  3. Add the Firebase configuration file. This file contains the settings for the Firebase SDK to connect to your Firebase project.
    1. Click Download GoogleService-Info.plist to get your configuration file.
    2. Move GoogleService-Info.plist into the root of your Xcode project and add it to all targets.
    3. Click Next in the Firebase console.
    4. The workflow in the console provides generic instructions for adding the Firebase SDK to your app, so skip ahead to the next step in this guide for specific instructions for Firebase AI Logic.

Step 3: Add Firebase libraries and initialize Firebase in your app

  1. Use Swift Package Manager to add the required Firebase libraries:
    1. In Xcode, with your app project open, select File > Add Packages.
    2. Enter the Firebase Apple SDK repository URL:
    https://github.com/firebase/firebase-ios-sdk
    
    1. Choose at least version 12.19.0.
    2. Click Add Package. Xcode will resolve and download the dependencies.
    3. When prompted, add the FirebaseAILogic and FirebaseAppCheck libraries to your app target.
  2. Initialize Firebase when your app starts up by adding the following code to your app's main entry point:
import SwiftUI
import FirebaseCore

@main
struct YourApp: App {
     init() {
       FirebaseApp.configure()
     }

var body: some Scene {
       WindowGroup {
         NavigationView {
           ContentView()
         }
       }
     }
}

Step 4: Enable and secure Firebase services

Now that your app is configured to use Firebase, you need to enable the Firebase AI Logic service and protect access to its associated APIs using Firebase App Check.

Step 4a: Set up Firebase AI Logic in your Firebase project

  1. In the Firebase console, go to AI Services > AI Logic.
  2. Click Get started to launch the guided setup workflow.
  3. When asked to choose a "Gemini API provider", we recommend selecting the Gemini Developer API, which lets you get started quickly at no cost. At any point later, you can always set up the Agent Platform Gemini API (formerly Vertex AI) (and its requirement for billing).
  4. Continue in the workflow to set up the required APIs and associated services for Firebase AI Logic.

Step 4b: Configure the App Check debug provider for local development

For local development, you need to configure the App Check debug provider to bypass attestation while still maintaining the enforcement of App Check.

  1. In your Xcode project, import FirebaseAppCheck and initialize App Check with the debug provider factory before you configure Firebase.
import SwiftUI
import FirebaseCore
import FirebaseAppCheck

@main
struct YourApp: App {
     init() {
       let providerFactory = AppCheckDebugProviderFactory()
       AppCheck.setAppCheckProviderFactory(providerFactory)
       FirebaseApp.configure()
     }

var body: some Scene {
       WindowGroup {
         NavigationView {
           ContentView()
         }
       }
     }
}
  1. Obtain your debug token:
    1. Launch your app in the simulator or on your test device.
    2. Open the Xcode console and look for the App Check debug token. For example:
    <Warning> [AppCheckCore][I-GAC004001] App Check debug token: '123a4567-b89c-12d3-e456-789012345678'.
    
    1. Copy the token (for example, 123a4567-b89c-12d3-e456-789012345678).
  2. Register your debug token with App Check:
    1. In the Firebase console, go to the Security > App Check > Apps tab.
    2. Find your app, click the overflow menu (more_vert), and then select Manage debug tokens.
    3. Follow the on-screen instructions to register your debug token.

For details about the debug provider (including how to get a new debug token), check out the official App Check docs.

Step 5: Initialize the AI Logic service in your app

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.8-flash")

Step 6: Send a request to a Gemini model

The following example shows the most basic type of request – generating text from a text-only prompt:

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Initialize the Gemini Developer API backend service.
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Initialize a `geminiLanguageModel` with a Gemini model that supports your use case.
let model = ai.geminiLanguageModel(name: "gemini-3.8-flash")

// Create a session by injecting the model into Apple's `LanguageModelSession`.
let session = LanguageModelSession(model: model)

// Generate a text response to a prompt.
let response = try await session.respond(to: "Write a story about a magic backpack.")
print(response.content)

Gemini models also support other types of requests, like analyzing images and PDFs, generating structured JSON output, and generating images (using "Nano Banana" models). See examples for these types of requests in the docs or in the sample app.

Stream the response

You can achieve faster interactions by not waiting for the entire result from the model generation, and instead use streaming to handle partial results. To stream the response, use streamResponse(to:) instead of respond(to:).

import FoundationModels
import FirebaseCore
import FirebaseAILogic

// Create a session by injecting the model into Apple's `LanguageModelSession`.
let session = LanguageModelSession(model: model)

// Generate a streamed text response to a prompt.
// To stream the response, use `streamResponse(to:)` instead of `respond(to:)`.
let stream = session.streamResponse(to: "Write a story about a magic backpack.")
var response = ""
for try await snapshot in stream {
  // The snapshot contains *all* content generated so far.
  response = snapshot.content
}

Next steps