How to Install Firebase Admin SDK on Your Own Server

How to Install Firebase Admin SDK on Your Own Server

The Firebase Admin SDK is a library that can allow you to interact with Firebase services in the backend. With Firebase you can authenticate users, store data in a NoSQL database, run A/B tests and so much more. Because of this, it is used for several kinds of projects, such as Discord Bots or websites. One reason Firebase is so special is because of the easy real-time synchronization of all data. So if the data is changed in the database, all apps listening to the database will know. You can use this to make websites that provide live update with data about your app or product, or an easy web dashboard to help your users configure your service. This tutorial features how to Install Firebase Admin SDK the easy way for 2 languages: Javascript & Java.

Firebase Admin SDK is typically used on the front end, to log users in websites & add realtime database listening. It is interfaced with using Javascript and by adding a quick script in your project HTML. But sometimes, the client-side just won’t do. 

install firebase admin sdk

Firebase Admin SDK is useful when you need to tackle business logistics or secure work that can’t be done on the client side. It is also great if you prefer to keep your processing in the cloud. While Firebase cloud functions (serverless functions called on-demand) are meant to solve these issues, for some workloads or requirements, sometimes the best tool for the job is the good old Admin SDK. An example of this would be work that’s needed to be run on an interval in rapid succession or heavy persistent cloud work. 

Prerequisites:

  1. An already existing or previously set up basic node.js file and/or java project
  2. Know how to import dependencies with Maven, Gradle or NPM

Install Firebase Admin SDK with Javascript or Node

We will first install Firebase Admin SDK with Javascript/Node. While the official firebase docs recommend you use environment variables, we personally prefer explicitly passing the path in your code, which will transfer the code between servers & machines easier.

As listed in the official documentation, begin by downloading your private key JSON file from the Firebase Console. 

To download a private key file for your service account:

  1. In the Firebase console, open Settings > Service Accounts.
  2. Click Generate New Private Key, then confirm by clicking Generate Key.
  3. Download and securely store the JSON file containing the key in the same directory as your main node file. 
Install firebase admin sdk with javascript and nodejs

First, navigate to the directory with your existing node js file.

Second, install the Firebase Admin SDK npm module by running “npm install firebase-admin –save”.

  1. After you’ve installed the package, initialize the SDK at the top of your node file by adding the line “var admin = require(“firebase-admin”);”
  2. Add a reference to your service account private key file by adding this line “var serviceAccount = require(“./firebaselogin.json”);” (Replacing firebaselogin.json with the name of the private key file.)
  3. Initialize the app by adding the following code (replacing ‘yourproject’ with your project ID)

admin.initializeApp({

  credential: admin.credential.cert(serviceAccount),

  databaseURL: “https://yourproject.firebaseio.com”

});

Install Firebase Admin SDK with Java

Now, let’s install Firebase Admin SDK with Java. First, we need to retrieve all the dependencies you need. 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 (More detail here: https://firebase.google.com/docs/admin/setup). 

If you want to try to use the Admin SDK without gradle or maven, you’ll have a tough time as the Admin SDK maven relies on tons of dependencies that you have to include in your code.

Now as talked about in the node tutorial you need to download your private key from the Firebase console.

To download a private key file for your service account:

  1. In the Firebase console, open Settings > Service Accounts.
  2. Click Generate New Private Key, then confirm by clicking Generate Key.
  3. Download and securely store the JSON file containing the key in the same directory as your main node file. 
Firebase admin sdk with java

Afterward, put this file in the directory where your JAR will be run.

Now onto your code.

  1. First, create a file input stream to your JSON file by adding this line: “FileInputStream serviceAccount = new FileInputStream(“./serviceAccountKey.json”);” (Replacing serviceAccountKey.json with the name of the private key file.)
  2. Second, create a FirebaseOptions object and link to your input stream with the following ( (replacing yourproject with your project ID):

FirebaseOptions options  new FirebaseOptions.Builder()

           .setCredentials(GoogleCredentials.fromStream(serviceAccount))

           .setDatabaseUrl(“https://yourproject.firebaseio.com”)

           .build();

Initialize firebase with the options you’ve created by adding the final line “FirebaseApp.initializeApp(options);”

BAM! It’s that simple. In just a few lines of code, you’ve got access to an amazing database for user management system & more, all in your own server!

Remember, depending on your workload, your script could be hardware intensive. So if you want max performance for your script, make sure to try out SkySilk’s powerful virtual servers, with best in class pricing and performance. Deploy a Node server in seconds with our one-click template.


(Visited 57 times, 1 visits today)

Share this post with your friends

Share on facebook
Share on twitter
Share on linkedin