Serverless API with Firebase

Pium Sudhara
4 min readJun 19, 2021
Image Source firebase.google.com

In simple terms Serverless architecture allows you to deploy and run your application without having manage servers. So you don’t need to worrying about managing the infrastructure, but still in servers. Reduce cost, scalable, quick deployment these are the few advantages of Serverless. AWS Lambda, Azure Functions, Google Cloud Functions are the famous serverless providers.

Hello everyone, so today I am going talked about how to create an Serverless API using Firebase Cloud Functions. When it’s come to the Firebase Cloud Functions is a product of Google Cloud Platform team and Firebase team.

Whether your code written in JavaScript or TypeScript it doesn't matter. Your code is stored in manage environment by Google Cloud without manage or scale servers. So if you like to know more about Firebase Functions Click Here.

So let’s begin.

Image Source Firebase GIFs — Get the best GIF on GIPHY

Prerequisites

  • First make sure you have an Firebase Account ;). If you don’t have you can sign up using your Google Account. Click Here to go to the Firebase Console. Make sure you have enabled Billing.
  • Second you have to install Node.
  • Postman for check out the API.

and I’m going to use Visual Studio Code as my IDE. You can use your preferred IDE.

So Let’s get started.

Image source Amphibian Abstracts: Make Memes via API Calls

Step 1

Create new directory for your application. Then open it using VS code and type following command in the terminal.

npm init

Ok. Then you need have install firebase in your application. Type following command to install Firebase tools globally. After installed Firebase tools you need login to Firebase.

npm install -g firebase-tools
firebase login

Step 2

Now let’s work with Firebase. Type following command to initialize Firebase in your application.

firebase init

Select Firebase Functions and hit enter. Then it will ask to create new firebase project or use exiting project. I already create Firebase project using console, so I will select existing project.

Next select JavaScript for the language and set No for the ESLint. And finally install dependencies set Yes.

Firebase initialization complete!. Now let’s go to the coding.

Step 3

In your file explorer open index.js file inside the functions folder.

In the index.js type the following code.

Ok now our coding part is done. It’s time to deployment part. Type following command to deploy cloud functions.

firebase deploy

If firebase deploy command does not work you can type following command.

firebase deploy --only functions "app"

Now our cloud functions successfully deployed into Firebase. Now it’s time to check the API. Open postman and type your function URL and hit enter. (My function URL is “https://us-cenfireapi21.cloudfunctions.net/app”.)

“Hello from Google Cloud Platform”…yes our API was successfully deployed and working. If you change your route into /author it will give following output.

And also you can check your API without deploying into the Firebase. You can use Firebase Emulator Suite. Check my article about Firebase Emulator Suite through this link. Firebase Emulator Suite. Advanced tools for developers build and… | by Pium Sudhara | Medium

Enter following command to start the Firebase Emulator Suite.

npm run serve

This command will open the Firebase Emulator Suite Dashboard. Under Functions emulator section you can check your API.

Image source Meme Creator — Funny Booom We’re Done here Meme Generator at MemeCreator.org!

Conclusions

Whether you are you student or profession Firebase very helpful in your projects. In some of my personal project I used Firebase. You can use Firebase Functions to connect with Cloud Firestore of Realtime Database. Creating API’s with Firebase Functions will reduce the code and it will easy to implement the application.

Finally

I hope this article will help you to know about of Firebase Cloud Functions. In next articles let’s talk about how to work with Firebase. If you need any help follow me on GitHub, LinkedIn or Twitter. Follow me for interesting articles.

That’s it, thank you all..! don’t forget to leave a 👏✌️ Use Firebase🔥 Happy Coding✌️❤️ and STAY SAFE !!!

Image source thank you meme — 10+ thank you meme (tumblr.com)

--

--

Pium Sudhara

Cloud Operations Engineer at LSEG☁️💻 | AWS Community Builder | pium.karunasena@gmail.com