Firebase Remote Config supports server-side configuration using the Firebase Admin Go SDK v4.17.0+. This capability empowers you to dynamically manage the behavior and configuration of server-side applications using Remote Config.
Unlike Firebase client SDKs, which fetch a client-specific configuration derived from the Remote Config template, the server-side Remote Config SDK fetches a complete Remote Config template from Firebase. Your server can then evaluate the template with each incoming request and use its own logic to serve a customized response with very low latency. You can use conditions to control and customize responses based on random percentages and client attributes defined in custom signals.
With server-side Remote Config, you can:
- Define configuration parameters for applications running on or accessed through your server, allowing for use cases like remotely configuring AI model parameters and prompts and other integrations, to ensure your API keys stay secure.
- Dynamically adjust parameters in response to changes in your environment or other application changes, like updating LLM parameters and model endpoints.
- Control costs by remotely updating the APIs your server calls.
- Generate custom configurations on-the-fly for clients that access your server.
You can deploy server-side Remote Config on Cloud Run, or self-hosted server environments.
Before you begin
Follow the instructions in Add the Firebase Admin SDK to your server to create a Firebase project, set up a service account, and add the Firebase Go SDK to your server.
Step 1: Initialize the Firebase Admin Go SDK and authorize API requests
When you initialize the Admin SDK with no parameters, the SDK uses Google
Application Default
Credentials
and reads options from the GOOGLE_APPLICATION_CREDENTIALS
environment
variable. To initialize the SDK and add Remote Config:
ctx := context.Background()
// Initialize Firebase
app, err := firebase.NewApp(ctx, nil)
if err != nil {
log.Fatalln("Error initializing app:", err)
}
remoteConfig, err := app.RemoteConfig(ctx)
if err != nil {
log.Fatalln("Error initializing remote config client:", err)
}
Step 2: Identify default parameter values for your server application
Identify the variables in your app that you want to dynamically update with Remote Config. Then, consider which variables must be set by default in your application and what their default values should be. This ensures that your application runs successfully even if its connection to the Remote Config backend server is interrupted.
For example, if you are writing a server application that manages a generative AI function, you might set a default model name, prompt preamble, and a generative AI configuration, like the following:
Parameter name | Description | Type | Default value |
---|---|---|---|
model_name |
Model API name | String | gemini-2.0-flash |
preamble_prompt
|
Prompt to prepend to the user's query | String | I'm a
developer who
wants to learn
about Firebase and
you are a helpful
assistant who
knows everything
there is to know
about Firebase! |
generation_config
|
Parameters to send to the model | JSON |
{"stopSequences":
["I hope this
helps"],
"temperature":
0.7,
"maxOutputTokens":
512, "topP": 0.9,
"topK": 30} |
Step 3: Configure your server application
After you've determined the parameters you want to use with Remote Config, configure your application to set default values, fetch the server-specific Remote Config template, and use its values. The following steps describe how to configure your Go application.
Access and load the template.
// Initialize server-side Remote Config defaultConfig := make(map[string]any) templateDataJSON := "" template, err := remoteConfig.InitServerTemplate(defaultConfig, templateDataJSON) if err != nil { log.Fatalln("Error initializing server template:", err) } // Load Remote Config err = template.Load(ctx) if err != nil { log.Fatalln("Error fetching server template:", err) }
Another way you can initialize the template is by using the
GetServerTemplate
function to retrieve the required server template. You can convert this template to a JSON format that can be stored locally. You can subsequently initialize this JSON template using theInitServerTemplate
function.To ensure that your application runs successfully even if its connection to the Remote Config backend server is interrupted, add default values for each parameter to your app. To do this, add a
defaultConfig
inside yourInitServerTemplate
orGetServerTemplate
template function:// Initialize server-side Remote Config defaultConfig := make(map[string]any) defaultConfig["model_name"] = "gemini-2.0-flash" // Assume the type generationConfig has been defined defaultConfig["generation_config"] = &generationConfig{ StopSequences: []string{"I hope this helps"}, Temperature: 0.7, MaxOutputTokens: 512, TopP: 0.9, TopK: 30, } defaultConfig["preamble_prompt"] = "I'm a developer who wants to learn about Firebase and you are a helpful assistant who knows everything there is to know about Firebase!" templateDataJSON := "" template, err := remoteConfig.InitServerTemplate(defaultConfig, templateDataJSON) if err != nil { log.Fatalln("Error initializing server template:", err) } // Load Remote Config err = template.Load(ctx) if err != nil { log.Fatalln("Error fetching server template:", err) }
After the template loads, use
template.Evaluate()
to import parameters and values from the template:// An empty evaluation context means no conditions are applied. evaluationContext := make(map[string]any) config, err := template.Evaluate(evaluationContext) if err != nil { log.Fatalln("Error evaluating template:", err) }
Optionally, if you set conditions in your Remote Config template, define and provide the values you want:
- If using percentage
conditions,
add the
randomizationID
that you want to use to evaluate your condition(s) within thetemplate.Evaluate()
function. - If using custom signals, define the attributes and their values. Custom signals are available with Firebase Admin Go SDK v4.17.0 and higher.
For example, you might set a Firebase installation ID as the
randomizationID
, or a user ID, to ensure that each user that contacts your server is added to the proper randomized group,version
as a custom signal to target specific client versions, andplatform
as a custom signal to target client platform.For more information about conditions, see Condition rule types.
// Add template parameters to `config`. Evaluates the // template and returns the parameter value assigned to // the group assigned to the {randomizationID}, version and platform. evaluationContext := make(map[string]any) evaluationContext["randomizationID"] = "2ac93c28-c459-4760-963d-a3974ec26c04" evaluationContext["version"] = "1.0" evaluationContext["platform"] = "Android" config, err := template.Evaluate(evaluationContext) if err != nil { log.Fatalln("Error evaluating template:", err) }
- If using percentage
conditions,
add the
Next, extract the parameter values you need from the config constant. Use
getters
to cast the values from Remote Config into the expected format. The following types are supported:- Boolean:
GetBoolean
- Integer:
GetInt
- Float:
GetFloat
- String:
GetString
- Value source:
GetValueSource
For example, if you are implementing Vertex AI on your server and want to change the model and model parameters, you might want to configure parameters for
model_name
andgeneration_config
. Here's an example of how you could access Remote Config's values:// Replace defaults with values from Remote Config. // Assume the type generationConfig has been defined var genConfig generationConfig err = json.Unmarshal([]byte(config.GetString("generation_config")), &genConfig) if err != nil { log.Fatalln("Error unmarshaling generation config:", err) } model := config.GetString("model_name") // Parameter created on the console isAIEnabled := config.GetBoolean("is_ai_enabled") // Generates a prompt comprised of the Remote Config // parameter and prepends it to the user prompt (assume it is extracted from the request body) userPrompt := "What is Remote Config?" prompt := fmt.Sprintf("%s %s", config.GetString("preamble_prompt"), userPrompt)
- Boolean:
If your server is long-running, as opposed to a serverless environment, periodically reload the template to confirm that you're fetching the most up-to-date template from the Remote Config server.
Step 4: Set server-specific parameter values in Remote Config
Next, create a server Remote Config template and configure parameters and values to use in your app.
To create a server-specific Remote Config template:
- Open the Firebase console Remote Config parameters page and, from the Client/Server selector, select Server.
- Define Remote Config parameters with the same names and data types as
the parameters that you defined in your app and provide values. These
values will override the
defaultConfig
you set in Configure your server application when you fetch and evaluate the template and assign these values to your variables. - Optionally, set conditions to persistently apply values to a random sample of instances or custom signals you define. For more information about conditions, see Condition rule types.
- When you've finished adding parameters, click Publish changes.
- Review the changes and click Publish changes again.