Add Firebase to your Server

Prerequisites

Before you begin, make sure you have the following:

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

Add Firebase to your app

To add Firebase to your app, 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. Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your app, click Import Google Project. Otherwise, click Create New Project.
  2. Click settings and select Permissions.
  3. Select Service accounts from the menu on the left.
  4. Click Create service account.
    1. Enter a name for your service account. You can optionally customize the ID from the one automatically generated from the name.
    2. Choose Project > Editor from the Role dropdown.
    3. Select Furnish a new private key and leave the Key type as JSON.
    4. Leave Enable Google Apps Domain-wide Delegation unselected.
    5. Click Create.

When you create the service account, a JSON file containing your service account's credentials is downloaded for you. 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

You can install the Firebase Node.js module with npm from the command line:

$ npm install firebase --save

To use the Firebase Node.js module in your application, require the Firebase module:

var firebase = require("firebase");

Java

We publish the Firebase Java SDK to the Maven central repository. To install the library, you can simply declare it as a dependency in your build.gradle file:

dependencies {
  compile 'com.google.firebase:firebase-server-sdk:3.0.1'
}

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-server-sdk</artifactId>
  <version>3.0.1</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 firebase = require("firebase");

firebase.initializeApp({ serviceAccount: "path/to/serviceAccountCredentials.json", databaseURL: "https://databaseName.firebaseio.com" });

Java

FirebaseOptions options = new FirebaseOptions.Builder()
  .setServiceAccount(new FileInputStream("path/to/serviceAccountCredentials.json"))
  .setDatabaseUrl("https://databaseName.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, you can pass the service account's individual fields inline if you are using Node.js:

Node.js

var firebase = require("firebase");

firebase.initializeApp({ serviceAccount: { projectId: "projectId", clientEmail: "foo@projectId.iam.gserviceaccount.com", privateKey: "-----BEGIN PRIVATE KEY-----\nkey\n-----END PRIVATE KEY-----\n" }, databaseURL: "https://databaseName.firebaseio.com" });

You are now ready to use the Firebase SDK on your server to implement custom authentication or to read and write data from the realtime database!

Send feedback about...

Need help? Visit our support page.