fastify-firebase
v3.0.0
Published
Fastify v5+ plugin for firebase-admin with TypeScript support, ADC, custom app names, and auto-cleanup
Maintainers
Readme
fastify-firebase
Fastify v5+ plugin for firebase-admin. Full TypeScript support.
- Works with service account JSON or Application Default Credentials (GCP)
- Supports
databaseURL,storageBucket, and custom appname - Accepts both camelCase and snake_case cert keys
- Singleton Firebase app with auto-cleanup on close
- Zero config on Cloud Run / Cloud Functions
Install
npm i fastify-firebaseUsage
With service account cert
import Fastify from 'fastify';
import fastifyFirebase from 'fastify-firebase';
import cert from './firebase.json';
const app = Fastify();
app.register(fastifyFirebase, cert);
app.get('/users', async (req, reply) => {
const snapshot = await req.server.firebase.firestore().collection('Users').get();
reply.send(snapshot.docs.map((doc) => doc.data()));
});
app.listen({port: 3000});On GCP (Cloud Run, Cloud Functions) no cert needed
app.register(fastifyFirebase); // uses Application Default CredentialsWith extra options
app.register(fastifyFirebase, {
...cert,
name: 'my-app', // custom app name (default: 'default')
databaseURL: 'https://my-app.firebaseio.com', // for Realtime Database
storageBucket: 'my-app.appspot.com', // for Cloud Storage
});Options
| Option | Type | Description |
|---|---|---|
| projectId / project_id | string | Firebase project ID |
| privateKey / private_key | string | Service account private key |
| clientEmail / client_email | string | Service account email |
| name | string | Firebase app name (default: 'default') |
| databaseURL | string | Realtime Database URL |
| storageBucket | string | Cloud Storage bucket |
All options are optional when using Application Default Credentials on GCP. When providing a cert, all three cert fields are required.
Migration from v2.x
- BREAKING: Registering without options no longer throws uses Application Default Credentials instead
- New options:
databaseURL,storageBucket - Firebase app is now deleted on Fastify close (auto-cleanup)
- Error messages are more specific
Migration from v1.x
- Requires Fastify v5+
- ESM-only (
import/export) - Plugin is async/Promise-based
