Google is committed to advancing racial equity for Black communities. See how.

Automate database creation

You can automate Cloud Firestore database creation with the following process:

  1. Create a new Google Cloud Platform project. Each project is limited to one Cloud Firestore database.
  2. Create an App Engine application. Cloud Firestore depends on App Engine. You must activate App Engine to use Cloud Firestore.
  3. Create a Cloud Firestore database.

This page describes how to complete this process using REST APIs, the gcloud command-line tool, and Terraform.

Create a database with REST APIs

You can provision a Cloud Firestore database using the Resource Manager API and the App Engine Admin API.

Authentication and authorization

To access the Resource Manager and App Engine Admin APIs, you must authenticate your request with an access token. Your access token requires an OAuth 2.0 scope of:

https://www.googleapis.com/auth/cloud-platform

To set up authentication for an application, see setting up authentication for server to server production applications.

While developing and testing your application, you can obtain an access token using:

gcloud auth application-default print-access-token.

Authorization

The authenticated user account or service account requires the resourcemanager.projects.create permission to create a new project. The Project Creator IAM role, for example, grants this permission.

To grant this role, see granting, changing, and revoking access to resources.

Create a new project and database

  1. Use the projects.create method to start a project creation operation. In the request body, define a Project resource, for example:

    HTTP

    POST https://cloudresourcemanager.googleapis.com/v1/projects HTTP/1.1
    
    Authorization: Bearer access-token
    Accept: application/json
    Content-Type: application/json
    
    {
      "projectId": "project-id"
    }
          

    where:

    On success, the request returns an operation name:

    200:
    
    {
      "name": "operations/cp.6311184959990822268"
    }
        
  2. Use the operation name from the previous step and the Resource Manager operations.get method to confirm project creation:

    HTTP

    GET https://cloudresourcemanager.googleapis.com/v1/operations/operation-name HTTP/1.1
    
    Authorization: Bearer access-token
    Accept: application/json
    

    When project creation succeeds, the response includes the following field:

    "done": true,
          
  3. Use the App Engine Admin API apps.create method to create an App Engine application and Cloud Firestore database. In the request body, define an Appplication resource and include the databaseType field, for example:

    HTTP

    POST https://appengine.googleapis.com/v1/apps HTTP/1.1
    
    Authorization: Bearer access_token
    Accept: application/json
    Content-Type: application/json
    
    {
      "databaseType": "CLOUD_FIRESTORE",
      "id": "project-id",
      "locationId": "location"
    }
          

    where:

    • project-id is the ID of the project you created.
    • location sets the location of both your App Engine application and your Cloud Firestore database. Once set, you cannot change the location. For a full list of supported locations, see App Engine locations.

      App Engine and Cloud Firestore support the same locations, but the following App Engine regions map to Cloud Firestore multi-regions:

      • us-central (Iowa) creates a Cloud Firestore database in the nam5 (United States) multi-region.
      • europe-west (Belgium) creates a Cloud Firestore database in the eur3 (Europe) multi-region.

    The request returns an operation name:

    200:
    
    {
      "name": "apps/project-id/operations/8612e502-4aeb-4f12-9e41-bbac0a0b819c",
      "metadata": {
        "@type": "type.googleapis.com/google.appengine.v1.OperationMetadataV1",
        "method": "google.appengine.v1.Applications.CreateApplication",
        "insertTime": "2020-06-05T23:34:32.587Z",
        "user": "username",
        "target": "apps/project-id"
      }
    }
            
  4. Use the operation name from the previous step and the apps.operations.get to confirm database creation:

    HTTP

    GET https://appengine.googleapis.com/v1/apps/operation-name HTTP/1.1
    
    Authorization: Bearer access-token
    Accept: application/json
        

    When the operation succeeds, the response includes the following field:

    "done": true,
          

Add a billing account and Firebase services

To programmatically associate a billing account with your project, use the projects.updateBillingInfo method.

To programmatically enable Firebase services for your project, see set up and manage a Firebase project using the Management REST API.

Create a database with gcloud

You can use the gcloud command-line tool to automate database creation in a Bash or PowerShell script. In your script, complete the following steps:

  1. Create a new project with gcloud projects create:

    gcloud projects create project-id
  2. Activate App Engine with gcloud app create:

    gcloud app create --region=region --project=project-id

    where region is the location of both your App Engine application and your Cloud Firestore database. Once set, you cannot change the location. For a full list of supported locations, see App Engine locations.

    App Engine and Cloud Firestore support the same locations, but the following App Engine regions map to Cloud Firestore multi-regions:

    • us-central (Iowa) creates a Cloud Firestore database in the nam5 (United States) multi-region.
    • europe-west (Belgium) creates a Cloud Firestore database in the eur3 (Europe) multi-region.
  3. Enable the App Engine Admin API with gcloud services enable:

    gcloud services enable appengine.googleapis.com --project=project-id
  4. Create a Cloud Firestore database with gcloud alpha firestore databases create or gcloud alpha datastore databases create:

    gcloud alpha firestore databases create --project=project-id --region=region

    To create a Cloud Firestore in Datastore mode database, use:

    gcloud alpha datastore databases create --project=project-id --region=region

    For region, you must use the same value you used to activate App Engine.

Create a database with Terraform

To provision a Cloud Firestore database with Terraform, use the google_app_engine_application resource. Set the database_type to CLOUD_FIRESTORE or CLOUD_DATASTORE_COMPATIBILITY.

For example, the following Terraform configuration file creates a new project and provisions a Cloud Firestore database:

firestore.tf

provider "google" {
  credentials = file("credentials-file")
}

resource "google_project" "my_project" {
  name = "My Project"
  project_id = "project-id"
}

resource "google_app_engine_application" "app" {
  project     = google_project.my_project.project_id
  location_id = "location"
  database_type = "CLOUD_FIRESTORE"
}

where:

  • credentials-file is the path to your service account key file.
  • project-id is your project ID. Project IDs must be unique.
  • location is the location of both your App Engine application and your Cloud Firestore database. Once set, you cannot change the location. For a full list of supported locations, see App Engine locations.

    App Engine and Cloud Firestore support the same locations, but the following App Engine regions map to Cloud Firestore multi-regions:

    • us-central (Iowa) creates a Cloud Firestore database in the nam5 (United States) multi-region.
    • europe-west (Belgium) creates a Cloud Firestore database in the eur3 (Europe) multi-region.