Hosting a REST API with a Cloud Firestore backend

Hosting a REST API with a Cloud Firestore backend

15 April 2021

Have you ever thought about building a REST API? A serverless Rest API with a serverless compute and a serverless database. For this we have various options available from Microsoft, Amazon, and Google. Here in this article we will be talking about how to build and deploy API on Google Cloud Platform. We will be using Firestore as our backend here.

So, the first step should be to go ahead and create a Project on Google Cloud Platform. After creating a project the things that we should note down are project name, project ID and project number. And then we would need to enable the Firestore database for this project. After enabling Firestore we would need to create a collection in the Firestore database.

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_01

Like in the above picture you can see, We have created a ‘dogs’ collection and in that collection we have added a document with the different fields. A collection in Firestore is pretty much a Table. And a table can have multiple rows of data which can be represented by the documents. Each row of table can have multiple columns and those columns can be represented by the fields in the documents. So here in the above image we have one table called ‘dogs’ and in that table we have some columns namely ‘lifeExpectancy’, ‘name’, ‘origin’ and ‘type’ which have respective values to them.

Now, we need to put a Rest API to handle this database. For creating API we will be using VS Code as IDE and will be using Node JS for writing API related code. Now go ahead and create a fresh Node application in VS Code. For our requirement we will need to add two dependencies to our application. We will update the package.json file and in the dependencies section we will add the firestore client library and express library. For your reference please check the image below.

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_02

The Express is going to serve as our HTTP server that is going to respond to our get/post or any other requests. Firestore Client library is going to help us in communicating with the Firestore database.

One more thing to notice is that we have added a scripts block and in that block we have added “start”: “node index.js”. This will tell the thing running our container to execute this container by executing node with the index.js as the parameter.

For index.js we can have something like this as our starting point. Here till now not much is happening, we are just starting our server and it is listening to port 8080. And we will know that as we have added a console log at line number 9 and it will let us know the port when the app is up and running. After that we have added a get request to the root which will respond with a json object with the status message which we have provided.

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_03

To test this out we will run npm start command in the terminal window and it should run and print the log message with the port number as 8080. Now to test our get request we can test it by two ways, either we can go to a browser and try hitting localhost:8080 or we can open another terminal window in our VS code and execute command curl localhost:8080. It should provide the following output.

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_04

But till now nothing is happening with the Firestore but on the line number 2 of our index.js we have Firestore library to our class. And on line number 4 we have created an instance of the database. Point to note here is that we don’t need to provide any project related details here, it will automatically use the application’s default credentials to access the Firestore associated with our project.

For accessing data from the database we can add another get method with a code snippet similar to the below image. Here we have created a parameterised get request which will check in the current database and match the name with the provided parameter and provide appropriate results.

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_05

We can also add data to the database by using the set method call as shown on line number 39. In the second block we have created a post request which receives the required parameters and adds them to the database.

Now we should deploy our project onto the google cloud platform. To deploy the project we need two scripts, one, a docker file that tells the compute what to do with the updated code. And second, we can write a script to deploy the project on the Google Cloud Platform.

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_06

In the above script, you can see, it is as simple as providing a command for installing the npm to production and copy all the files to the working directory and start the index.js (remember, in the package.json we have added the start in the scripts block, that tells the npm to start index.js).

Hosting_a_REST_API_with_a_Cloud_Firestore_backend_07

In the above image you can see, we have provided the google project id. There are two commands running here, first one at line number 3 builds the project and second line at line number 6 it will deploy the built project on to the google cloud platform as per the provided parameters in the command. One of the important things to be aware of is that the value for the region argument should be checked. As all the regions in Google Cloud platform do not support serverless compute. So we need to check that otherwise the script will throw an error message.

To run this deploy on mac os or linux, we can run sudo bash ./deploy.sh command on terminal or use a similar command on windows to deploy our project on Google Cloud Platform. But before that you may need to set up gcloud on your device so that you can run the gcloud command from the terminal. I will encourage you to check the official documentation for the same.

Also, you can check the code used for this blog here. This blog is inspired by the video provided by Google’s team here on YouTube. I will encourage you to check them out.

search
Blog Categories
Request a quote