0
0
FirebaseHow-ToBeginner · 4 min read

How to Use Firebase Admin SDK: Setup and Basic Usage

To use the Firebase Admin SDK, first install it in your server environment, then initialize it with your service account credentials using admin.initializeApp(). This lets you securely manage Firebase services like authentication and database from backend code.
📐

Syntax

The Firebase Admin SDK is initialized in your backend code by importing the firebase-admin package and calling admin.initializeApp() with your service account credentials. This setup allows your server to securely access Firebase services.

  • require('firebase-admin'): Imports the SDK.
  • admin.initializeApp({ credential }): Initializes the SDK with credentials.
  • admin.credential.cert(serviceAccount): Loads your service account key.
javascript
const admin = require('firebase-admin');

const serviceAccount = require('path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
💻

Example

This example shows how to initialize the Firebase Admin SDK and create a new user in Firebase Authentication from a Node.js backend.

javascript
const admin = require('firebase-admin');

const serviceAccount = require('./serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

async function createUser() {
  try {
    const userRecord = await admin.auth().createUser({
      email: 'user@example.com',
      emailVerified: false,
      password: 'secretPassword',
      displayName: 'John Doe',
      disabled: false
    });
    console.log('Successfully created new user:', userRecord.uid);
  } catch (error) {
    console.error('Error creating new user:', error);
  }
}

createUser();
Output
Successfully created new user: <user-uid>
⚠️

Common Pitfalls

Common mistakes when using Firebase Admin SDK include:

  • Not using a valid service account JSON file or incorrect path causes initialization errors.
  • Trying to use the Admin SDK in client-side code exposes sensitive credentials.
  • Not handling asynchronous calls properly can lead to unhandled promise rejections.

Always keep your service account file secure and use the Admin SDK only in trusted server environments.

javascript
/* Wrong: Using Admin SDK in frontend code (exposes credentials) */
// <script>
// const admin = require('firebase-admin'); // This should never run in browser
// </script>

/* Right: Use Admin SDK only in backend Node.js code */
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });
📊

Quick Reference

Here is a quick summary of key Firebase Admin SDK usage points:

ActionMethodNotes
Initialize SDKadmin.initializeApp({ credential })Use service account credentials JSON
Create Useradmin.auth().createUser({email, password})Creates new Firebase Auth user
Get Useradmin.auth().getUser(uid)Fetch user data by UID
Delete Useradmin.auth().deleteUser(uid)Remove user from Firebase Auth
Send FCM Messageadmin.messaging().send(message)Send push notifications

Key Takeaways

Initialize Firebase Admin SDK with a service account JSON file on your server.
Use the Admin SDK only in backend environments to keep credentials secure.
Handle asynchronous calls with async/await or promises to avoid errors.
The Admin SDK lets you manage users, send messages, and access Firebase services programmatically.
Always keep your service account file private and never expose it in client code.