Manage your Remote Config templates from the Admin Node.js SDK

Firebase Remote Config allows developers to control the appearance and the behaviour of their apps without having to publish an app update. You can build your app with in-app default configurations and later change those configurations by updating your project’s Remote Config template.

We are happy to announce that you can now manage your Remote Config templates directly from the Admin Node.js SDK.

Typically, you would use the Firebase console to specify and update Remote Config templates manually. However, sometimes you need more automation and server-side control of these settings, such as scheduling Remote Config updates or importing configurations from your own proprietary systems and other Firebase projects. Until now the Remote Config REST API was the only way to implement such use cases, which at times could be cumbersome.

As of version 8.13.0, the Admin SDK introduced a Remote Config API that simplifies the process of creating, accessing, and updating your Remote Config templates from your own backend servers and also from managed Cloud environments like Google Cloud Functions.

Let’s go through some examples on how to use the Firebase Admin Node.js SDK to manage your projects’ templates. You can start by creating a fresh Remote Config template for your project, or you can get the current active template and make changes to it. Here’s how you can get the current active template in Node.js:

const admin = require('firebase-admin');
admin.initializeApp();

const template = await admin.remoteConfig().getTemplate();
console.log('ETag from server: ' + template.etag); 

Each Remote Config template version is associated with an entity tag (ETag) for version control purposes. The Remote Config API uses this ETag to prevent race conditions and concurrent updates to resources. To learn more about ETags, see ETag - HTTP.

Once you have obtained your current active Remote Config template you can start making modifications. Here are some of the operations you can perform using the Admin Node.js SDK:

  • Add new or update existing Remote Config Conditions
  • Add new or update existing Remote Config Parameters
  • Manage Remote Config Parameter Groups
  • Validate your modified Remote Config template
  • Publish your modified Remote Config template as the current active version

Let’s go through an example on adding a new Remote Config condition and a parameter to your template.

// add a new condition
template.conditions.push({
  name: 'android_en',
  expression: "device.os == 'android' "
    + "&& device.country in ['us','uk']",
  tagColor: 'BLUE' as admin.remoteConfig.TagColor,
});

// add a new parameter, that references the above condition
template.parameters['header_text'] = {
  defaultValue: {
    value: 'A Gryffindor must be brave, talented and helpful.'
  },
  conditionalValues: {
    'android_en': {
      value: 'A Droid must be brave, talented and helpful.'
    },
  },
}; 

The above example creates a new condition named android_en and uses it in a conditional value of the parameter named header_text. Checkout the documentation to learn more about conditional expressions. Keep in mind that the conditions in a template are listed in descending order by priority. A parameter might have several conditional values associated with it. If multiple conditions evaluate to true, the first condition in the list takes precedence.

Once you make updates to your template you must publish it to make the new changes effective. After you publish a Remote Config template using the Firebase Admin Node.js SDK, that template becomes the current active version of your project. Before you publish a template you can always validate it to check for any errors.

try {
  const validatedTemplate = admin.remoteConfig()
    .validateTemplate(template);
  const publishedTemplate = admin.remoteConfig()
    .publishTemplate(validatedTemplate);
} catch(err) {
  console.log('Error publishing the template:', error);
} 

For more details and code samples on programmatically modifying Remote Config templates, check out the detailed documentation.

In addition to the API for modifying Remote Config templates, the Firebase Admin Node.js SDK provides an API to manage Remote Config template versions. This API supports the following operations.

  • List all stored versions of the Remote Config template
  • Retrieve a specific version of the Remote Config template
  • Roll back to a specific stored version of the Remote Config template

The listVersions operation allows you to retrieve a list of metadata for all stored versions of your project, sorted in reverse chronological order. Note that the last 300 versions are stored. All versions that correspond to non-active Remote Config templates (that is, all except the template that is being fetched by clients) are also deleted if they are more than 90 days old. Here’s a sample code to fetch a list of template versions.

// list all the Remote Config template Versions
// Only the last 300 versions are stored.
const listVersionsResult = admin.remoteConfig().listVersions();
listVersionsResult.versions.forEach((version) => {
  console.log('version', JSON.stringify(version));
}); 

For a complete list of available operations and examples, check out the documentation on programmatically managing Remote Config template versions.

We hope this new addition of the Remote Config API in the Admin Node.js SDK helps with your backend development tasks. Stay tuned for the Remote Config API to be added across other Firebase Admin SDK platforms. We are excited to see how you use the new APIs in your projects! Hop over to our Github Repo to check out the implementation. Help us further improve our SDKs by providing your valuable feedback, reporting issues, and contributions to our codebase.