Can Firebase RemoteConfig be accessed from cloud functions
Asked Answered
B

2

9

I'm using Firebase as a simple game-server and have some settings that are relevant for both client and backend and would like to keep them in RemoteConfig for consistency, but not sure if I can access it from my cloud functions in a simple way (I don't consider going through the REST interface a "simple" way)

As far as I can tell there is no mention of it in the docs, so I guess it's not possible, but does anyone know for sure?

Berthaberthe answered 29/4, 2017 at 10:58 Comment(2)
Did you made a Function to access RemoteConfig using the REST interface you can share with us?Excommunicative
No, sorry. I decided it wasn't worth the effort for a few variables so decided to just replicate them instead.Berthaberthe
C
8

firebaser here

There is a public REST API that allows you to read and set Firebase Remote Config conditions. This API requires that you have full administrative access to the Firebase project, so must only be used on a trusted environment (such as your development machine, a server you control or Cloud Functions).

There is no public API to get Firebase Remote Config settings from a client environment at the moment. Sorry I don't have better news.

Clupeid answered 29/4, 2017 at 14:6 Comment(4)
The REST API that was introduced recently should be accessible from Cloud Functions, right? What would also be interesting: Do you need the Blaze plan to access that API?Andy
Good point on the new REST API. I updated my answer. This REST API is available in projects on any plan. If you're having problems, post a question with the minimal steps that reproduce where you are stuck.Clupeid
@FrankvanPuffelen This answer is over 4 years old, so I thought I'd just poke in case something has changed.Necropsy
Not that I know of, but if it exists it'd be mentioned here: firebase.google.com/docs/remote-configClupeid
E
4

This is probably only included in newer versions of firebase (8th or 9th and above if I'm not mistaken).

// We first need to import remoteConfig function.
import { remoteConfig } from firebase-admin

// Then in your cloud function we use it to fetch our remote config values.
const remoteConfigTemplate = await remoteConfig().getTemplate().catch(e => {
 // Your error handling if fetching fails... 
}

// Next it is just matter of extracting the values, which is kinda convoluted, 
// let's say you want to extract `game_version` field from remote config:
const gameVersion = remoteConfigTemplate.parameters.game_version.defaultValue.value

So parameters are always followed by the name of the field that you defined in Firebase console's remote config, in this example game_version. It's a mouthful (or typeful) but that's how you get it.

Also note that if value is stored as JSON string, you will need to parse it before usage, commonly: JSON.parse(gameVersion).

Similar process is outlined in Firebase docs.

Escamilla answered 9/9, 2022 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.