Firebase AI Logic Basics
Firebase (Google)OfficialCreated by Google LLCDistributed by Firebase (Google)
- Skill
- Tool integration
- Software development
- Apache-2.0
- Low risk
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
Firebase AI Logic (formerly Vertex AI for Firebase) enables developers to incorporate generative AI features directly into client-side applications. By using the Firebase SDKs for Android, iOS, Web, and Flutter, developers can call Gemini models without managing a dedicated backend. The service supports text generation, multimodal inputs, chat sessions, streaming, and structured JSON outputs, while providing security through Firebase App Check and configuration management via Remote Config.
What it does
- Provisions the Firebase AI Logic service via the Firebase CLI.
- Integrates Gemini models into mobile and web client code.
- Supports multimodal inputs including images, audio, video, and PDFs.
- Enables multi-turn chat sessions and streaming responses.
- Provides client-side Text-to-Speech generation using dedicated models.
- Enforces security and quota protection using Firebase App Check.
When to use it
- When adding generative AI features to a mobile or web application.
- When you need to call Gemini models directly from client-side code.
- When you require a managed service for AI features without building a custom backend.
- When implementing features like image analysis, chat, or speech synthesis in an app.
- When you need to secure AI model access using Firebase App Check.
Advantages
- Eliminates the need for a dedicated AI backend.
- Supports multiple platforms including Android, iOS, Web, and Flutter.
- Provides built-in security and quota management via App Check.
- Allows dynamic model updates using Firebase Remote Config.
- Supports hybrid on-device inference for improved performance.
Usage examples
- “How do I provision Firebase AI Logic for my project?”
- The agent provides the command 'npx -y firebase-tools@latest init ailogic' to enable the service in the Firebase project.
- “Show me how to initialize the Gemini model in a Flutter app.”
- The agent provides the code to import 'firebase_ai', initialize the service, and call 'googleAI.generativeModel' with the latest supported model.
- “How can I secure my AI calls in my iOS app?”
- The agent explains the necessity of setting up Firebase App Check and provides the Swift code to initialize the AI service with 'useLimitedUseAppCheckTokens: true'.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/firebase/firebase-ai-logic-basics/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip firebase-ai-logic-basics.zip -d .agents/skills
Documentation by Google LLC
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. The app can directly call Gemini models via Firebase AI Logic without the developer needing to manage a dedicated backend. Firebase AI Logic was previously known as "Vertex AI for Firebase".
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
Agent Platform Gemini API (formerly branded Vertex AI): Ideal for scale with enterprise-grade production readiness, requires Blaze pricing plan
Use the Gemini Developer API as a default, and only Agent Platform Gemini API (formerly branded Vertex AI) if the application requires it.
Setup & initialization
Prerequisites
Identify the platform the user is interested in building on prior to starting: Android, iOS, Web, or Flutter.
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)
The Firebase CLI commands below run through
npxon every platform, so they need Node.js 20+ and npm. Use thefirebase-basicsskill to check for them and to log in to the Firebase CLI. If Node.js is missing or older than version 20, ask the user to install it as that skill describes. Don't install system packages yourself, for example withsudo apt-get, because that changes the user's machine without their consent.
1. Provision Firebase AI Logic (ALL platforms)
Use the Firebase CLI to provision Firebase AI Logic for the Firebase Project:
Make sure that you're in a firebase directory (with a
firebase.json).Verify that you're running Firebase CLI commands against the intended Firebase Project. When you run this command, the active project is marked with "current":
npx -y firebase-tools@latest projects:listMake sure there's at least one Firebase App for the target platform registered with the current Firebase Project:
npx -y firebase-tools@latest apps:listProvision Firebase AI Logic for the Firebase Project:
npx -y firebase-tools@latest init ailogicThis command will enable the Gemini Developer API in the Firebase project.
[!WARNING] CRITICAL: Provisioning Firebase AI Logic is Required For all platforms (Android, iOS, Web, or Flutter), even if the app already uses Firebase, you MUST run
npx -y firebase-tools@latest init ailogicto provision the service. The configuration files ofGoogleService-Info.plist,google-services.json, andfirebase_options.dartONLY handle client configuration and do NOT enable the Firebase AI Logic service, leading toPERMISSION_DENIEDerrors.
If you can't run init ailogic yourself (for example because Node.js is missing
or the Firebase CLI isn't logged in) do NOT skip it. The app's requests to
Gemini models will fail until the Firebase AI Logic service is provisioned. Tell
the user to run this command in their project directory, and include it in your
final summary:
npx -y firebase-tools@latest init ailogic2. Add the SDK and initialize the service in the app
Adding the SDK dependency and initializing the service are platform-specific. Before writing code, read the reference for the target platform listed under Initialization Code References.
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 any Gemini 1.x or Gemini 2.x models (such as
gemini-1.5-flash,gemini-1.5-pro,gemini-2.0-flash,gemini-2.0-pro,gemini-2.5-flash,gemini-2.5-flash-lite, orgemini-2.5-pro), as older models are deprecated or shut down. Always use a currently supported Gemini 3+ model; if URL access is blocked in your environment, proceed directly without searching local files.
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.
Text-to-Speech (TTS) Generation
Generate spoken audio directly on client devices without a custom speech backend. Firebase AI Logic supports speech synthesis using dedicated Gemini TTS models:
Supported Models:
gemini-3.1-flash-tts-previewCapabilities:
Single-speaker voice persona selection (
voiceName) and multi-speaker dialogues (up to 2 distinct speakers) viaSpeechConfig/MultiSpeakerVoiceConfigDirect audio responses by setting
responseModalitiesto audioStreaming speech responses with
generateContentStreamfor low-latency playbackAudio directives (
[Audio Profile: ...],[Scene: ...],[Director's Note: ...]) and emotional tags ([whispers],[laughs],[slowly])Client-side decoding and playback of 24 kHz 16-bit linear PCM (
audio/l16) or WAV container format (audio/x-wav)
See the Text-to-Speech (TTS) Generation Guide for full implementation details, streaming patterns, and copy-paste code snippets for iOS (Swift), Android (Kotlin), and Web.
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 inference runs prompts with an on-device model when one is available and falls back to a cloud-hosted model otherwise (or vice versa). Availability and setup steps differ by platform, more info in the hybrid and on-device documentation, which links to each platform's guide.
Security & Production
Firebase 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 for Firebase AI Logic for setup instructions. Each platform uses its own attestation provider, so follow the instructions for the target platform.
App Check Debug Tokens for Local Development & CI/CD
Because App Check attestation providers (like Play Integrity or DeviceCheck) reject emulators, simulators, or CI environments, you must use App Check Debug Tokens during development and testing to bypass standard attestation.
[!WARNING] CRITICAL: Never Hardcode or Commit Debug Tokens App Check debug tokens allow clients to bypass attestation and access backend resources without a genuine device. Treat them as private secrets. Never commit debug tokens to version control or hardcode raw token strings in client code (Web, Android, iOS, or Flutter). Always inject them through local environment variables, gitignored local configurations, or CI secrets. If a token is compromised, revoke it immediately in the Firebase Console.
Local Development (Auto-Generated)
Configure your code's App Check provider to use the debug factory. The API is platform-specific; see the App Check section of the platform reference or the App Check documentation linked above.
Run your app in the emulator/localhost.
Look at your runtime debugger console / Logcat logs for the generated UUID:
Example:
AppCheck debug token: "123a4567-b89c-12d3-e456-789012345678"
Register this token in the Firebase Console under Security > App Check > Apps > Manage debug tokens.
💡 Tip (Prevent Debug Token Churn): Simulator or emulator resets, fresh installs, and clearing browser data can make the SDK generate a new debug token that you must register again. To keep a stable debug token without hardcoding it, see the App Check section of the platform reference.
CI/CD Pipelines (Pre-Provisioned)
Generate and register a new debug token in the Firebase Console under Security > App Check > Apps > Manage debug tokens.
Add this token string as an encrypted secret in your CI system (e.g.
APP_CHECK_DEBUG_TOKEN).Configure your build to pass this secret as an environment variable to the SDK during test execution.
Firebase 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
Initialization Code References
Android (Kotlin)
Gemini API Provider: Gemini Developer API
Reference: usage_patterns_android.md
iOS (Swift)
Gemini API Provider: Gemini Developer API
Reference: ios_setup.md
Web Modular API
Gemini API Provider: Gemini Developer API
Reference: usage_patterns_web.md
Flutter (Dart)
Gemini API Provider: Gemini Developer API
Reference: 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 any Gemini 1.x or Gemini 2.x models (such as
gemini-1.5-flash,gemini-2.0-flash,gemini-2.5-flash, orgemini-2.5-flash-lite), as older models are deprecated or shut down.
References
Android (Kotlin) SDK usage patterns iOS SDK code examples and usage patterns Web SDK setup, code examples, and usage patterns
Flutter SDK code examples and usage patterns Client-side Text-to-Speech (TTS) generation
Files
- ATTRIBUTION.mdAdded by AutoGineer984 B
- LICENSELicence file added by AutoGineer11.1 KiB
- SKILL.md12.3 KiB
- references/flutter_setup.md5.0 KiB
- references/ios_setup.md8.1 KiB
- references/tts_generation.md21.7 KiB
- references/usage_patterns_android.md7.2 KiB
- references/usage_patterns_web.md8.6 KiB
references/ios_setup.md
Firebase AI Logic iOS Setup Guide
1. Import and Initialize
Ensure you have installed the FirebaseAILogic SDK via Swift Package Manager.
Use the xcode-project-setup skill to automate adding the SPM dependency to the
Xcode project.
import FirebaseAILogic
// To enable replay protection for generative or preview models, pass
// useLimitedUseAppCheckTokens: true with your backend:
let ai = FirebaseAI.firebaseAI(
backend: .googleAI(),
useLimitedUseAppCheckTokens: true
)
// [AGENT] Replace "<latest_supported_model>" with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt (do NOT use Gemini 1.x or 2.x models)
let model = ai.generativeModel(modelName: "<latest_supported_model>")App Check Replay Protection
Generative and preview models enforce replay protection with 5-minute limited-use App Check tokens. If you call a protected model without enabling limited-use tokens, the request fails with:
HTTP 403: "To access this model, you must enforce Firebase App Check"To resolve this error, initialize FirebaseAI with
useLimitedUseAppCheckTokens: true:
let ai = FirebaseAI.firebaseAI(
backend: .googleAI(),
useLimitedUseAppCheckTokens: true
)This ensures the SDK fetches a fresh, short-lived limited-use token for each request rather than reusing a standard cached App Check token.
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 the model 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(
backend: .googleAI(),
useLimitedUseAppCheckTokens: true
).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(harmCategory: .harassment, threshold: .blockLowAndAbove),
SafetySetting(harmCategory: .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
)4. App Check Debug Provider
For App Check debug tokens during local development and CI/CD, add the
FirebaseAppCheck product to the app target (with the xcode-project-setup
skill), then set the debug provider factory in debug builds before
FirebaseApp.configure(). Otherwise the debug provider isn't used.
import FirebaseAppCheck
import FirebaseCore
// In your app's init(), before configuring Firebase:
#if DEBUG
AppCheck.setAppCheckProviderFactory(AppCheckDebugProviderFactory())
#endif
FirebaseApp.configure()For CI/CD, add an AppCheckDebugToken environment variable to the Xcode test
scheme with a value like $(APP_CHECK_DEBUG_TOKEN), then pass the token from
the CI test command:
xcodebuild test -scheme <scheme> -workspace <project>.xcworkspace \
APP_CHECK_DEBUG_TOKEN=<token>Debug Token Persistence
When running on simulators or during development, the Firebase iOS SDK generates
a new debug token UUID whenever NSUserDefaults is cleared (e.g., simulator
reset or fresh install). To avoid invalidating tokens registered in the Firebase
Console and prevent token churn, persist a stable token by setting the
AppCheckDebugToken environment variable, which the SDK reads automatically at
runtime:
[!WARNING] CRITICAL: Never Hardcode or Commit Debug Tokens Debug tokens grant access to backend resources without device attestation. Never commit debug tokens to version control or hardcode token strings in source code.
In Xcode Scheme (Recommended): Edit Scheme -> Run -> Arguments -> Environment Variables -> Add
AppCheckDebugToken = <YOUR_DEBUG_TOKEN>. Keep user schemes (xcuserdata/) unshared and gitignored.In Code (Safe Dynamic Loading Only): If setting the environment variable in code before configuring
AppCheckDebugProviderFactory, load the token dynamically from a gitignored local file or environment rather than hardcoding the token literal:
#if DEBUG
// ✅ SAFE: Load from gitignored local file or process environment
// Note: loadGitIgnoredDebugToken() is a placeholder for your custom helper (e.g., reading from a gitignored plist)
if let debugToken = loadGitIgnoredDebugToken() {
setenv("AppCheckDebugToken", debugToken, 0)
}
let providerFactory = AppCheckDebugProviderFactory()
AppCheck.setAppCheckProviderFactory(providerFactory)
#endif
FirebaseApp.configure() // Configure Firebase AFTER setting the App Check provider factoryAdvanced 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": .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)")
}Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Node.js
Security
What our scanner noticed· Low risk
- Instructions that use the network
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Google LLC
- Publisher
- Firebase (Google) (Official)
- Distributed by
- Firebase (Google)
- Source
- firebase/agent-skills · skills/firebase-ai-logic-basics · commit b76be96
- Licence
- Apache License 2.0 (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Google LLC or Firebase (Google).
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download firebase-ai-logic-basics.zip