> Documentation index: https://firebase.google.com/docs/ai-logic/llms.txt

> [!WARNING]
> **Preview** : Using Firebase AI Logic to access the Gemini API through Apple's Foundation Models framework is currently in Preview, which means that it isn't subject to any SLA or deprecation policy and could change in backwards-incompatible ways.

<br />

[Video](https://www.youtube.com/watch?v=HUgkMFwq3ZY)

At WWDC 2026, Apple opened the Foundation Models framework to third-party
model adapters, which means you can access cloud-hosted models (like
Gemini) through the Foundation Models framework using the same API
as you would use to access on-device models.

In your app, you can swap the model instance to route your requests to either
on-device or cloud inference to fit your use case:

- On-device models offer maximum privacy, zero cost, and offline support.
- Cloud-hosted Gemini models offer large context windows, advanced capabilities, and more reasoning power.

You can **access the cloud-hosted Gemini models through
Apple's Foundation Models framework by using the Firebase SDK for Apple platforms --
specifically the Firebase AI Logic library**. This guide shows you how to
get started.

To protect access to Gemini models, this guide also shows you how to
set up [Firebase App Check](https://firebase.google.com/docs/ai-logic/app-check.md.txt), which is
***critical*** even during development.

## Prerequisites

- Download Xcode 27 from the
  [Mac App Store](https://apps.apple.com/app/xcode/id497799835).

- An Apple platform simulator or a physical device, both running the
  corresponding OS version (for example, iOS 27).

- A new Xcode project of an Apple platforms app using a SwiftUI interface.

> [!NOTE]
> **Note:** For this initial release, the following Apple platforms are supported: iOS and macOS.

### Supported Gemini models

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

- General-use models

  - `gemini-4-hydrogen`
  - `gemini-4-argon`
  - `gemini-4-carbon`
  - `gemini-3.8-flash`
  - `gemini-3.5-flash-lite` (and the older `gemini-3.1-flash-lite`)
- Image-generating (Nano Banana) models

  - `gemini-nano-banana-2.1` (Nano Banana 2.1)
  - `gemini-3.1-flash-image` (Nano Banana 2)
  - `gemini-3.1-flash-lite-image` (Nano Banana 2 Lite)
  - `gemini-3-pro-image` (Nano Banana Pro)

Gemini Live API models are ***not*** supported.
Older general-use Gemini 3.x Flash models support this capability, but
they're no longer recommended for use due to upcoming shutdown dates and/or
automatic routing to newer models.

## Step 1: Create a Firebase project

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

1. Sign into the
   [Firebase console](https://console.firebase.google.com/).

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](https://console.firebase.google.com/project/_/overview?useAutoProject=true)
   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

   3. Choose at least version **12.19.0**.

   4. Click **Add Package**. Xcode will resolve and download the dependencies.

   5. 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:

   ```swift
   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**](https://console.firebase.google.com/project/_/ailogic/?useAutoProject=true).

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)](https://console.firebase.google.com/project/_/ailogic?openVertexAiOnboarding=true&useAutoProject=true)
   (and its requirement for billing).
4. Continue in the workflow to set up the required APIs and associated services
   for Firebase AI Logic.

   This stage of the workflow automatically
   [enforces Firebase App Check for AI Logic](https://firebase.google.com/docs/ai-logic/app-check.md.txt),
   which is a *critical* service to help protect the Gemini API when it's
   directly accessed from your app. As part of getting started (see steps later
   in this guide), you'll need to configure the App Check *debug provider*
   for local development when App Check is enforced.

   > [!NOTE]
   > **Note:** To release your app's AI feature to end users, you need to [register your app with a production App Check attestation provider](https://firebase.google.com/docs/ai-logic/app-check.md.txt) so that they can use the feature when App Check is enforced.

### 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`.

   ```swift
   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()
         }
       }
     }
   }
   ```
2. 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'.

   3. Copy the token (for example, `123a4567-b89c-12d3-e456-789012345678`).

3. Register your debug token with App Check:

   1. In the Firebase console, go to the
      **Security** \> **App Check** \>
      [**Apps** tab](https://console.firebase.google.com/project/_/appcheck/apps/?useAutoProject=true).

   2. Find your app, click the overflow menu
      (), 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](https://firebase.google.com/docs/app-check/ios/debug-provider.md.txt).

> [!CAUTION]
> **Here are some critical points about the
> App Check debug provider:**
>
> - **Keep your debug token and debug build private.** Don't commit your debug token to a public repository, and don't ship your debug token or debug build in production builds of your app.
> - **Register your app with a production attestation provider before
>   releasing to end users.** You'll need to [register your app with a production App Check attestation provider](https://firebase.google.com/docs/ai-logic/app-check.md.txt) (for example, App Attest, Play Integrity, or reCAPTCHA Enterprise). so that your end-users can use your feature with App Check enforced.

## Step 5: Initialize the AI Logic service in your app

|---|
| *Click your Gemini API provider to view provider-specific content and code on this page.* <button value="dev" default="">Gemini Developer API</button> <button value="vertex">Agent Platform Gemini API</button> |

With Firebase and App Check configured, you can now initialize the
Firebase AI Logic service in your app.

```swift
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-4-hydrogen")

```

## Step 6: Send a request to a Gemini model

With Firebase AI Logic setup, protected, and initialized in your app,
you're ready to send a request to a Gemini model.

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

```swift
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-4-hydrogen")


// 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](https://firebase.google.com/docs/ai-logic/apple-foundation-models-framework/capabilities.md.txt#text-from-multimodal-input)
,
[generating structured JSON output](https://firebase.google.com/docs/ai-logic/apple-foundation-models-framework/capabilities.md.txt#structured-json-output)
, and
[generating images](https://firebase.google.com/docs/ai-logic/apple-foundation-models-framework/capabilities.md.txt#generate-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:)`.

```swift
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-4-hydrogen")


// 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

- Explore the [available capabilities](https://firebase.google.com/docs/ai-logic/apple-foundation-models-framework/capabilities.md.txt) when accessing the Gemini API through Apple's Foundation Models framework.
- Learn how to [configure the model](https://firebase.google.com/docs/ai-logic/apple-foundation-models-framework/configuration.md.txt) to control its responses, including setting a thinking level ("reasoning").
- Learn how to [provide tools to the model](https://firebase.google.com/docs/ai-logic/apple-foundation-models-framework/tools.md.txt), including grounding for up-to-date information.
- Read more about [Firebase App Check](https://firebase.google.com/docs/ai-logic/app-check.md.txt) and how it protects your resources.

<br />

[**Give feedback**
about accessing the Gemini API through Apple's Foundation Models framework](https://github.com/firebase/firebase-ios-sdk/issues)

<br />