Go to console

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 Platform (GCP) project that does not currently 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 GCP project and generate your access token.

Enable the Management REST API for your GCP project

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

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

Generate your API access token

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

const {google} = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/cloud-platform'];

function getAccessToken() {
  return new Promise(function(resolve, reject) {
    const key = require('./service-account.json');
    const jwtClient = new google.auth.JWT(
        key.client_email,
        null,
        key.private_key,
        SCOPES,
        null
    );
    jwtClient.authorize(function(err, tokens) {
      if (err) {
        reject(err);
        return;
      }
      resolve(tokens.access_token);
    });
  });
}

Find the resource name of your project

You can find the GCP 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 GCP projects:

const rp = require('request-promise');

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

  try {
    const resp = await rp(options);
    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-gcp-project",
      "displayName": "First GCP Project"
    },
    {
      "project": "projects/second-gcp-project",
      "displayName": "Second GCP Project"
    }
  ]
}

This example response has two GCP 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 GCP Project using the projects/first-gcp-project resource name.

Add Firebase services to your project

Google Cloud Platform (GCP) projects can take advantage of the services offered by Firebase. In this section, you'll learn how to add Firebase services to your existing GCP project programmatically. Note that you can also add Firebase services to your existing GCP 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 GCP project:

async function addFirebase(projectId) {
  const options = {
    uri: 'https://firebase.googleapis.com/v1beta1/' + projectId + ':addFirebase',
    method: 'POST',
    // Use a manual access token here since explicit user access token is required.
    // Service accounts cannot call `projects.addFirebase`.
    headers: {
      'Authorization': 'Bearer ' + '<YOUR_ACCESS_TOKEN>',
    },
    json: true,
  };

  try {
    const resp = await rp(options);
    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-gcp-project",
    "projectNumber": "...",
    "displayName": "First GCP Project",
    "name": "projects/first-gcp-project",
    "resources": {
      "hostingSite": "first-gcp-project",
      "realtimeDatabaseInstance": "first-gcp-project"
    }
  }
}

Since done is true and the response type is a FirebaseProject, the GCP 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:

async function addGoogleAnalytics(projectId, analyticsAccountId) {
  const options = {
    uri: 'https://firebase.googleapis.com/v1beta1/' + projectId + ':addGoogleAnalytics',
    method: 'POST',
    headers: {
      'Authorization': 'Bearer ' + '<your-access-token>',
    },
    body: {
      'analyticsAccountId': analyticsAccountId
    },
    json: true,
  };

  try {
    const resp = await rp(options);
    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.

Finalize your project's default location (Optional)

If your Firebase project will use Cloud Firestore, Cloud Storage, or a Google App Engine (GAE) app, you can finalize the default Google Cloud Platform (GCP) resource location for your project programmatically.

Check out Select locations for your project for information on which location is best for your project. If you're not sure which location to provide or you'd like to do this at another time, you can always select a location in the Firebase console later. This method creates an App Engine application with a default Cloud Storage bucket located in the locationId that you provide in the request body.

The default GCP resource location may have already been specified if the Project already has an App Engine application or this defaultLocation.finalize method was previously called.

REQUEST

Call projects.defaultLocation.finalize. Here's how to construct your request body:

  • Required:

    • locationId: The location where your data is stored for GCP services that require a location setting, like Cloud Firestore or Cloud Storage.
{
  "locationId": "us-west2"
}

Here's an example for Node.js to finalize your project's default location:

async function finalizeProjectLocation(projectId, locationId) {
  const accessToken = await getAccessToken();
  const options = {
    uri: 'https://firebase.googleapis.com/v1beta1/' + projectId + '/defaultLocation:finalize',
    method: 'POST',
    headers: {
      'Authorization': 'Bearer ' + accessToken,
    },
    body: {
      'locationId': locationId
    },
    json: true,
  };

  try {
    const resp = await rp(options);
    console.log(resp);
  } catch(err) {
    console.error(err['message']);
  }
}

RESULT

The result of a call to projects.defaultLocation.finalize 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 google.protobuf.Empty. If the operation is unsuccessful, the response body error will be of type google.rpc.Status. The Operation is automatically deleted after completion.