'Firebase: How to run 'HTTPS callable functions' locally using Cloud Functions shell?

I couldn't find a solution for this use case in Firebase official guides.

  • They are HTTPS callable functions
  • Want to run Functions locally using Cloud Functions shell to test
  • Functions save received data to Firestore
  • The 'auth' context information is also needed

My code as below. Thanks in advance.


Function :

exports.myFunction = functions.https.onCall((data, context) => {
  const id = context.auth.uid;
  const message = data.message;

  admin.firestore()...
  // Do something with Firestore //
});

Client call :

const message = { message: 'Hello.' };

firebase.functions().httpsCallable('myFunction')(message)
  .then(result => {
    // Do something //
  })
  .catch(error => {
    // Error handler //
  });


Solution 1:[1]

There is an api exactly for this use case, see here.

I used it in javascript(Client side) as follows -

button.addEventListener('click',()=>{
//use locally deployed function
firebase.functions().useFunctionsEmulator('http://localhost:5001');
//get function reference
const sayHello = firebase.functions().httpsCallable('sayHello');
sayHello().then(result=>{
    console.log(result.data);
}) 
})

where sayHello() is the callable firebase function.

When the client is an android emulator/device. Use 10.0.2.2 in place of localhost.

Also the code for flutter would be like so -

CloudFunctions.instance.useFunctionsEmulator(origin: 'http://10.0.2.2:5000')
    .getHttpsCallable(functionName: 'sayHello')

Solution 2:[2]

Cloud functions have emulators for that. Check this link it can suite your case. Its not functions shell, but for testing purposes i think it can still works for you

Solution 3:[3]

In newer versions of firebase, this is the way:

import firebaseApp from './firebaseConfig';
import { getFunctions, httpsCallable, connectFunctionsEmulator } from 'firebase/functions';

const functions = getFunctions(firebaseApp);

export async function post(funcName, params) {
    connectFunctionsEmulator(functions, 'localhost', '5001'); // DEBUG
    let func = httpsCallable(functions, funcName);
    let result = await func(params);
    return result.data;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dominik Šimoník
Solution 3 amiregelz