HTTPS Callable

HTTPS Callable Call functions from your app

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

Sending back the result

to send data back to the client, return data that can be JSON encoded.

To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client.

In client Call the function

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: messageText}).then(function(result) {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;
  // ...
});

How to protect firebase Cloud Function HTTP endpoint to allow only Firebase authenticated users?

export const getData = functions.https.onCall((data, context) => {
  // verify Firebase Auth ID token
  if (!context.auth) {
    // return { message: 'Authentication Required!', code: 401 };
// https://firebase.google.com/docs/functions/callable#handle_errors
  throw new functions.https.HttpsError(
      "failed-precondition",
      "The function must be called " + "while authenticated."
    );
  }

  // do your things..
  const uid = context.auth.uid;
  const query = data.query;

  return { message: 'Some Data', code: 400 };
});
exports.helloWorld = functions.https.onCall((data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError(
      "failed-precondition",
      "The function must be called " + "while authenticated."
    );
  }
  return { text: "Hello World" };
});

https://github.com/firebase/functions-samples

Last updated