Get started with the Gemini API using the Firebase AI Logic SDKs
Stay organized with collectionsSave and categorize content based on your preferences.
This guide shows you how to get started making calls to theGemini APIdirectly from your app using theFirebase AI Logicclient SDKs
for your chosen platform.
You can also use this guide to get started withaccessingImagenmodelsusing theFirebase AI Logic 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
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
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
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
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+
Unity
This guide assumes that you're familiar with developing games with Unity.
Make sure that your development environment and Unity game meet these
requirements:
Unity Editor 2021 LTS or newer
Check out helpful resources
Swift
Try out the quickstart app
Use the quickstart app to try out the SDK quickly and see a complete
implementation of various use cases. Or use the quickstart app if don't have
your own Apple platforms app. To use the quickstart app, you'll need toconnect it to a Firebase project.
This video demonstrates how to get started withFirebase AI Logicby
building a real-world AI-powered meal planning app that generates recipes from
a text prompt.
You can also download and explore the codebase for the app in the video.
Use the quickstart app to try out the SDK quickly and see a complete
implementation of various use cases. Or use the quickstart app if don't have
your own Android app. To use the quickstart app, you'll need toconnect it to a Firebase project.
This video demonstrates how to get started withFirebase AI Logicby
building a real-world AI-powered meal planning app that generates recipes from
a text prompt.
You can also download and explore the codebase for the app in the video.
Use the quickstart app to try out the SDK quickly and see a complete
implementation of various use cases. Or use the quickstart app if don't have
your own Android app. To use the quickstart app, you'll need toconnect it to a Firebase project.
This video demonstrates how to get started withFirebase AI Logicby
building a real-world AI-powered meal planning app that generates recipes from
a text prompt.*
You can also download and explore the codebase for the app in the video.
*This video and its app are in Kotlin, but they can still
help Java developers understand the basics about how to get started withFirebase AI Logic.
Web
Try out the quickstart app
Use the quickstart app to try out the SDK quickly and see a complete
implementation of various use cases. Or use the quickstart app if don't have
your own web app. To use the quickstart app, you'll need toconnect it to a Firebase project.
Use the quickstart app to try out the SDK quickly and see a complete
implementation of various use cases. Or use the quickstart app if don't have
your own Flutter app. To use the quickstart app, you'll need toconnect it to a Firebase project.
This video demonstrates how to get started withFirebase AI Logicby
building a real-world AI-powered meal planning app that generates recipes from
a text prompt.
You can also download and explore the codebase for the app in the video.
Use the quickstart app to try out the SDK quickly and see a complete
implementation of various use cases. Or use the quickstart app if don't have
your own Unity game. To use the quickstart app, you'll need toconnect it to a Firebase project.
Step 1: Set up a Firebase project and connect your app
Sign into theFirebaseconsole,
and then select your Firebase project.
Don't already have a Firebase project?
If you don't already have a Firebase project, click the button to create a
new Firebase project, and then use either of the following options:
Option 1: Create a wholly new Firebase project (and its underlyingGoogle Cloudproject automatically) by entering a new project name in the
first step of the workflow.
Option 2: "Add Firebase" to an existingGoogle Cloudproject by
clickingAdd Firebase to Google Cloud project(at bottom of page).
In the first step of the workflow, start entering theproject nameof
the existing project, and then select the project from the displayed list.
Complete the remaining steps of the on-screen workflow to create a Firebase
project. Note that when prompted, you donotneed to set upGoogle Analyticsto use theFirebase AI Logic SDKs.
ClickGet startedto launch a guided workflow that helps you set up therequired APIsand resources for your project.
Select the "Gemini API" provider that you'd like to use with theFirebase AI Logic SDKs.Gemini Developer APIis
recommended for first-time users. You can always add billing or set upVertex AIGemini APIlater, if you'd like.
Gemini Developer API—billing optional(available on the no-cost Spark pricing plan, and you can upgrade later if
desired) The console will enable the required APIs and create aGeminiAPI key in your project. Donotadd thisGeminiAPI key into your app's codebase.Learn more.
Vertex AIGemini API—billing required(requires the pay-as-you-go Blaze pricing plan) The console will help you set up billing and enable the
required APIs in your project.
If prompted in the console's workflow, follow the on-screen instructions to
register your app and connect it to Firebase.
Continue to the next step in this guide to add the SDK to your app.
Step 2: Add the SDK
With your Firebase project set up and your app connected to Firebase
(see previous step), you can now add theFirebase AI Logic SDK to your app.
Swift
Use Swift Package Manager to install and manage Firebase dependencies.
TheFirebase AI Logiclibrary provides access to the APIs for interacting
withGeminiandImagenmodels. The library is included
as part of the Firebase SDK for Apple platforms (firebase-ios-sdk).
If you're already using Firebase, then make sure your Firebase package is
v11.13.0 or later.
In Xcode, with your app project open, navigate toFile > Add Package Dependencies.
When prompted, add the Firebase Apple platforms SDK repository:
https://github.com/firebase/firebase-ios-sdk
Select the latest SDK version.
Select theFirebaseAIlibrary.
When finished, Xcode will automatically begin resolving and downloading your
dependencies in the background.
Kotlin
TheFirebase AI Logic SDK for Android (firebase-ai) provides
access to the APIs for interacting withGeminiandImagenmodels.
In yourmodule (app-level) Gradle file(like<project>/<app-module>/build.gradle.kts),
add the dependency for theFirebase AI Logiclibrary for Android.
We recommend using theFirebase Android BoMto control library versioning.
dependencies{// ... other androidx dependencies// Import theBoMfor the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.2.0"))// Add the dependency for theFirebase AI Logiclibrary// When using theBoM, you don't specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-ai")}
By using theFirebase Android BoM,
your app will always use compatible versions of Firebase Android libraries.
(Alternative)
Add Firebase library dependencies without using theBoM
If you choose not to use theFirebase BoM, you must specify each
Firebase library version in its dependency line.
Note that if you usemultipleFirebase libraries in your app, we
strongly recommend using theBoMto manage library versions, which
ensures that all versions are compatible.
dependencies{// Add the dependency for theFirebase AI Logiclibrary// When NOT using theBoM, you must specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-ai:17.2.0")}
Java
TheFirebase AI Logic SDK for Android (firebase-ai) provides
access to the APIs for interacting withGeminiandImagenmodels.
In yourmodule (app-level) Gradle file(like<project>/<app-module>/build.gradle.kts),
add the dependency for theFirebase AI Logiclibrary for Android.
We recommend using theFirebase Android BoMto control library versioning.
For Java, you need to add two additional libraries.
dependencies{// ... other androidx dependencies// Import theBoMfor the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.2.0"))// Add the dependency for theFirebase AI Logiclibrary// When using theBoM, you don't specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-ai")// 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 theFirebase Android BoM,
your app will always use compatible versions of Firebase Android libraries.
(Alternative)
Add Firebase library dependencies without using theBoM
If you choose not to use theFirebase BoM, you must specify each
Firebase library version in its dependency line.
Note that if you usemultipleFirebase libraries in your app, we
strongly recommend using theBoMto manage library versions, which
ensures that all versions are compatible.
dependencies{// Add the dependency for theFirebase AI Logiclibrary// When NOT using theBoM, you must specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-ai:17.2.0")}
Web
TheFirebase AI Logiclibrary provides access to the APIs for interacting
withGeminiandImagenmodels. The library is included
as part of the Firebase JavaScript SDK for Web.
Install the Firebase JS SDK for Web using npm:
npm install firebase
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-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);
Dart
TheFirebase AI Logicplugin for Flutter (firebase_ai) provides
access to the APIs for interacting withGeminiandImagenmodels.
From your Flutter project directory, run the following command to
install the core plugin and theFirebase AI Logicplugin:
flutterpubaddfirebase_corefirebase_ai
In yourlib/main.dartfile, import the Firebase core plugin, theFirebase AI Logicplugin, and the configuration file you generated
earlier:
Download theFirebaseUnitySDK, then extract the SDK somewhere
convenient.
TheFirebaseUnitySDK is not platform-specific.
In your open Unity project, navigate toAssets>Import Package>Custom Package.
From the extracted SDK, select theFirebaseAIpackage.
In theImport Unity Packagewindow, clickImport.
Back in theFirebaseconsole, in the setup workflow, clickNext.
Step 3: Initialize the service and create a model instance
Click yourGemini APIprovider to view provider-specific content
and code on this page.
Before sending a prompt to aGeminimodel,
initialize the service for your chosen API provider and create aGenerativeModelinstance.
Swift
importFirebaseAI// Initialize the Gemini Developer API backend serviceletai=FirebaseAI.firebaseAI(backend:.googleAI())// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=ai.generativeModel(modelName:"gemini-2.5-flash")
Kotlin
// Initialize the Gemini Developer API backend service// Create a `GenerativeModel` instance with a model that supports your use casevalmodel=Firebase.ai(backend=GenerativeBackend.googleAI()).generativeModel("gemini-2.5-flash")
Java
// Initialize the Gemini Developer API backend service// Create a `GenerativeModel` instance with a model that supports your use caseGenerativeModelai=FirebaseAI.getInstance(GenerativeBackend.googleAI()).generativeModel("gemini-2.5-flash");// Use the GenerativeModelFutures Java compatibility layer which offers// support for ListenableFuture and Publisher APIsGenerativeModelFuturesmodel=GenerativeModelFutures.from(ai);
Web
import{initializeApp}from"firebase/app";import{getAI,getGenerativeModel,GoogleAIBackend}from"firebase/ai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Gemini Developer API backend serviceconstai=getAI(firebaseApp,{backend:newGoogleAIBackend()});// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(ai,{model:"gemini-2.5-flash"});
Dart
import'package:firebase_ai/firebase_ai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';// Initialize FirebaseAppawaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Gemini Developer API backend service// Create a `GenerativeModel` instance with a model that supports your use casefinalmodel=FirebaseAI.googleAI().generativeModel(model:'gemini-2.5-flash');
Unity
usingFirebase;usingFirebase.AI;// Initialize the Gemini Developer API backend servicevarai=FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());// Create a `GenerativeModel` instance with a model that supports your use casevarmodel=ai.GetGenerativeModel(modelName:"gemini-2.5-flash");
Note thatdepending on the capability you're using, you might not always
create aGenerativeModelinstance.
Also, after you finish this getting started guide, learn how to choose amodelfor your use case and app.
Step 4: Send a prompt request to a model
You're now set up to send a prompt request to aGeminimodel.
You can usegenerateContent()to generate text from a prompt that contains
text:
Swift
importFirebaseAI// Initialize the Gemini Developer API backend serviceletai=FirebaseAI.firebaseAI(backend:.googleAI())// Create a `GenerativeModel` instance with a model that supports your use caseletmodel=ai.generativeModel(modelName:"gemini-2.5-flash")// Provide a prompt that contains textletprompt="Write a story about a magic backpack."// To generate text output, call generateContent with the text inputletresponse=tryawaitmodel.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 aCoroutine scope.
// Initialize the Gemini Developer API backend service// Create a `GenerativeModel` instance with a model that supports your use casevalmodel=Firebase.ai(backend=GenerativeBackend.googleAI()).generativeModel("gemini-2.5-flash")// Provide a prompt that contains textvalprompt="Write a story about a magic backpack."// To generate text output, call generateContent with the text inputvalresponse=generativeModel.generateContent(prompt)print(response.text)
// Initialize the Gemini Developer API backend service// Create a `GenerativeModel` instance with a model that supports your use caseGenerativeModelai=FirebaseAI.getInstance(GenerativeBackend.googleAI()).generativeModel("gemini-2.5-flash");// Use the GenerativeModelFutures Java compatibility layer which offers// support for ListenableFuture and Publisher APIsGenerativeModelFuturesmodel=GenerativeModelFutures.from(ai);// Provide a prompt that contains textContentprompt=newContent.Builder().addText("Write a story about a magic backpack.").build();// To generate text output, call generateContent with the text inputListenableFuture<GenerateContentResponse>response=model.generateContent(prompt);Futures.addCallback(response,newFutureCallback<GenerateContentResponse>(){@OverridepublicvoidonSuccess(GenerateContentResponseresult){StringresultText=result.getText();System.out.println(resultText);}@OverridepublicvoidonFailure(Throwablet){t.printStackTrace();}},executor);
Web
import{initializeApp}from"firebase/app";import{getAI,getGenerativeModel,GoogleAIBackend}from"firebase/ai";// TODO(developer) Replace the following with your app's Firebase configuration// See: https://firebase.google.com/docs/web/learn-more#config-objectconstfirebaseConfig={// ...};// Initialize FirebaseAppconstfirebaseApp=initializeApp(firebaseConfig);// Initialize the Gemini Developer API backend serviceconstai=getAI(firebaseApp,{backend:newGoogleAIBackend()});// Create a `GenerativeModel` instance with a model that supports your use caseconstmodel=getGenerativeModel(ai,{model:"gemini-2.5-flash"});// Wrap in an async function so you can use awaitasyncfunctionrun(){// Provide a prompt that contains textconstprompt="Write a story about a magic backpack."// To generate text output, call generateContent with the text inputconstresult=awaitmodel.generateContent(prompt);constresponse=result.response;consttext=response.text();console.log(text);}run();
Dart
import'package:firebase_ai/firebase_ai.dart';import'package:firebase_core/firebase_core.dart';import'firebase_options.dart';// Initialize FirebaseAppawaitFirebase.initializeApp(options:DefaultFirebaseOptions.currentPlatform,);// Initialize the Gemini Developer API backend service// Create a `GenerativeModel` instance with a model that supports your use casefinalmodel=FirebaseAI.googleAI().generativeModel(model:'gemini-2.5-flash');// Provide a prompt that contains textfinalprompt=[Content.text('Write a story about a magic backpack.')];// To generate text output, call generateContent with the text inputfinalresponse=awaitmodel.generateContent(prompt);print(response.text);
Unity
usingFirebase;usingFirebase.AI;// Initialize the Gemini Developer API backend servicevarai=FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());// Create a `GenerativeModel` instance with a model that supports your use casevarmodel=ai.GetGenerativeModel(modelName:"gemini-2.5-flash");// Provide a prompt that contains textvarprompt="Write a story about a magic backpack.";// To generate text output, call GenerateContentAsync with the text inputvarresponse=awaitmodel.GenerateContentAsync(prompt);UnityEngine.Debug.Log(response.Text??"No text in response.");
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,[]]