Serverless REST APIs made simple with Cloud functions


Serverless is one of the top keyword in the integration space right now. Everyone is talking about the serverless just to make sure that they are already in the flow.

What is Serverless?

Before we go deeper on how to write serverless APIs with Cloud function, I would like to give a quick introduction about serverless. The following is the given explanation from the wiki,

Serverless computing is a cloud-computing execution model in which 
the cloud provider runs the server, and dynamically manages 
the allocation of machine resources.

So, the server will be maintained by the cloud providers. Developers or business owners should write only the specific logic as a function, then deploy it into the cloud function infrastructure. So that the scalability and performance optimization will be handled by the cloud providers.

Traditional deployment

Mostly these kinds of deployment follow the monolith way of developing application and deploy everything in a VM or in a cloud. This will have some limitations with respect to scalability and better resource utilization. Moreover, this will also have some limitations on the heaviness of the deployment.

Micro deployment

This is mainly talking about container-based deployment. In recent times, everyone is talking about Docker and Kubernetes way of deploying applications. They will have a bunch of microservices and all of these services will work together to meet the customer use-cases. Each microservices will have different deployment lifecycles as well as a development strategy. Even though microservices bring lots of benefits, it also has its own challenges to manage.

Serverless deployment

This deployment model is one level down of microservice deployment. Here we are talking about each tiny functionalities can be deployed seamlessly into the cloud and serve better. This is also referred with the terms of function-as-a-service. There are lots of cloud vendors are supporting this way of deployment namely AWS Lambda, Google Cloud Functions, IBM Cloud Functions, Microsoft Azure Functions, Oracle Fn and Project Riff.

In this article, I am going to walk you through on how to develop a serverless REST APIs with Google Cloud Functions. I believe you have already gone through my previous article on setting up Cloud functions infrastructure. If not, I would recommend you to go through them first and then continue with this one.


  • Firebase project
  • Firestore enabled for the project
  • Cloud function setup is ready locally

Firestore data structure

Since our datastore is Firestore, we will directly create some test data into Firestore collection as below,

In the above example, the collection name is countries. In which we have 3 documents store namely, aus, deu, and ind. Each document is defined with 4 properties (capital, code, currency, name) according to the country code. This is a simple data structure for this use-case.

Cloud functions


const functions = require('firebase-functions');

const admin = require('firebase-admin');
const firestore = admin.firestore(); //firestore reference

const express = require('express');
const cors = require('cors');

const app = express();
app.use(cors({ origin: true })); // Automatically allow cross-origin requests

app.get('/countries', (req, res) => {
  const countriesRef = firestore.collection('countries');
    .then(snapshot => {
      if (snapshot.empty) {
        res.send("Countries are not available");

      var countries = [];
      snapshot.forEach(doc => {

      return null;
    .catch(err => {
      throw err;
});'/countries', (req, res) => {
  res.send("/countries POST function called");

app.get('/countries/:id', (req, res) => {
  res.send("/countries/:id GET function called with id " +;

exports.apis = functions.https.onRequest(app);

I have created 3 HTTP methods and finally which is attached with base path of /apis

Now you need to execute $ firebase deploy –only functions to deploy your functions. Once everything goes fine, you should be able to access your APIs with the following path.

GET https://us-central1-<projectid>
POST https://us-central1-<projectid>
GET https://us-central1-<projectid>{id}

NOTE: <projectid> should be replaced with your real project id reference.

Now you should be able to access the above REST endpoints through any HTTP client.

//NOTE: Replace the projectid with yours.

    "name": "Australia",
    "code": "AUS",
    "currency": "DOLLAR (AUD)",
    "capital": "Canberra"
    "capital": "Berlin",
    "name": "Germany",
    "code": "deu",
    "currency": "EURO (EUR)"
    "name": "India",
    "code": "IND",
    "currency": "RUPEE (INR)",
    "capital": "New Delhi"

I believe you have got a fair idea on how to write Serverless REST APIs with Firebase Cloud functions. The above examples are not offering any complex functionalities. But this is done with the intention of explaining the concept easily. Keep watching this space for more detailed examples/use-cases in the future

I hope this has helped you to get the basics of serverless APIs with cloud functions. Please share it with your friends if you feel this helped you.

Happy coding!

API Evangelist | Chief Hustler | Habit Coach | Madras Java User Group Leader

Latest articles

Flutter: How does column layout work?

Layouts are the fundamental starting points for developing Flutter applications. There are many layout options are available by default. I am going...

Flutter: Stateless vs Stateful widgets

Flutter is a mobile application development framework developed by Google. You need to understand the fundamental building blocks before you dig deeper...

Why Flutter is a great technology for makers &...

As per the wikipedia, Flutter is an open source UI development kit created by Google. It is used to...

Why 12-Factory methodology matters to your Microservice development?

The 12-Factor application development methodology and why is it important for your microservices strategy

When to use Firebase Cloud functions?

Recently I got a question from one of the Indie makers that, when to use Firebase Cloud Functions? This...

Similar articles


Please enter your comment!
Please enter your name here