
Firebase Ai Logic Basics
- 78.4k installs
- 390 repo stars
- Updated July 27, 2026
- firebase/agent-skills
firebase-ai-logic-basics is an official Firebase skill for integrating Firebase AI Logic (the Gemini API) into web and mobile apps using client-side SDKs.
About
firebase-ai-logic-basics is an official Firebase skill for integrating Firebase AI Logic (the Gemini API) into web and mobile apps using client-side SDKs, without a dedicated backend. It covers setup, multimodal inference, structured output, chat sessions, streaming, and security. A developer uses it to add Gemini-powered features while correctly provisioning the service with the Firebase CLI. It stresses that init ailogic is mandatory and that App Check must be set up to prevent quota abuse.
- Integrates Firebase AI Logic (Gemini) via client-side SDKs
- Covers multimodal, structured output, and streaming
- Mandatory `firebase-tools init ailogic` provisioning
- Requires App Check for safe production use
- Supports Android, iOS, Flutter, Web, and Unity
Firebase Ai Logic Basics by the numbers
- 78,411 all-time installs (skills.sh)
- +5,677 installs in the week ending Jul 28, 2026 (Skillselion tracking)
- Ranked #17 of 16,659 AI & Agent Building skills by installs in the Skillselion catalog
- Security screen: LOW risk (skills.sh audit)
- Data as of Jul 28, 2026 (Skillselion catalog sync)
firebase-ai-logic-basics capabilities & compatibility
Gemini Developer API has a free tier for prototyping; Vertex AI and image generation require a Blaze pay-as-you-go plan.
- Capabilities
- gemini integration · multimodal inference · structured output · ai backend setup
- Works with
- gcp
- Use cases
- api development · image generation
- Runs
- Local or remote
- Pricing
- Freemium
What firebase-ai-logic-basics says it does
Official skill for integrating Firebase AI Logic (Gemini API) into web applications. Covers setup, multimodal inference, structured output, and security.
Firebase AI Logic is a product of Firebase that allows developers to add gen AI to their mobile and web apps using client-side SDKs.
npx skills add https://github.com/firebase/agent-skills --skill firebase-ai-logic-basicsAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 78.4k |
|---|---|
| repo stars | ★ 390 |
| Security audit | 3 / 3 scanners passed |
| Last updated | July 27, 2026 |
| Repository | firebase/agent-skills ↗ |
How do I correctly add Gemini-powered features to my app using Firebase AI Logic without hitting PERMISSION_DENIED or quota abuse?
Integrate Firebase AI Logic (Gemini API) into web and mobile apps via client-side SDKs with correct CLI provisioning and App Check security.
Who is it for?
Developers adding Gemini-powered features to web or mobile apps who want the correct Firebase AI Logic setup, provisioning, and security steps.
Skip if: Projects on an unsupported platform, which the skill directs to the Firebase docs, or teams wanting a server-managed AI backend rather than client-side SDKs.
When should I use this skill?
When integrating Firebase AI Logic or the Gemini API into a web or mobile app, including setup, multimodal inference, structured output, and security.
What you get
A provisioned Firebase AI Logic service and client SDK integration that can run text, multimodal, chat, streaming, and structured-output inference safely behind App Check.
- provisioned Firebase AI Logic service
- client SDK AI integration
By the numbers
- supports 2 Gemini API providers
- requires Node.js 16+
- files over 20 MB must go to Cloud Storage
Files
Firebase AI Logic Basics
Overview
Firebase AI Logic is a product of Firebase that allows developers to add gen AI to their mobile and web apps using client-side SDKs. You can call Gemini models directly from your app without managing a dedicated backend. Firebase AI Logic, which was previously known as "Vertex AI for Firebase", represents the evolution of Google's AI integration platform for mobile and web developers.
It supports the two Gemini API providers:
- Gemini Developer API: It has a free tier ideal for prototyping, and
pay-as-you-go for production
- Vertex AI Gemini API: Ideal for scale with enterprise-grade production
readiness, requires Blaze plan
Use the Gemini Developer API as a default, and only Vertex AI Gemini API if the application requires it.
Setup & Initialization
Prerequisites
- Before starting, ensure you have Node.js 16+ and npm installed. Install
them if they aren’t already available.
- Identify the platform the user is interested in building on prior to starting:
Android, iOS, Flutter or Web.
- If their platform is unsupported, Direct the user to Firebase Docs to learn
how to set up AI Logic for their application (share this link with the user https://firebase.google.com/docs/ai-logic/get-started)
Installation
The library is part of the standard Firebase Web SDK.
npm install -g firebase@latest
If you're in a firebase directory (with a firebase.json) the currently selected project will be marked with "current" using this command:
npx -y firebase-tools@latest projects:list
Ensure there's at least one app associated with the current project
npx -y firebase-tools@latest apps:list
Initialize AI logic SDK with the init command
npx -y firebase-tools@latest init ailogic
This will automatically enable the Gemini Developer API in the Firebase console.
More info in Firebase AI Logic Getting Started
Core Capabilities
[!WARNING] CRITICAL: Use current model names: Always check the
Firebase AI Logic Models documentation
for the currently supported model names. Do NOT use gemini-2.0-pro orgemini-2.0-flash or other older models that are shutdown.Text-Only Generation
Multimodal (Text + Images/Audio/Video/PDF input)
Firebase AI Logic allows Gemini models to analyze image files directly from your app. This enables features like creating captions, answering questions about images, detecting objects, and categorizing images. Beyond images, Gemini can analyze other media types like audio, video, and PDFs by passing them as inline data with their MIME type. For files larger than 20 megabytes (which can cause HTTP 413 errors as inline data), store them in Cloud Storage for Firebase and pass their URLs to the Gemini Developer API.
Chat Session (Multi-turn)
Maintain history automatically using startChat.
Streaming Responses
To improve the user experience by showing partial results as they arrive (like a typing effect), use generateContentStream instead of generateContent for faster display of results.
Generate Images with Nano Banana
[!WARNING] Use current Image model names: Always check the
Firebase AI Logic Models documentation
for the currently supported image generation (Nano Banana) model names.
- Requires an upgraded Blaze pay-as-you-go billing plan.
Search Grounding with the built in googleSearch tool
Supported Platforms and Frameworks
Supported Platforms and Frameworks include Kotlin and Java for Android, Swift for iOS, JavaScript for web apps, Dart for Flutter, and C Sharp for Unity.
Advanced Features
Structured Output (JSON)
Enforce a specific JSON schema for the response.
On-Device AI (Hybrid)
Hybrid on-device inference for web apps, where the Firebase Javascript SDK automatically checks for Gemini Nano's availability (after installation) and switches between on-device or cloud-hosted prompt execution. This requires specific steps to enable model usage in the Chrome browser, more info in the hybrid-on-device-inference documentation.
Security & Production
App Check
[!WARNING] Critical Safety Requirement: In order to use AI Logic safely,
you MUST set up App Check on your app. This prevents unauthorized clients from
using your API quota and accessing your backend resources.
See App Check with reCAPTCHA Enterprise for setup instructions.
Remote Config
Consider that you do not need to hardcode model names (e.g., a specific model version string). Use Firebase Remote Config to update model versions dynamically without deploying new client code. See Changing model names remotely
[!WARNING] CRITICAL: Backend Provisioning Required For all platforms
(Flutter, Android, iOS, Web), you MUST run npx firebase-tools init ailogicto provision the service. flutterfire configure ONLY handles clientconfiguration and does NOT enable the AI service, leading to
PERMISSION_DENIED errors.Initialization Code References
| Language, Framework, Platform | Gemini API provider | Context URL |
|---|---|---|
| Web Modular API | Gemini Developer API (Developer API) | firebase://docs/ai-logic/get-started |
| iOS (Swift) | Gemini Developer API | ios_setup.md |
| Flutter (Dart) | Gemini Developer API | flutter_setup.md |
[!WARNING] CRITICAL: Use current model names: Always check the
Firebase AI Logic Models documentation
for the currently supported model names. Do NOT use gemini-2.0-pro orgemini-2.0-flash or other older models that are shutdown.References
Web SDK code examples and usage patterns iOS SDK code examples and usage patterns Flutter SDK code examples and usage patterns
Android (Kotlin) SDK usage patterns
Flutter Setup for Firebase AI Logic
This guide covers how to integrate Firebase AI Logic (Gemini API) into your Flutter applications.
[!IMPORTANT] Foundational Workflows & CLI-First Approach:
>
1. Review Foundation: Before implementing platform-specific code, ALWAYS
review the foundational firebase-basics skill to ensure familiarity withcore workflows.
1. Backend Provisioning via CLI: Use the Firebase CLI for backend setup.
Running npx firebase-tools init ailogic is MANDATORY to provision the service. flutterfire configure does NOT enable the AI service and will result in PERMISSION_DENIED if skipped.1. Client Configuration: Use flutterfire configure strictly for generating firebase_options.dart. Avoid manual Console configuration.[!NOTE]firebase_vertexaihas been replaced byfirebase_ai. Always use
firebase_ai for new projects.Installation
Add the necessary Firebase dependencies to your pubspec.yaml:
dependencies:
flutter:
sdk: flutter
firebase_core: ^4.0.0
firebase_auth: ^6.0.0
firebase_ai: ^3.0.0Run flutter pub get to install the packages.
Initialization
Initialize Firebase and sign in (anonymously or via authenticated user) before using AI Logic.
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_ai/firebase_ai.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAuth.instance.signInAnonymously();
runApp(const MyApp());
}Usage
Use FirebaseAI.googleAI for the Gemini Developer API.
[!IMPORTANT] Model Selection: Refer to
Firebase AI Logic Models
to find the latest supported model. Do not use old models like
gemini-2.0-proorgemini-2.0-flash.
[!IMPORTANT] Choose the Right API Provider: Always use
FirebaseAI.googleAI (Gemini Developer API) as the default for prototypingand standard use. Avoid using the Vertex AI Gemini API unless your application
specifically requires enterprise-grade scalability and is configured for the
Blaze plan.
Text Generation
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_auth/firebase_auth.dart';
Future<String> generateText(String prompt) async {
final googleAI = FirebaseAI.googleAI(auth: FirebaseAuth.instance);
// [AGENT] Replace '<latest_supported_model>' with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
final model = googleAI.generativeModel(model: '<latest_supported_model>');
final response = await model.generateContent([Content.text(prompt)]);
return response.text ?? 'No response';
}Chat Session
final chat = model.startChat(history: [
Content.text('Hello, I am a user.'),
Content.model([TextPart('Hello! How can I help you today?')]),
]);
final response = await chat.sendMessage(Content.text('What is CBT?'));Firebase AI Logic iOS Setup Guide
1. Import and Initialize
Ensure you have installed the FirebaseAILogic SDK via Swift Package Manager.
import FirebaseAILogic
// Initialize the Firebase AI service and the generative model.
let ai = FirebaseAI.firebaseAI()
// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
let model = ai.generativeModel(modelName: "<latest_supported_model>")2. SwiftUI Integration (Best Practices)
Use the @Observable pattern to manage AI state and provide a smooth UX with loading indicators and error handling.
⛔️ CRITICAL WARNING: Do NOT initialize the model inline as a class
property if there's any chance the view model is instantiated before
FirebaseApp.configure() executes in the app root. To be safe, initialize themodel lazily or pass it in from a point in the hierarchy where Firebase is
guaranteed to be configured.
import SwiftUI
import FirebaseAILogic
@MainActor
@Observable
final class AIViewModel {
// [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
private lazy var model = FirebaseAI.firebaseAI().generativeModel(modelName: "<latest_supported_model>")
var responseText: String = ""
var isFetching: Bool = false
var errorMessage: String?
func generate(prompt: String) async {
isFetching = true
errorMessage = nil
defer { isFetching = false }
do {
let response = try await model.generateContent(prompt)
self.responseText = response.text ?? "No response"
} catch {
self.errorMessage = error.localizedDescription
}
}
}
struct AIView: View {
@State private var viewModel = AIViewModel()
@State private var prompt = "Write a story about a magic backpack."
var body: some View {
VStack {
TextField("Enter prompt", text: $prompt)
Button("Generate") {
Task { await viewModel.generate(prompt: prompt) }
}
.disabled(viewModel.isFetching)
if viewModel.isFetching {
ProgressView()
} else if let error = viewModel.errorMessage {
Text(error).foregroundStyle(.red)
} else {
ScrollView {
Text(viewModel.responseText)
}
}
}
.padding()
}
}3. Safety Settings
You can configure safety thresholds to prevent the model from generating harmful content.
let safetySettings = [
SafetySetting(category: .harassment, threshold: .blockLowAndAbove),
SafetySetting(category: .hateSpeech, threshold: .blockMediumAndAbove)
]
let model = FirebaseAI.firebaseAI().generativeModel(
modelName: "<latest_supported_model>", // [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
safetySettings: safetySettings
)Advanced Features
Chat Session (Multi-turn)
Chat sessions persist state across multiple interactions, which is essential for ongoing conversations or when using tools like function calling.
let chat = model.startChat()
Task {
do {
let response1 = try await chat.sendMessage("Hello! I have two dogs in my house.")
print(response1.text ?? "")
let response2 = try await chat.sendMessage("How many paws are in my house?")
print(response2.text ?? "")
} catch {
print("Error in chat: \(error)")
}
}Function Calling (Tools)
Define functions that the model can request to execute to interact with external systems. Note: Advanced workflows like function calling generally require a multi-turn Chat Session to handle the back-and-forth execution.
let getStockPriceTool = Tool(functionDeclarations: [
FunctionDeclaration(
name: "getStockPrice",
description: "Get the current stock price for a given symbol.",
parameters: [
"symbol": Schema(
type: .string,
description: "The stock symbol, e.g. AAPL"
)
]
)
])
let model = FirebaseAI.firebaseAI().generativeModel(
modelName: "<latest_supported_model>", // [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
tools: [getStockPriceTool]
)
// In your task (using a chat session):
let chat = model.startChat()
let response = try await chat.sendMessage("What is the stock price of Apple?")
if let functionCall = response.functionCalls.first {
// Handle the function call (e.g. call a local API and send the result back)
print("Model requested function: \(functionCall.name) with args: \(functionCall.args)")
}Firebase AI Logic on Android (Kotlin)
First, ensure you have initialized the Firebase App (see firebase-basics skill). Then, initialize the AI Logic service as below
0. Enable Firebase AI Logic via CLI
Before adding dependencies in your app, make sure you enable the AI Logic service in your Firebase Project using the Firebase CLI:
npx -y firebase-tools@latest init
# When prompted, select 'AI logic' to enable the Gemini API in your project.______________________________________________________________________
1. Add Dependencies
In your module-level build.gradle.kts (usually app/build.gradle.kts), add the dependency for Firebase AI:
dependencies {
// [AGENT] Fetch the latest available BoM version from https://firebase.google.com/support/release-notes/android before adding this
implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>"))
// Add the dependency for the Firebase AI library
implementation("com.google.firebase:firebase-ai")
}______________________________________________________________________
2. Initialize and Generate Content
In your Activity or Fragment, initialize the FirebaseAI service and generate content using a Gemini model:
import com.google.firebase.ai.FirebaseAI
import com.google.firebase.ai.ktx.ai
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize Firebase AI
val ai = Firebase.ai
// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
val model = ai.generativeModel("<latest_supported_model>")
// Generate content
lifecycleScope.launch {
try {
val response = model.generateContent("Write a story about a magic backpack.")
Log.d(TAG, "Response: ${response.text}")
} catch (e: Exception) {
Log.e(TAG, "Error generating content", e)
}
}
}
}Jetpack Compose (Modern)
Initialize inside a ComponentActivity and use setContent:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.lifecycle.lifecycleScope
import com.google.firebase.Firebase
import com.google.firebase.ai.ai
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val ai = Firebase.ai
// [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
val model = ai.generativeModel("<latest_supported_model>")
lifecycleScope.launch {
val response = model.generateContent("Hello Gemini!")
setContent {
MaterialTheme {
Text("AI Response: ${response.text}")
}
}
}
}
}______________________________________________________________________
3. Multimodal Input (Text and Images)
Pass bitmap data along with text prompts:
val image1: Bitmap = ... // Load your bitmap
val image2: Bitmap = ...
val response = model.generateContent(
content("Analyze these images for me") {
image(image1)
image(image2)
text("Compare these two items.")
}
)
Log.d(TAG, response.text)______________________________________________________________________
4. Chat Session (Multi-turn)
Maintain chat history automatically:
val chat = model.startChat(
history = listOf(
content("user") { text("Hello, I am a software engineer.") },
content("model") { text("Hello! How can I help you today?") }
)
)
lifecycleScope.launch {
val response = chat.sendMessage("What should I learn next?")
Log.d(TAG, response.text)
}______________________________________________________________________
5. Streaming Responses
For faster display, stream the response:
lifecycleScope.launch {
model.generateContentStream("Tell me a long story.")
.collect { chunk ->
print(chunk.text) // Update UI incrementally
}
}Firebase AI Logic Basics
Initialization Pattern
You must initialize the ai-logic service after the main Firebase App.
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// If running in Firebase App Hosting, you can skip Firebase Config and instead use:
// const app = initializeApp();
const firebaseConfig = {
// ... your firebase config
};
const app = initializeApp(firebaseConfig);
// Initialize the AI Logic service (defaults to Gemini Developer API)
// To set the AI provider, set the backend as the second parameter
const ai = getAI(app, { backend: new GoogleAIBackend() });
const generationConfig = {
candidate_count: 1,
maxOutputTokens: 2048,
stopSequences: [],
temperature: 0.7, // Balanced: creative but focused
topP: 0.95, // Standard: allows a wide range of probable tokens
topK: 40, // Standard: considers the top 40 tokens
};
// Specify the config as part of creating the `GenerativeModel` instance
// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
const model = getGenerativeModel(ai, { model: "<latest_supported_model>", generationConfig });Core Capabilities
Text-Only Generation
async function generateText(prompt) {
const result = await model.generateContent(prompt);
const response = await result.response;
return response.text();
}Multimodal (Text + Images/Audio/Video/PDF input)
Firebase AI Logic accepts Base64 encoded data or specific file references.
// Helper to convert file to base64 generic object
async function fileToGenerativePart(file) {
const base64EncodedDataPromise = new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(file);
});
return {
inlineData: {
data: await base64EncodedDataPromise,
mimeType: file.type,
},
};
}
async function analyzeImage(prompt, imageFile) {
const imagePart = await fileToGenerativePart(imageFile);
const result = await model.generateContent([prompt, imagePart]);
return result.response.text();
}Chat Session (Multi-turn)
Maintain history automatically using startChat.
const chat = model.startChat({
history: [
{
role: "user",
parts: [{ text: "Hello, I am a developer." }],
},
{
role: "model",
parts: [{ text: "Great to meet you. How can I help with code?" }],
},
],
});
async function sendMessage(msg) {
const result = await chat.sendMessage(msg);
return result.response.text();
}Streaming Responses
For real-time UI updates (like a typing effect).
async function streamResponse(prompt) {
const result = await model.generateContentStream(prompt);
for await (const chunk of result.stream) {
const chunkText = chunk.text();
console.log("Stream chunk:", chunkText);
// Update UI here
}
}Generate Images with Nano Banana
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend, ResponseModality } from "firebase/ai";
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, {
model: "<latest_supported_image_model>", // [AGENT] Replace with the latest image model from https://firebase.google.com/docs/ai-logic/models.md.txt
// Configure the model to respond with text and images (required)
generationConfig: {
responseModalities: [ResponseModality.TEXT, ResponseModality.IMAGE],
},
});
// Provide a text prompt instructing the model to generate an image
const prompt = 'Generate an image of the Eiffel Tower with fireworks in the background.';
// To generate an image, call `generateContent` with the text input
const result = model.generateContent(prompt);
// Handle the generated image
try {
const inlineDataParts = result.response.inlineDataParts();
if (inlineDataParts?.[0]) {
const image = inlineDataParts[0].inlineData;
console.log(image.mimeType, image.data);
}
} catch (err) {
console.error('Prompt or candidate was blocked:', err);
}Advanced Features
Structured Output (JSON) Enforce a specific JSON schema for the response.
import { getGenerativeModel, Schema } from "firebase/ai";
const jsonModel = getGenerativeModel(ai, {
model: "<latest_supported_model>", // [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
generationConfig: {
responseMimeType: "application/json",
// Optional: Define a schema
schema = Schema.object({ ... });
}
});
async function getJsonData(prompt) {
const result = await jsonModel.generateContent(prompt);
return JSON.parse(result.response.text());
}On-Device AI (Hybrid) Automatically switch between local Gemini Nano and cloud models based on device capability.
import {getGenerativeModel, InferenceMode } from "firebase/ai";
const hybridModel = getGenerativeModel(ai, { mode: InferenceMode.PREFER_ON_DEVICE });Related skills
Forks & variants (1)
Firebase Ai Logic Basics has 1 known copy in the catalog totaling 569 installs. They canonicalize to this original listing.
- firebase - 569 installs
FAQ
Why is `init ailogic` required?
Running `firebase-tools init ailogic` provisions the AI Logic service and enables the Gemini Developer API. flutterfire configure alone does not enable AI and will cause PERMISSION_DENIED.
Which platforms are supported?
Kotlin and Java for Android, Swift for iOS, JavaScript for web, Dart for Flutter, and C# for Unity, using client-side Firebase SDKs.
Is Firebase Ai Logic Basics safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.