Skip to main content

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

Select:
  • Language: TypeScript
  • ESLint: Yes
  • Install dependencies: Yes

2. Configure Genkit

Create or update functions/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

Set secrets:

Memory and Timeout

CORS Configuration

Deployment

Deploy All Functions

Deploy Specific Function

View Logs

Testing Locally

Using Emulators

The emulator provides:
  • Functions: http://localhost:5001
  • Firestore: http://localhost:8080
  • Auth: http://localhost:9099

Call Function Locally

Durable Streaming (Beta)

Persist stream state to handle reconnections:
Clients receive a stream ID in the 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

  1. Go to Firebase Console
  2. Select your project
  3. Navigate to Performance or Cloud Trace
  4. 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

Allow access:

Complete Example

See the full Firebase Functions example:
Source: 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