You can automate Cloud Firestore database creation with the following process:
- Create a new Google Cloud project. Each project is limited to one Cloud Firestore database.
- Create an App Engine application. Cloud Firestore depends on App Engine. You must activate App Engine to use Cloud Firestore.
- 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
- 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:
access-token
is an authenticated access token.project-id
is a unique project ID.
On success, the request returns an operation name:
200: { "name": "operations/cp.6311184959990822268" }
-
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,
-
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 Application 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 thenam5
(United States) multi-region.europe-west
(Belgium) creates a Cloud Firestore database in theeur3
(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" } }
-
Use the operation name from the previous step and the apps.operations.get to confirm database creation:
HTTP
GET https://appengine.googleapis.com/v1/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:
Create a new project with
gcloud projects create
:gcloud projects create project-id
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 thenam5
(United States) multi-region.europe-west
(Belgium) creates a Cloud Firestore database in theeur3
(Europe) multi-region.
Enable the App Engine Admin API with
gcloud services enable
:gcloud services enable appengine.googleapis.com --project=project-id
Create a Cloud Firestore database with
gcloud alpha firestore databases create
orgcloud 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_firestore_database
resource.
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_project_service" "firestore" { project = google_project.my_project.project_id service = "firestore.googleapis.com" } resource "google_firestore_database" "database" { project = google_project.my_project.project_id name = "(default)" location_id = "location" type = "FIRESTORE_NATIVE" depends_on = [google_project_service.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 Cloud Firestore database. Once set, you cannot change the location. For a full list of supported locations, see Cloud Firestore locations.
If you wish to use App Engine, use the
google_app_engine_application
resource instead. Set the database_type
to CLOUD_FIRESTORE
or
CLOUD_DATASTORE_COMPATIBILITY
.
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" }
us-central
(Iowa) creates a Cloud Firestore database in thenam5
(United States) multi-region.europe-west
(Belgium) creates a Cloud Firestore database in theeur3
(Europe) multi-region.
Furthermore, Cloud Firestore is available in some regions that App Engine is not.