Get firebase function url from admin SDK programatically

Viewed 300

Is there a way to get the Firebase Function url from the Admin SDK for nodejs?

In prod it will look like: https://REGION-PROJECT.cloudfunctions.net/FUNCTION_NAME

And when serving locally: http://localhost:PORT/PROJECT/REGION/FUNCTION_NAME

Is it possible to get this value in nodejs programmatically? E.g. from admin or configuration settings?

2 Answers

The Firebase Admin SDK doesn't expose any APIs that deal with Cloud Functions. From what you've shared, it sounds like you have everything you need to build the URL yourself, which is the right way to go.

If you really must use another piece of software, you'll have to dive very deep into the inner workings of the Firebase CLI and figure out how to reuse its code. You'll probably have to look in two places - one for the emulator, and another for deployment. I suspect this is almost certainly not going to be worth your time, since building the strings yourself is pretty straightforward.

Since you don't say exactly what you want to accomplish, I'll just guess that you want your app to be aware of when you're in development vs. production mode.

What I did was to simply write in some checks in my front- and back-end code. For instance, in the browser/web code:

if (window.location.href.toString().includes('localhost:5000')) {
// exhibit desired behavior for emulator
} else {
// exhibit desired behavior for production
}

Depending on how you're calling your node/backend functions, you could pass along the execution context (emulated or production), and use that in the back, too.

It's not perfect, but it's actually streamlined development for me quite a bit. Hope that helps!

Related