Firebase JavaScript SDK

12.12.0 · active · verified Sun Apr 19

The Firebase JavaScript SDK provides a comprehensive client-side library for interacting with Google's Firebase platform, suitable for web, mobile-web (e.g., React Native, Ionic), and Node.js desktop (e.g., Electron) applications. The current stable version is 12.12.0, with minor versions released frequently, often weekly or bi-weekly, reflecting continuous development and updates across its sub-packages. It offers a suite of services including Authentication, Cloud Firestore, Realtime Database, Cloud Storage, Cloud Functions, Messaging, Performance Monitoring, Analytics, Remote Config, App Check, and AI capabilities. A key differentiator since version 9 is its modular, tree-shakable API, which significantly improves application bundle sizes. This SDK is distinct from the Firebase Admin Node.js SDK, which is designed for privileged server environments and administrative access.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a Firebase app and demonstrates adding a document to Cloud Firestore using the modular API.

import { initializeApp } from 'firebase/app';
import { getFirestore, collection, addDoc } from 'firebase/firestore';

// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_PROJECT_ID.appspot.com",
  messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
  appId: "YOUR_APP_ID",
  // Optional: measurementId: "G-XXXXXXXXXX"
};

async function initializeAndAddData() {
  try {
    const app = initializeApp(firebaseConfig);
    console.log("Firebase app initialized successfully.");

    const db = getFirestore(app);
    const docRef = await addDoc(collection(db, "users"), {
      first: "Ada",
      last: "Lovelace",
      born: 1815
    });
    console.log("Document written with ID: ", docRef.id);
  } catch (error: any) {
    console.error("Error initializing Firebase or adding document:", error.message);
  }
}

initializeAndAddData();

view raw JSON →