Go to console

Set up and manage a project using the Management REST API

The Firebase Management REST API enables programmatic setup and management ofFirebase 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 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 Firebase 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

Before you look for GCP project resource names, make sure you have an existing GCP project and an API access token.

You can find GCP projects that are available to have Firebase services added to them by calling availableProjects.list. The request body for this call must be empty.

Here's an example for Node.js to request a list of available 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);
  }
}

The response body 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 GCP project programmatically. Before you add Firebase to a GCP project, make sure you have an existing GCP Project, an API access token, and the unique resource name of your project.

You can add Firebase services to your project by calling projects.addFirebase. The request body for this call must be empty.

Here's an example for Node.js to add Firebase services to your 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']);
  }
}

The result of this call is an Operation:

{
  "name": "operations/..."
}

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.

Add 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 apps to your Firebase project. Before you add apps to your FirebaseProject, make sure you have an API access token and that you've added Firebase services to your GCP project.

iOS

Add a Firebase iOS app to your project by calling projects.iosApps.create.

Constructing your request body:

  • Required:

    • bundleId: The canonical bundle ID of the iOS app as it would appear in the iOS App Store.
  • Optional, but recommended:

    • displayName: The user-assigned display name of the app. This value is useful for finding the app later in the Firebase console.
    • appStoreId: The automatically generated Apple ID assigned to the app by Apple. Specify an appStoreId if it has already been assigned by Apple.

In the request body for our example, we'll only use a displayName and bundleId:

{
  "displayName": "My Firebase iOS App",
  "bundleId": "com.firebase.ios"
}

Here's an example for Node.js to add an iOS app to your project:

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

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

The result of this call is an Operation:

{
  "name": "operations/..."
}

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 IosApp. 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.IosApp",
    "name": "projects/first-gcp-project/iosApps/...",
    "appId": "...",
    "displayName": "My Firebase iOS App",
    "projectId": "first-gcp-project",
    "bundleId": "com.firebase.ios"
  }
}

Since done is true and the response type is an IosApp, the FirebaseProject now has an IosApp. The response also contains other useful information about your newly created iOS app, like the unique Firebase appId.

Android

Add a Firebase Android app to your project by calling projects.androidApps.create.

Constructing your request body:

  • Required:

    • packageName: The canonical package name of the Android app as it would appear in the Google Play Developer Console.
  • Optional, but recommended:

    • displayName: The user-assigned display name of the app. This value is useful for finding your app later in the Firebase console.

In the request body for our example, we'll use packageName and displayName:

{
  "displayName": "My Firebase Android App"
  "packageName": "com.firebase.android"
}

Here's an example for Node.js to add an Android app to your project:

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

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

The result of this call is an Operation:

{
  "name": "operations/..."
}

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 AndroidApp. 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.AndroidApp",
    "name": "projects/first-gcp-project/androidApps/...",
    "appId": "...",
    "displayName": "My Firebase Android App",
    "projectId": "first-gcp-project",
    "packageName": "com.firebase.android"
  }
}

Since done is true and the response type is an AndroidApp, the FirebaseProject now has an AndroidApp. The response also contains other useful information about your newly created Android app, like the unique Firebase appId.

Add SHA certificates

You can also add SHA certificates to any existing Firebase Android app by calling projects.androidApps.sha.create. The request body for this method call must have an empty name field. The result of this call is a newly created instance of ShaCertificate.

When calling projects.androidApps.sha.create, you will need a valid SHA-1 or SHA-256 certificate hash. You can use this keytool command to generate your certificates:

$ keytool -exportcert -list -v -alias <your-key-name> -keystore <path-to-production-keystore>

For more information, visit the Google APIs for Android website.

Web

Add a Firebase Web app to your project by calling projects.webApps.create.

Constructing your request body:

  • Optional:

    • displayName: The user-assigned display name of app. This value is useful for finding your app later in the Firebase console.
  • Optional, but not recommended:

    • appUrls: The fully qualified URLs where the app is hosted. When a web app is associated with a Firebase Hosting site, Firebase automatically populates these fields, so leave these fields empty in your request body.

We'll only specify a displayName in the request body for our example:

{
  "displayName": "My Firebase Web App"
}

Here's an example for Node.js to add a Web app to your project:

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

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

The result of this call is an Operation:

{
  "name": "operations/..."
}

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 WebApp. 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.WebApp",
    "name": "projects/first-gcp-project/webApps/...",
    "appId": "...",
    "displayName": "My Firebase Web App",
    "projectId": "first-gcp-project"
  }
}

Since done is true and the response type is a WebApp, the FirebaseProject now has a WebApp. The response also contains other useful information about your newly created web app, like the unique Firebase appId.

You can link an existing Google Analytics account to your FirebaseProject by calling projects.addGoogleAnalytics. Make sure you have an API access token and that you've added Firebase services to your GCP project before you link a Google Analytics account.

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

  • Specifying an analyticsAccountId will provision a new Google Analytics property and associate the new property with your Firebase project.
  • Specifying an analyticsPropertyId will associate an existing Google Analytics property with your Firebase project.

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

Note that when you call projects.addGoogleAnalytics:

  • Any Firebase Apps already in your FirebaseProject are automatically provisioned as new data streams in the Google Analytics property.

  • Any data streams already an existing Google Analytics property are automatically associated with their corresponding Firebase Apps (only applies when an app's packageName and bundleId match those for an existing data stream).

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

The request body for our example call for project.addGoogleAnalytics will specify our Google Analytics account analyticsAccountId:

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

Here's an example for Node.js to link your 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']);
  }
}

The result of this call is an Operation:

{
  "name": "operations/..."
}

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",
    "appId": "...",
    "streamId": "..."
  }
}

Since done is true and the response type is analyticsDetails, the FirebaseProject is now linked to the specified Google Analytics account.

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 using projects.defaultLocation.finalize. Before you finalize your project's default location, make sure you have an API access token and that you've added Firebase services to your project.

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 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. The request body for this method must specify a locationId, like our example below:

{
  "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']);
  }
}

The result of this call is an Operation.

{
  "name": "operations/..."
}

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.