Firebase is rolling out a new API in the Admin SDK for setting up and managing Firebase projects. So far this API has landed in the Node.js and Java flavors of the Admin SDK, with more launches expected in the future. Developers can use this API to create apps in a Firebase project, list the existing apps, and update the metadata associated with them. This means developers can now implement backend services, serverless functions and other administrative tools to automate a number of management tasks related to Firebase projects.
To use the new API, you must first import and initialize the Firebase Admin SDK. Then you can create apps using it as shown in listing 1.
Note that no arguments are passed to the
initializeApp() method. This implies the Admin SDK in this example is using Application Default Credentials (ADC). When executed in a managed environment such as Google Cloud Functions, listing 1 creates a new Android app in the same Firebase project as the one used to deploy the code. To execute this example locally, you can set the
GOOGLE_APPLICATION_CREDENTIALS environment variable, and point it to a service account JSON file. In that case the new app is created in the same Firebase project as the service account.
The Admin SDK uses the following 3-stepped protocol to determine the target Firebase project for project management operations:
- Check if the
projectIdapp option has been specified when initializing the SDK.
- Check if a project can be discerned from the credentials used to initialize the SDK. This is possible when the SDK is initialized with service account credentials or ADC.
- Check if the
GOOGLE_CLOUD_PROJECTenvironment variable is set.
If you choose to specify the project ID as an app option (step 1) or as an environment variable (step 3), you should also specify valid credentials that have authorization over the target project. Obviously you cannot make changes to a project that you have no authorization over. If the SDK fails to determine a project ID by following the above steps, the
projectManagement() API throws an error indicating the problem.
Having created some apps, now lets try to query them using the Admin SDK. Listing 2 illustrates how this is accomplished.
The call to
listAndroidApps() returns an array of objects that can be used to further inspect and update individual apps. Alternatively, if the ID of a specific app is already known, you can bypass calling
listAndroidApps(), as shown in listing 3.
In addition to querying app metadata, you can also perform certain update tasks such as adding SHA certificates to Android apps, and changing the display names of apps. The above examples only demonstrate the Android-related methods in the new API, but it has methods for managing IOS apps too. You can check out the full API surface in the documentation.
This Firebase project management API is still a work in progress. More language support, and more features can be expected in the future. In the meantime try out what’s released so far, and engage with the Firebase community with feedback, suggestions, and code contributions. If you have any success stories of how this feature has helped you, I’d love to hear them. If you run into any issues or limitations, I’d like to know about those experiences too. Happy coding with Firebase!