0
0
Firebasecloud~5 mins

Firebase Admin SDK (Node.js) - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to manage your Firebase services from a server or backend securely. The Firebase Admin SDK for Node.js lets you do this by giving your server trusted access to Firebase features like authentication and databases.
When you want to create or manage users in Firebase Authentication from your backend.
When you need to read or write data securely to Firestore or Realtime Database from a server.
When you want to send push notifications to users using Firebase Cloud Messaging from your server.
When you need to verify Firebase ID tokens on your backend to check user identity.
When you want to manage Firebase projects or services programmatically without user interaction.
Config File - serviceAccountKey.json
serviceAccountKey.json
{
  "type": "service_account",
  "project_id": "example-project-123",
  "private_key_id": "abcdef1234567890abcdef1234567890abcdef12",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC...\n-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-abc12@example-project-123.iam.gserviceaccount.com",
  "client_id": "123456789012345678901",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-abc12%40example-project-123.iam.gserviceaccount.com"
}

This JSON file contains the service account credentials that allow your Node.js server to securely access Firebase services. It includes keys and IDs that identify your Firebase project and authorize your server.

Commands
Initialize a new Node.js project to manage dependencies and scripts.
Terminal
npm init -y
Expected OutputExpected
{ "name": "firebase-admin-example", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
Install the Firebase Admin SDK package so you can use it in your Node.js app.
Terminal
npm install firebase-admin
Expected OutputExpected
+ firebase-admin@11.10.1 added 1 package from 1 contributor and audited 1 package in 1.234s found 0 vulnerabilities
Run the Node.js script that initializes Firebase Admin SDK and performs an example action like listing users.
Terminal
node index.js
Expected OutputExpected
Listing users: User: uid1, email: user1@example.com User: uid2, email: user2@example.com Finished listing users.
Key Concept

If you remember nothing else from this pattern, remember: the Firebase Admin SDK lets your server securely manage Firebase services using a special service account key.

Common Mistakes
Trying to use Firebase Admin SDK without downloading and referencing the service account JSON file.
Without the service account credentials, your server cannot authenticate to Firebase and will get permission errors.
Download the service account JSON from Firebase Console and load it in your Node.js app to initialize the SDK.
Committing the service account JSON file to a public code repository.
This exposes your private keys and can let others control your Firebase project, causing security risks.
Keep the service account JSON file secret and add it to .gitignore to avoid uploading it publicly.
Calling Firebase Admin SDK functions before initializing the app with the service account credentials.
The SDK needs initialization to know which project and credentials to use, otherwise calls will fail.
Always initialize the Firebase Admin app with admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }) before using SDK features.
Summary
Initialize a Node.js project and install firebase-admin package.
Download your Firebase service account JSON and use it to initialize the Admin SDK in your code.
Run your Node.js script to securely manage Firebase services like authentication or database.