How to create and connect to a firestore database in an express app ?
Tags: webdev, firebase, firestore, database
Published: 2022-03-22 09:31:20

As a web developer myself, I understand how hard it can be sometimes to find a simple guide that can help you do a specific thing you want, that's why I thought about making a simple guide to help newbies work with firebase's firestore database using expressjs.

In this guide I am gonna show you all the necessary steps to create an expressjs app, create and connect to a firestore database, and then store some data for testing.

First of all let's create an express app.

Inside a folder run :

```
npm init && npm install express --save
```
Create a file named index.js to initialize your express app.

Next we need to install the firesbase-admin module inside of our express app:

```
npm install firebase-admin --save
```

The module that we just installed gives us access to some builtin methods that we gonna need to interact with our database, these methods are : initializeApp, cert, and getFirestore, destructure the mentioned methods using :

```
const express = require("express");
const app = express();

const { initializeApp, cert } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
```
Next, we need to create a firebase project in order to access to a firestore realtime database:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/so31fbsv091fojfejwm6.png)

After creating a project named Sample-project, we should next create a web app to connect to firestore.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqjxzfvmvlizij8k1vi0.png)

After creating our webapp, we can then access the project overview to create our firestore database here:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5elml729llzz1t6048n8.png)

Next thing we would need to do is to generate a new service account key to interact with our database, you can download here and name it whatever you want in your project, for my case I named it serviceAccountKey.json.

Here we go to our project settings, then service accounts and click on  generate new key that is featured on the image below :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzi6qhn0zqxeww695ayy.png)

You will get prompted to download your serviceAccountKey, and make sure to do it inside of your project.

Next thing you need to include the key in your express app like this :

```
const serviceAccount = require("./serviceAccountKey.json");
```
in order to initialize your app with admin privileges, you need to initialize your app this way :

```
initializeApp({
  credential: cert(serviceAccount),
  databaseURL: process.env.databaseURL
});
```
You can get your database URL by simply adding your projectID + ".firebaseio.com"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bu4mzdk0e058vh5rm3k.png)

You can get your project ID by simply browsing to your project settings as mentioned in the screenshot above.

And finally, we went through all these steps to reach this last one, the one where we get our firestore instance that let's us communicate with our database from our app.

```
const db = getFirestore();
```

And now it's time to make our first request creating a new collection and inserting some documents into it for test.

```
app.post("/testDB", (req, res) => {

let setDoc = 
db.collection('testCollection').doc('testDoc').set(req.body);

res.send({'Message': 'Success'});

});
```
I hope you enjoyed this guide, I am looking for your opinions about it, and if there is anything to add don't hesitate to comment about it, thank you :).