Firebase provides a comprehensive suite of backend services for modern web and mobile applications. The Firebase JavaScript SDK offers a modular architecture with tree-shaking support, enabling developers to include only the services they need while maintaining optimal bundle size.
npm install firebaseFirebase uses a modular import system where each service is imported from its own subpath:
import { initializeApp } from 'firebase/app';
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';
import { getFirestore, collection, getDocs } from 'firebase/firestore';For CommonJS:
const { initializeApp } = require('firebase/app');
const { getAuth, signInWithEmailAndPassword } = require('firebase/auth');
const { getFirestore, collection, getDocs } = require('firebase/firestore');import { initializeApp } from 'firebase/app';
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';
import { getFirestore, collection, getDocs } from 'firebase/firestore';
// Initialize Firebase app
const firebaseConfig = {
apiKey: "your-api-key",
authDomain: "your-project.firebaseapp.com",
projectId: "your-project-id",
storageBucket: "your-project.appspot.com",
messagingSenderId: "123456789",
appId: "your-app-id"
};
const app = initializeApp(firebaseConfig);
// Use services
const auth = getAuth(app);
const db = getFirestore(app);
// Authenticate user
const userCredential = await signInWithEmailAndPassword(auth, 'user@example.com', 'password');
// Query database
const querySnapshot = await getDocs(collection(db, 'users'));
querySnapshot.forEach((doc) => {
console.log(doc.id, doc.data());
});Firebase JavaScript SDK is built around several key architectural principles:
Core Firebase app management and initialization functionality. Required for all other Firebase services.
function initializeApp(options: FirebaseOptions, name?: string): FirebaseApp;
function getApp(name?: string): FirebaseApp;
function getApps(): FirebaseApp[];
function deleteApp(app: FirebaseApp): Promise<void>;
interface FirebaseApp {
readonly name: string;
readonly options: FirebaseOptions;
automaticDataCollectionEnabled: boolean;
}
interface FirebaseOptions {
apiKey?: string;
authDomain?: string;
databaseURL?: string;
projectId?: string;
storageBucket?: string;
messagingSenderId?: string;
appId?: string;
measurementId?: string;
}User authentication and identity management with support for multiple providers including email/password, OAuth (Google, Facebook, Twitter), phone numbers, and anonymous authentication.
function getAuth(app?: FirebaseApp): Auth;
function signInWithEmailAndPassword(auth: Auth, email: string, password: string): Promise<UserCredential>;
function createUserWithEmailAndPassword(auth: Auth, email: string, password: string): Promise<UserCredential>;
function signOut(auth: Auth): Promise<void>;
function onAuthStateChanged(auth: Auth, nextOrObserver: NextOrObserver<User>): Unsubscribe;
interface Auth {
readonly app: FirebaseApp;
readonly currentUser: User | null;
languageCode: string | null;
}
interface User {
readonly uid: string;
readonly email: string | null;
readonly displayName: string | null;
readonly photoURL: string | null;
readonly emailVerified: boolean;
}Firebase Authentication provides specialized variants for different platforms:
firebase/auth/cordova - Native mobile integration with OAuth providersfirebase/auth/web-extension - Browser extension-optimized authentication flowsCordova Authentication | Web Extension Authentication
NoSQL document database with real-time synchronization, offline support, and powerful querying capabilities. Includes both full and lite versions for different use cases.
function getFirestore(app?: FirebaseApp): Firestore;
function collection(firestore: Firestore, path: string): CollectionReference<DocumentData>;
function doc(firestore: Firestore, path: string): DocumentReference<DocumentData>;
function addDoc(reference: CollectionReference<T>, data: T): Promise<DocumentReference<T>>;
function getDoc(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
function onSnapshot(reference: DocumentReference<T>, observer: (snapshot: DocumentSnapshot<T>) => void): Unsubscribe;
interface Firestore {
readonly app: FirebaseApp;
}
interface DocumentReference<T = DocumentData> {
readonly firestore: Firestore;
readonly id: string;
readonly path: string;
}
interface CollectionReference<T = DocumentData> extends Query<T> {
readonly firestore: Firestore;
readonly id: string;
readonly path: string;
}Real-time JSON database with automatic data synchronization across all connected clients. Perfect for applications requiring live updates and collaboration features.
function getDatabase(app?: FirebaseApp, url?: string): Database;
function ref(database: Database, path?: string): DatabaseReference;
function set(ref: DatabaseReference, value: unknown): Promise<void>;
function get(query: Query): Promise<DataSnapshot>;
function on(query: Query, eventType: EventType, callback: (snapshot: DataSnapshot) => unknown): Unsubscribe;
interface Database {
readonly app: FirebaseApp;
}
interface DatabaseReference extends Query {
readonly key: string | null;
readonly parent: DatabaseReference | null;
readonly root: DatabaseReference;
}
interface DataSnapshot {
exists(): boolean;
val(): any;
key: string | null;
ref: DatabaseReference;
}File upload and download service with automatic scaling and CDN distribution. Supports resumable uploads, metadata management, and security rules.
function getStorage(app?: FirebaseApp, bucketUrl?: string): FirebaseStorage;
function ref(storage: FirebaseStorage, url?: string): StorageReference;
function uploadBytes(ref: StorageReference, data: Blob | Uint8Array | ArrayBuffer): Promise<UploadResult>;
function getDownloadURL(ref: StorageReference): Promise<string>;
function deleteObject(ref: StorageReference): Promise<void>;
interface FirebaseStorage {
readonly app: FirebaseApp;
readonly bucket: string;
maxUploadRetryTime: number;
maxOperationRetryTime: number;
}
interface StorageReference {
readonly bucket: string;
readonly fullPath: string;
readonly name: string;
readonly storage: FirebaseStorage;
}Call server-side functions deployed to Firebase Cloud Functions. Provides seamless integration between client and server code with automatic scaling and HTTPS endpoints.
function getFunctions(app?: FirebaseApp, regionOrCustomDomain?: string): Functions;
function httpsCallable<RequestData = any, ResponseData = any>(
functionsInstance: Functions,
name: string
): HttpsCallable<RequestData, ResponseData>;
interface Functions {
readonly app: FirebaseApp;
}
interface HttpsCallable<RequestData = any, ResponseData = any> {
(data?: RequestData): Promise<HttpsCallableResult<ResponseData>>;
}
interface HttpsCallableResult<ResponseData = any> {
readonly data: ResponseData;
}Google Analytics integration with automatic event tracking and custom event logging. Provides detailed insights into user behavior and app performance.
function getAnalytics(app?: FirebaseApp): Analytics;
function logEvent(analyticsInstance: Analytics, eventName: string, eventParams?: EventParams): void;
function setUserId(analyticsInstance: Analytics, id: string | null): void;
function setUserProperties(analyticsInstance: Analytics, properties: CustomParams): void;
interface Analytics {
readonly app: FirebaseApp;
}
interface EventParams {
[key: string]: any;
}
interface CustomParams {
[key: string]: any;
}Push notification service for web and mobile applications. Supports targeted messaging, topic subscriptions, and rich notifications.
function getMessaging(app?: FirebaseApp): Messaging;
function getToken(messaging: Messaging, options?: GetTokenOptions): Promise<string>;
function onMessage(messaging: Messaging, nextOrObserver: NextOrObserver<MessagePayload>): Unsubscribe;
interface Messaging {
readonly app: FirebaseApp;
}
interface MessagePayload {
readonly data?: { [key: string]: string };
readonly notification?: NotificationPayload;
}
interface GetTokenOptions {
vapidKey?: string;
serviceWorkerRegistration?: ServiceWorkerRegistration;
}For background message handling in web applications:
firebase/messaging/sw - Handle push notifications when app is not activeApplication performance monitoring with automatic metrics collection and custom trace creation. Helps identify bottlenecks and optimize user experience.
function getPerformance(app?: FirebaseApp): FirebasePerformance;
function trace(performance: FirebasePerformance, traceName: string): Trace;
interface FirebasePerformance {
readonly app: FirebaseApp;
}
interface Trace {
start(): void;
stop(): void;
putAttribute(attributeName: string, attributeValue: string): void;
putMetric(metricName: string, num: number): void;
}Dynamic configuration service that allows you to change app behavior without deploying updates. Perfect for A/B testing and feature flags.
function getRemoteConfig(app?: FirebaseApp): RemoteConfig;
function activate(remoteConfig: RemoteConfig): Promise<boolean>;
function fetchAndActivate(remoteConfig: RemoteConfig): Promise<boolean>;
function getString(remoteConfig: RemoteConfig, key: string): string;
function getBoolean(remoteConfig: RemoteConfig, key: string): boolean;
function getNumber(remoteConfig: RemoteConfig, key: string): number;
interface RemoteConfig {
readonly app: FirebaseApp;
settings: RemoteConfigSettings;
defaultConfig: { [key: string]: string | number | boolean };
}
interface Value {
asBoolean(): boolean;
asNumber(): number;
asString(): string;
}App attestation service that protects your backend resources from abuse. Verifies that requests come from your authentic app.
function initializeAppCheck(app: FirebaseApp, options: AppCheckOptions): AppCheck;
function getToken(appCheckInstance: AppCheck, forceRefresh?: boolean): Promise<AppCheckTokenResult>;
interface AppCheck {
readonly app: FirebaseApp;
}
interface AppCheckTokenResult {
readonly token: string;
}
interface AppCheckOptions {
provider: AppCheckProvider;
isTokenAutoRefreshEnabled?: boolean;
}Generative AI capabilities powered by Google's Vertex AI platform. Enables natural language processing, content generation, and chat functionality.
function getVertexAI(app?: FirebaseApp, options?: VertexAIOptions): VertexAI;
function getGenerativeModel(vertexAI: VertexAI, modelParams: ModelParams): GenerativeModel;
function generateContent(model: GenerativeModel, request: GenerateContentRequest): Promise<GenerateContentResult>;
interface VertexAI {
readonly app: FirebaseApp;
}
interface GenerativeModel {
generateContent(request: GenerateContentRequest): Promise<GenerateContentResult>;
startChat(startChatParams?: StartChatParams): ChatSession;
}
interface GenerateContentRequest {
contents: Content[];
}Relational database integration with PostgreSQL, generated client SDKs, and real-time subscriptions. Perfect for applications requiring structured data with relationships.
function getDataConnect(app: FirebaseApp, options: ConnectorConfig): DataConnect;
function queryRef<Data, Variables>(
dataConnectInstance: DataConnect,
queryName: string,
variables?: Variables
): QueryRef<Data, Variables>;
function mutationRef<Data, Variables>(
dataConnectInstance: DataConnect,
mutationName: string,
variables?: Variables
): MutationRef<Data, Variables>;
function subscribe<Data, Variables>(
queryRef: QueryRef<Data, Variables>,
observer: SubscriptionOptions<Data, Variables>
): QueryUnsubscribe;
interface ConnectorConfig {
location: string;
connector: string;
service: string;
}Firebase Installation ID management for analytics, messaging, and service authentication. Provides unique identifiers for each app installation.
function getInstallations(app?: FirebaseApp): Installations;
function getId(installations: Installations): Promise<string>;
function getToken(installations: Installations, forceRefresh?: boolean): Promise<string>;
function onIdChange(installations: Installations, callback: IdChangeCallbackFn): IdChangeUnsubscribeFn;
function deleteInstallations(installations: Installations): Promise<void>;
interface Installations {
readonly app: FirebaseApp;
}Lightweight, read-only Cloud Firestore client optimized for server-side applications and scenarios without real-time updates.
function getFirestore(app?: FirebaseApp): FirebaseFirestore;
function doc(firestore: FirebaseFirestore, path: string): DocumentReference<DocumentData>;
function collection(firestore: FirebaseFirestore, path: string): CollectionReference<DocumentData>;
function getDoc<T>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
function getAggregateFromServer<T>(
query: Query<T>,
aggregateSpec: { count: AggregateField<number> }
): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }>>;class FirebaseError extends Error {
readonly code: string;
readonly message: string;
readonly name: string;
}type Unsubscribe = () => void;
type NextOrObserver<T> = ((value: T) => void) | Observer<T>;
interface Observer<T> {
next?: (value: T) => void;
error?: (error: Error) => void;
complete?: () => void;
}interface FirebaseOptions {
apiKey?: string;
authDomain?: string;
databaseURL?: string;
projectId?: string;
storageBucket?: string;
messagingSenderId?: string;
appId?: string;
measurementId?: string;
}Firebase v9+ provides compatibility packages under the firebase/compat/* namespace for easier migration from v8:
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
// v8-style API
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();The compat layer provides the same API as v8 while benefiting from the tree-shaking optimizations of v9+.