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 to talk about how to create a Serverless API using Firebase Cloud Functions. When it’s come to the Firebase Cloud Functions is a product of the Google Cloud Platform team and Firebase team.
So let’s begin.
- First, make sure you have a 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.
Create a new directory for your application. Then open it using VS code and type the following command in the terminal.
Ok. Then you need have to install firebase in your application. Type following command to install Firebase tools globally. After installed Firebase tools, you need to log in to Firebase.
npm install -g firebase-tools firebase login
Now let’s work with Firebase. Type the following command to initialize Firebase in your application.
Select Firebase Functions and hit enter. Then it will ask to create a new firebase project or use an existing project. I already create the Firebase project using the console, so I will select the existing project.
Firebase initialization complete!. Now let’s go to the coding.
In your file explorer open the 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.
If the firebase deploy command does not work you can type the 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 the following output.
And also you can check your API without deploying it 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 the following command to start the Firebase Emulator Suite.
npm run serve
This command will open the Firebase Emulator Suite Dashboard. Under the Functions emulator section, you can check your API.
Whether you are you student or a professional Firebase very helpful in your projects. In some of my personal projects, I used Firebase. You can use Firebase Functions to connect with Cloud Firestore or Realtime Database. Creating APIs with Firebase Functions will reduce the code and it will easy to implement the application.