Skip to main content
The Firebase plugin extends Genkit with three capabilities:
  1. Firestore vector search — use Cloud Firestore’s native vector similarity search as a retriever for RAG pipelines.
  2. Firebase Genkit Monitoring — export traces, metrics, and logs to the Google Cloud Observability suite through a single call.
  3. Firebase Auth integration — verify Firebase ID tokens to protect HTTP-triggered flows (Go only, see auth.go).

Installation

npm install @genkit-ai/firebase

Configuration

The TypeScript Firebase plugin does not use a plugin initializer — it exports standalone functions (defineFirestoreRetriever, enableFirebaseTelemetry) that you call directly.Make sure the Firebase Admin SDK is initialised before using these functions:
import * as admin from 'firebase-admin';

admin.initializeApp();

Firestore as a vector store

Firestore’s vector search lets you find documents whose embedding vectors are nearest to a query embedding. Genkit wraps this with a retriever so your flows can use it for RAG.

Prerequisites

Before querying, create a vector index on the Firestore collection:
gcloud firestore indexes composite create \
  --project=my-project \
  --collection-group=documents \
  --query-scope=COLLECTION \
  --field-config field-path=embedding,vector-config='{"dimension":"768","flat":"{}"}'

Defining a retriever

import { genkit } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';
import { defineFirestoreRetriever } from '@genkit-ai/firebase';
import { initializeApp } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';

initializeApp();
const db = getFirestore();

const ai = genkit({ plugins: [googleAI()] });

const docsRetriever = defineFirestoreRetriever(ai, {
  name: 'documents',
  firestore: db,
  collection: 'documents',
  embedder: googleAI.embedder('gemini-embedding-001'),
  vectorField: 'embedding',
  contentField: 'text',
  // Optional: include metadata fields in results
  metadataFields: ['title', 'source', 'createdAt'],
  // Distance measure: 'COSINE' (default), 'EUCLIDEAN', or 'DOT_PRODUCT'
  distanceMeasure: 'COSINE',
  // Only return documents within this distance
  distanceThreshold: 0.7,
});

Querying the retriever

const docs = await ai.retrieve({
  retriever: docsRetriever,
  query: 'How does RAG work?',
  options: {
    limit: 5,
    // Optional: filter by metadata field
    where: { source: 'official-docs' },
  },
});

// docs is Document[] — use it to build the prompt
const context = docs.map(d => d.text()).join('\n\n');

Indexing documents

Indexing (writing embeddings to Firestore) is handled outside the plugin — you embed each document and write it to the collection using the Firebase Admin SDK:
async function indexDocument(text: string, title: string) {
  const [embedding] = await ai.embed({
    embedder: googleAI.embedder('gemini-embedding-001'),
    content: text,
  });

  await db.collection('documents').add({
    text,
    title,
    embedding: FieldValue.vector(embedding.embedding),
    createdAt: FieldValue.serverTimestamp(),
  });
}

Firebase Genkit Monitoring

Enable telemetry export to Google Cloud Trace, Logging, and Monitoring:
import { enableFirebaseTelemetry } from '@genkit-ai/firebase';

// Call once at app startup, before any flows run
await enableFirebaseTelemetry({
  // Optional: override auto-detected project ID
  // projectId: 'my-project',
});
enableFirebaseTelemetry delegates to the @genkit-ai/google-cloud plugin internally, so all Google Cloud Observability features are available.
When deploying to Firebase App Hosting or Cloud Run, the project ID and credentials are automatically inferred from the runtime environment. No additional configuration is needed.

Firebase Auth for flow security

In Go, the Firebase plugin provides an Auth client you can use to verify Firebase ID tokens in HTTP middleware:
// Retrieve the Auth client from the plugin
firebasePlugin, _ := genkit.LookupPlugin(g, "firebase").(*firebase.Firebase)
authClient, _ := firebasePlugin.Auth(ctx)

// In your HTTP handler:
token, err := authClient.VerifyIDToken(ctx, idTokenFromRequest)
if err != nil {
  http.Error(w, "Unauthorized", http.StatusUnauthorized)
  return
}
// token.UID is the authenticated user

Deploying to Firebase App Hosting

Firebase App Hosting has native support for Genkit. Set up Firebase and deploy:
firebase init apphosting
firebase deploy
See the Firebase deployment guide for a complete walkthrough including environment variable configuration and telemetry setup.

RAG guide

Build retrieval-augmented generation pipelines end-to-end.

Firebase deployment

Deploy Genkit flows to Firebase App Hosting.

Vertex AI plugin

Vertex AI Vector Search for larger-scale retrieval.

Observability

Traces, metrics, and logging in production.