5

I want to call a cloud function from firebase from another nodejs server or just a nodejs script.

My firebase function is an onCall function.

I am using https://www.npmjs.com/package/firebase-admin for interacting with firebase but it doesn't seem to have a way of calling cloud functions...

Can I do it any other way? Like a http request?

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
exilonX
  • 1,712
  • 3
  • 26
  • 50
  • 2
    Is the cloud function handing HTTP requests? In that case you can call it how you would any API endpoint since it has a url – Dominic May 28 '19 at 14:41
  • I could make it that way but I don't really want to do that! I would prefer calling it through their API or something and not through http – exilonX May 28 '19 at 14:46

3 Answers3

3

If you want to invoke a callable Function from server code, you will have to follow its protocol documentation in order to make sure the request is valid.

It's probably easier just to use a regular HTTP type function. It should be easy to share its logic with a callable function by sharing the common code in another normal JavaScript function.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • The thing that I don't really understand is the security regard of using a functions.https.onRequest((req, res) => {}) it doesn't require any authentication and I can make any firebase query I want? Am I missing something? – exilonX May 29 '19 at 14:58
  • *"I can make any firebase query I want?"* - I don't really understand what you're asking here. Are you having a problem with coding it? – Doug Stevenson May 29 '19 at 15:07
  • No I did it, my problem is that I am doing a unauthenticated request and still the request is returning stuff from firebase! – exilonX May 29 '19 at 15:19
  • If you are using a normal HTTP type trigger, you will have to implement your own form of authorization. – Doug Stevenson May 29 '19 at 15:22
1

In short: You have to trigger http event.
Currently, firebase does support two invoke options.

If you read through documentation here and here, there is always has to be some trigger.
And firebase accept http request.
Even if you use mobile app from example one, under the hood, firebase SDK will make http request.

The other option is scheduled invocations, but as I get from comments, it's not what you're looking for.

Grynets
  • 2,477
  • 1
  • 17
  • 41
1

To call locally runs firebase emulators:start --only function check the function name you want to call at functions list http://localhost:4001/functions.

  await fetch('<HTTP-LOCAL-URL-YOU-PICKED-FROM-THE-LIST>', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({data:{ /* YOUR JSON COMES HERE */ }})
  });

To call remotely

  await fetch('http://us-central1-<MY_PROJECT_NAME>.cloudfunctions.net/<MY_FUNCTION_NAME>', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({data:{ /* YOUR JSON COMES HERE */ }})
  });

It will ping your backend local and remote respectively.

fct
  • 311
  • 3
  • 18