Call Functions From an Application

Learn to call functions from an application.

We’ve already discussed how to trigger functions directly and how to trigger functions via HTTP requests. Now, let’s focus on how to trigger functions directly from our application.

The Cloud Functions SDK allows us to call functions directly from our application. These types of functions are called HTTPS Callable functions. To trigger these functions, we must first write and deploy the function, then add client logic that allows us to trigger the functions from our application.

Note: HTTPS Callable functions are similar but not identical to typical HTTP functions. A major difference between them is that for Callable functions, we must use the Cloud Functions for Firebase client SDK alongside the functions.https backend API. Therefore, unlike HTTP trigger functions, they can’t be triggered directly from the browser.

Write an HTTPS Callable function

To create an HTTPS Callable function, we must use the onCall handler that exists on the functions.https object. This method takes a callback function with two parameters, data and context, that fires when the Cloud Function is invoked. The data object represents any data we send to this Cloud Function from the frontend when it triggers. The context object is an optional parameter that holds the current user’s authentication information.

To terminate an HTTP Callable function, we must return a response to the client by using the JavaScript return statement to end function execution:

Get hands-on with 1400+ tech skills courses.