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.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
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:
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.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.