Add the Firebase Admin SDK to your Server

Prerequisites

Before you begin, make sure you have the following:

  • If using the Admin Node.js SDK, a server running Node.js 0.12 or later.
  • If using the Admin Java SDK, a server running Java 7 or later.
  • A server app.

Add Firebase to your app

To use the Firebase Admin SDKs, you'll need a Firebase project, a service account to communicate with the Firebase service, and a configuration file with your service account's credentials.

  1. Navigate to the Service Accounts tab in your project's settings page.
  2. Select your Firebase project. If you don't already have one, click the Create New Project button. If you already have an existing Google project associated with your app, click Import Google Project instead.
  3. Click the Generate New Private Key button at the bottom of the Firebase Admin SDK section of the Service Accounts tab.

After you click the button, a JSON file containing your service account's credentials will be downloaded. You'll need this to initialize the SDK in the next step.

Add the SDK

If you are setting up a new project, you need to install the SDK for the language of your choice.

Node.js

The Firebase Admin Node.js SDK is available on npm. If you don't already have a package.json file, create one via npm init. Next, install the firebase-admin npm package and save it to your package.json:

$ npm install firebase-admin --save

To use the module in your application, require it from any JavaScript file:

var admin = require("firebase-admin");

If you are using ES2015, you can import the module instead:

import * as admin from "firebase-admin";

Java

The Firebase Admin Java SDK is published to the Maven central repository. To install the library, declare it as a dependency in your build.gradle file:

dependencies {
  compile 'com.google.firebase:firebase-admin:4.1.3'
}

If you use Maven to build your application, you can add the following dependency to your pom.xml:

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-admin</artifactId>
  <version>4.1.3</version>
</dependency>

Initialize the SDK

Once you have created a Firebase console project and downloaded a JSON file with your service account credentials, you can initialize the SDK with this code snippet:

Node.js

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

Java

FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

FirebaseOptions options = new FirebaseOptions.Builder()
  .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
  .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
  .build();

FirebaseApp.initializeApp(options);

You can find your database name on the Database page of your Firebase console project.

If the service account file cannot be referenced, the Admin Node.js SDK can accept the service account's individual fields inline:

Node.js

admin.initializeApp({
  credential: admin.credential.cert({
    projectId: "<PROJECT_ID>",
    clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
    privateKey: "-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n"
  }),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

The Admin SDKs can alternatively be authenticated with a different credential type. For example, if you are running your code within Google Cloud Platform, you can make use of Google Application Default Credentials to automatically have the Admin SDKs themselves fetch a service account on your behalf:

Node.js

admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

Java

FirebaseOptions options = new FirebaseOptions.Builder()
  .setCredential(FirebaseCredentials.applicationDefault())
  .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
  .build();

FirebaseApp.initializeApp(options);

The Admin SDKs also provide a credential which allows you to authenticate with a Google OAuth2 refresh token:

Node.js

var refreshToken; // Get refresh token from OAuth2 flow

admin.initializeApp({
  credential: admin.credential.refreshToken(refreshToken),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

Java

FileInputStream refreshToken = new FileInputStream("path/to/refreshToken.json");

FirebaseOptions options = new FirebaseOptions.Builder()
  .setCredential(FirebaseCredentials.fromRefreshToken(refreshToken))
  .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
  .build();

FirebaseApp.initializeApp(options);

You are now ready to use the Firebase Admin SDKs to accomplish the following tasks:

Initialize multiple apps

In most cases, you only have to initialize a single, default app. You can access services off of that app in two equivalent ways:

Node.js

// Initialize the default app
var defaultApp = admin.initializeApp(defaultAppConfig);

console.log(defaultApp.name);  // "[DEFAULT]"

// Retrieve services via the defaultApp variable...
var defaultAuth = defaultApp.auth();
var defaultDatabase = defaultApp.database();

// ... or use the equivalent shorthand notation
defaultAuth = admin.auth();
defaultDatabase = admin.database();

Java

// Initialize the default app
FirebaseApp defaultApp = FirebaseApp.initializeApp(defaultOptions);

System.out.println(defaultApp.getName());  // "[DEFAULT]"

// Retrieve services by passing the defaultApp variable...
FirebaseAuth defaultAuth = FirebaseAuth.getInstance(defaultApp);
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance(defaultApp);

// ... or use the equivalent shorthand notation
FirebaseAuth defaultAuth = FirebaseAuth.getInstance();
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();

Some use cases require you to create multiple apps at the same time. For example, you might want to read data from the Realtime Database of one Firebase project and mint custom tokens for another project. Or you might want to authenticate two apps with separate credentials. The Firebase SDK allows you create multiple apps at the same time, each with their own configuration information.

Node.js

// Initialize the default app
admin.initializeApp(defaultAppConfig);

// Initialize another app with a different config
var otherApp = admin.initializeApp(otherAppConfig, "other");

console.log(admin.app().name);  // "[DEFAULT]"
console.log(otherApp.name);     // "other"

// Use the shorthand notation to retrieve the default app's services
var defaultAuth = admin.auth();
var defaultDatabase = admin.database();

// Use the otherApp variable to retrieve the other app's services
var otherAuth = otherApp.auth();
var otherDatabase = otherApp.database();

Java

// Initialize the default app
FirebaseApp.initializeApp(defaultOptions);

// Initialize another app with a different config
FirebaseApp otherApp = FirebaseApp.initializeApp(otherAppConfig, "other");

System.out.println(defaultApp.getName());  // "[DEFAULT]"
System.out.println(otherApp.getName());    // "other"

// Use the shorthand notation to retrieve the default app's services
FirebaseAuth defaultAuth = FirebaseAuth.getInstance();
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();

// Use the otherApp variable to retrieve the other app's services
FirebaseAuth otherAuth = FirebaseAuth.getInstance(otherApp);
FirebaseDatabase otherDatabase = FirebaseDatabase.getInstance(otherApp);

Send feedback about...

Need help? Visit our support page.