Firebase Deployment
Deploy your Genkit flows to Cloud Functions for Firebase with built-in authentication, monitoring, and Firebase ecosystem integration.Overview
Cloud Functions for Firebase provides:- Managed infrastructure - No server management
- Automatic scaling - From zero to millions of requests
- Firebase integration - Auth, Firestore, Realtime Database
- Built-in monitoring - Integrated telemetry with Firebase
Prerequisites
Project Setup
1. Initialize Firebase Project
- Language: TypeScript
- ESLint: Yes
- Install dependencies: Yes
2. Configure Genkit
Create or updatefunctions/src/index.ts:
functions/src/index.ts
Authentication
Using Firebase Auth
Input Validation
Streaming Responses
Cloud Functions automatically handles streaming:Configuration
Environment Variables and Secrets
Memory and Timeout
CORS Configuration
Deployment
Deploy All Functions
Deploy Specific Function
View Logs
Testing Locally
Using Emulators
- Functions:
http://localhost:5001 - Firestore:
http://localhost:8080 - Auth:
http://localhost:9099
Call Function Locally
Durable Streaming (Beta)
Persist stream state to handle reconnections:X-Genkit-Stream-Id header and can reconnect.
Limitations:
- Firestore documents have a 1MB size limit
- Large streams may exceed this limit
Alternative: Realtime Database
Monitoring with Firebase
Enable Telemetry
View Traces
- Go to Firebase Console
- Select your project
- Navigate to Performance or Cloud Trace
- View detailed execution traces for each flow
Production Best Practices
1. Use Vertex AI (Not Google AI)
For production, use Vertex AI instead of Google AI:2. Set Appropriate Timeouts
3. Handle Errors Gracefully
4. Use IAM for Service-to-Service Calls
Complete Example
See the full Firebase Functions example:js/testapps/firebase-functions-sample1/functions/src/index.ts
Troubleshooting
Function Timeout
Problem: Function times out during AI generation. Solution: Increase timeout:Out of Memory
Problem: Function crashes with OOM error. Solution: Increase memory allocation:Cold Starts
Problem: First request is slow. Solution: Use min instances (costs more):Next Steps
Express Plugin
Learn about Express.js integration
Monitoring
Set up production monitoring