/firebase-tutorials

How to write HTTPS callable function in Firebase?

Create and deploy HTTPS callable functions in Firebase with this step-by-step guide. Learn project setup, coding, testing, and error handling for secure client-server interactions.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to write HTTPS callable function in Firebase?

 

Step 1: Set up Firebase Project

 

To begin with writing an HTTPS callable function in Firebase, you first need to set up a Firebase project:

  1. Go to the Firebase Console.
  2. Click on "Add Project" and follow the instructions to create a new project or select an existing project.
  3. Follow the setup instructions to add Firebase to your web, iOS, or Android app if you haven't already.

 

Step 2: Initialize Firebase Functions

 

Make sure you have Node.js and npm installed. Then install the Firebase CLI globally if it's not already installed:


npm install -g firebase-tools

Initialize Firebase Functions in your project directory:


firebase init functions

Select the Firebase project you created in the first step when prompted. Use ESLint for code linting if desired, and choose to install dependencies locally.

 

Step 3: Write the HTTPS Callable Function

 

Navigate to the functions directory within your project and open index.js or index.ts if you're using TypeScript. Here’s a basic example of a callable function:


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sayHello = functions.https.onCall((data, context) => {
  // Use context for authentication information
  const name = data.name;
  return {
    message: `Hello, ${name}!`
  };
});

This function will accept a name parameter and return a personalized greeting.

 

Step 4: Deploy the Function

 

Deploy your functions to Firebase:


firebase deploy --only functions

This command deploys your function to Firebase, making it available for use.

 

Step 5: Call the Function from Your App

 

To call the function from your app, you will need to use Firebase's client SDK. Here’s how you can call the sayHello function from a web application:

First, ensure you've included Firebase in your project and initialized it correctly. Then use the following code:


const sayHello = firebase.functions().httpsCallable('sayHello');

sayHello({ name: 'John' })
  .then(result => {
    const message = result.data.message;
    console.log(message);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Replace 'John' with the desired name. This will call your Cloud Function and log the received message.

 

Step 6: Handle Errors (Optional)

 

Ensure that you handle errors properly in your function. Modify the function as necessary to throw proper errors:


exports.sayHello = functions.https.onCall((data, context) => {
  if (!data.name) {
    throw new functions.https.HttpsError('invalid-argument', 'The function must be called with one argument "name".');
  }
  
  const name = data.name;
  return {
    message: `Hello, ${name}!`
  };
});

This adjustment throws an error if no name is provided, ensuring better error handling.

 

Conclusion

 

By following these steps, you've created and deployed an HTTPS callable function using Firebase. Callable functions are useful for client-server interactions, enabling securely managed operations with Firebase Authentication if needed. Make sure to set up Firebase Authentication if your application requires user identity verification before function execution.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022