1

Firebase https functions can be called from the client-side as the function call is provided by the client SDKs.

But can they be called from the server-side? I can't find any documentation or questions/answers about that.

Specifically, I'm using Next.js, and I'd like to call the https function during server-side rendering and pass the results via props to the client-side.

artooras
  • 6,315
  • 9
  • 45
  • 78
  • Any of the functions can also be used server side. Which language is your server written in? – Raffael Feb 01 '22 at 17:42
  • Next.js server-side is based on Node.js - so, Javascript. – artooras Feb 01 '22 at 17:47
  • My mistake sorry, but all the functions can also be called from a server. – Raffael Feb 01 '22 at 17:48
  • Yes, but how? That's my question. Which SDK should be used? Because (I assume) I cannot use the client-side JavaScript SDK on the server-side with all the initilizations and whatnot, can I? Or perhaps there's a way to use a raw `fetch` request with auth headers, etc.? Perhaps there are examples? – artooras Feb 01 '22 at 17:51

3 Answers3

1

You cannot call HTTPS Cloud functions using the admin SDK. Referencing previous threads, you would be better off building an HTTP function that you could call from your backend with an HTTP client. This is due to the problems in implementing the authentication required for HTTPS callable functions from the backend. This is what Maciek Sawicki mentioned.

Something else worth mentioning is that for other actions (like using Firestore or other Firebase services), there is the Firebase Admin SDK. The Admin SDK is meant to be used from the backend, and has different authentication requirements. Here is the reference if you would like to review what services you can use.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
  • Thanks Ernesto. I'm already using firebase admin SKD. I was just wondering if there's built-in functionality to call https functions as well... – artooras Feb 02 '22 at 08:06
  • 1
    @artooras I checked the [`firebase-admin`](https://firebase.google.com/docs/reference/admin) reference and could not indeed find any wrapper for calling HTTPS functions. This other [thread](https://stackoverflow.com/questions/65060445/) also confirms there is no built-in functionality. It seems that building regular HTTP functions is a convenient alternative. – ErnestoC Feb 02 '22 at 22:28
0

Generic approach is describe here

For your SSR in Next.js use case you need some js http client like axios.

Maciek Sawicki
  • 6,717
  • 9
  • 34
  • 48
  • I also recommend looking at `gaxios` or `node-fetch` as both of these are used by the Firebase SDKs already. – samthecodingman Feb 01 '22 at 22:34
  • Thanks Maciek. I have implemented the http call using plain fetch though. On another note, am I right in understanding that the https function is publicly accessible, and it is up to me to secure access to it? For instance, passing some proprietary header that I can verify on the cloud function and reject if it's incorrect or not there at all? – artooras Feb 04 '22 at 08:45
0

I think this example should cover most cases:

const token = await admin.credential.applicationDefault().getAccessToken();
const appCheckToken = await admin.appCheck().createToken(admin.app()?.options?.projectId as string);
const selfPolizeEmissionResponse = await axios.post(
  'https://us-central1-{yourProjectId}.cloudfunctions.net/{yourCallableName}',
  {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'X-Firebase-AppCheck': appCheckToken,
    },
    body: JSON.stringify({
      data: {
        // Your Data to be use by callable!
      },
    }),
  }
);
xtealer
  • 123
  • 1
  • 8