Set up and manage a Firebase project using the Management REST API

The Firebase Management REST API enables programmatic setup and management of Firebase projects, including a project's Firebase resources and Firebase Apps.

This overview describes the general workflow to add Firebase resources and apps to an existing Google Cloud project that doesn't yet use Firebase services.

You can jump to specific sections of this page if you just want to:

Before following any steps on this page, make sure that you enable the API.

For information about access management for the Firebase Management API, visit the Cloud Identity Access Management (IAM) API documentation.

Before you begin

Before you begin, you'll need to enable the Management API for your Google Cloud project and generate your access token.

Enable the Management REST API for your Google Cloud project

If you haven't already, you'll need to enable the Firebase Management API for use with your Google Cloud project.

  1. Open the Firebase Management API page in the Google APIs console.
  2. When prompted, select your Google Cloud project.
  3. Click Enable on the Firebase Management API page.

Generate your API access token

Here's an example for Node.js that retrieves your access token.

First, if you aren't in a Google Cloud environment, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path to your service account key.

Linux or macOS

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-file.json"

Windows

With PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your\service-account-file.json"

Then, use the Firebase Admin SDK to get an access token from your service account credentials:

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

function getAccessToken() {
  return admin.credential.applicationDefault().getAccessToken()
      .then(accessToken => {
        return accessToken.access_token;
      })
      .catch(err => {
        console.error('Unable to get access token');
        console.error(err);
      });
}

Find the resource name of your project

You can find the Google Cloud projects which are available for adding Firebase services.

REQUEST

Call availableProjects.list. The request body for this call must be empty.

Here's an example for Node.js to request a list of available Google Cloud projects:

const fetch = require('node-fetch');

async function listProjects() {
  const accessToken = getAccessToken();
  const uri = 'https://firebase.googleapis.com/v1beta1/availableProjects';
  const options = {
    method: 'GET',
    headers: {
      'Authorization': 'Bearer ' + accessToken,
    },
  };

  try {
    const rawResponse = await fetch(uri, options);
    const resp = await rawResponse.json();
    const projects = resp['projectInfo'];
    console.log('Project total: ' + projects.length);
    console.log('');
    for (let i in projects) {
      const project = projects[i];
      console.log('Project ' + i);
      console.log('ID: ' + project['project']);
      console.log('Display Name: ' + project['displayName']);
      console.log('');
    }
  } catch(err) {
    console.error(err);
  }
}

RESULT

The response body from a call to availableProjects.list contains a list of ProjectInfo objects. If the list of projects is too long, the response body also contains a nextPageToken that you can use as a query parameter to get the next page of projects.

Here's an example response body of an availableProjects.list call:

{
  "projectInfo": [
    {
      "project": "projects/first-cloud-project",
      "displayName": "First Cloud Project"
    },
    {
      "project": "projects/second-cloud-project",
      "displayName": "Second Cloud Project"
    }
  ]
}

This example response has two Google Cloud projects that can have Firebase services added to them. Note that the project field provides the globally unique resource name for a project.

You can use any project value listed in the response from availableProjects.list to add Firebase services or add apps to your project.

In the next section, we'll add Firebase services to First Cloud Project using the projects/first-gcp-project resource name.

Add Firebase services to your project

Google Cloud projects can take advantage of the services offered by Firebase. In this section, you'll learn how to add Firebase services to your existing Google Cloud project programmatically. Note that you can also add Firebase services to your existing Google Cloud project in the Firebase console.

REQUEST

Call projects.addFirebase. The request body for this call must be empty.

Here's an example for Node.js to add Firebase services to your Google Cloud project:

const fetch = require('node-fetch');

async function addFirebase(projectId) {
  const accessToken = getAccessToken();
  const uri = 'https://firebase.googleapis.com/v1beta1/projects/' + projectId + ':addFirebase';
  const options = {
    method: 'POST',
    // Use a manual access token here since explicit user access token is required.
    headers: {
      'Authorization': 'Bearer ' + accessToken,
    },
  };

  try {
    const rawResponse = await fetch(uri, options);
    const resp = await rawResponse.json();
    console.log(resp);
  } catch(err) {
    console.error(err['message']);
  }
}

RESULT

The result of a call to projects.addFirebase is an Operation. Before you can call other Firebase-related endpoints for your project, the operation must be successful.

To check if the operation is successful, you can call operations.get on the operation until the value of done is true and its response is of type FirebaseProject. If the operation fails, its error is set to google.rpc.Status.

Here's the response body of an operations.get call:

{
  "name": "operations/...",
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.firebase.service.v1beta1.FirebaseProject",
    "projectId": "first-cloud-project",
    "projectNumber": "...",
    "displayName": "First Cloud Project",
    "name": "projects/first-cloud-project",
    "resources": {
      "hostingSite": "first-cloud-project",
      "realtimeDatabaseInstance": "first-cloud-project"
    }
  }
}

Since done is true and the response type is a FirebaseProject, the Google Cloud project now has Firebase services. The response also contains other useful information about your newly created FirebaseProject, like its projectNumber and its default resources. The Operation is automatically deleted after completion.

Add Firebase Apps to your project

Many different apps can use a FirebaseProject, including iOS, Android, and web apps. In this section, you'll learn how to add Firebase Apps to your existing FirebaseProject programmatically. Note that you can also add Firebase Apps to your existing Firebase project in the Firebase console.

Select a type of Firebase App to add to your Firebase project.

You can link an existing Google Analytics account to your existing FirebaseProject programmatically. Note that you can also link your existing Firebase project to Google Analytics in the Integrations tab of your Project Settings.

The call to projects.addGoogleAnalytics requires an analytics_resource, which can either be an analyticsAccountId or an analyticsPropertyId:

  • Specify an existing analyticsAccountId to provision a new Google Analytics property within the specified account and associate the new property with your Firebase project.

  • Specify an existing analyticsPropertyId to associate the Google Analytics property with your Firebase project.

You can find both your analyticsAccountId and any existing analyticsPropertyId on the Google Analytics website.

When you call projects.addGoogleAnalytics:

  1. The first check determines if any existing data streams in the Google Analytics property correspond to any existing Firebase Apps in your FirebaseProject (based on the packageName or bundleId associated with the data stream). Then, as applicable, the data streams and apps are linked. Note that this auto-linking only applies to Android Apps and iOS Apps.

  2. If no corresponding data streams are found for your Firebase Apps, new data streams are provisioned in the Google Analytics property for each of your Firebase Apps. Note that a new data stream is always provisioned for a Web App even if it was previously associated with a data stream in your Analytics property.

Learn more about the hierarchy and structure of Google Analytics accounts in the Analytics documentation.

REQUEST

Call projects.addGoogleAnalytics.

In the request body for our example call to project.addGoogleAnalytics, we'll specify our Google Analytics account analyticsAccountId. This call will provision a new Google Analytics property and associate the new property with the FirebaseProject.

{
  "analyticsAccountId": "<your-google-analytics-account-id>"
}

Here's an example for Node.js to link a Firebase project with a Google Analytics account:

const fetch = require('node-fetch');

async function addGoogleAnalytics(projectId, analyticsAccountId) {
  const accessToken = getAccessToken();
  const uri = 'https://firebase.googleapis.com/v1beta1/projects/' + projectId + ':addGoogleAnalytics';
  const options = {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer ' + accessToken,
    },
    body: JSON.stringify({
      'analyticsAccountId': analyticsAccountId
    }),
  };

  try {
    const rawResponse = await fetch(uri, options);
    const resp = await rawResponse.json();
    console.log(resp);
  } catch(err) {
    console.error(err['message']);
  }
}

RESULT

The result of a call to projects.addGoogleAnalytics is an Operation. Before you can call other Firebase-related endpoints for your project, the operation must be successful.

To check if the operation is successful, you can call operations.get on the operation until the value of done is true and the response is of type analyticsDetails. If the operation fails, its error is set to google.rpc.Status.

Here's the response body of an operations.get call:

{
  "name": "operations/...",
  "none": true,
  "response": {
    "@type": "type.googleapis.com/google.firebase.service.v1beta1.AnalyticsDetails",
    "analyticsProperty": [
      {
        "id": "...",
        "displayName": "..."
      }
    ],
    "streamMappings": [
      {
        "app": "...",
        "streamId": "...",
        "measurementId": "..."
      }
    ]
  }
}

Since done is true and the response type is analyticsDetails, the FirebaseProject is now linked to the specified Google Analytics account. The Operation is automatically deleted after completion.