This quickstart shows you how to set up Cloud Firestore Enterprise edition, add data, then view the data you just added in the Firebase console using server client libraries for C#, Go, Java, Node.js, PHP, Python, and Ruby.
Use these client libraries to set up privileged server environments with full access to your database.
Create a Cloud Firestore database
If you haven't already, create a Firebase project: In the Firebase console, click Add project, then follow the on-screen instructions to create a Firebase project or to add Firebase services to an existing Google Cloud project.
Open your project in the Firebase console. In the left panel, expand Build and then select Firestore database.
Click Create database.
Select a location for your database.
If you aren't able to select a location, then your project's "location for default Google Cloud resources" has already been set. Some of your project's resources (like the default Cloud Firestore instance) share a common location dependency, and their location can be set either during project creation or when setting up another service that shares this location dependency.
Select a starting mode for your Cloud Firestore Security Rules:
- Test mode
Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Secure your data section.
To get started with the web, Apple platforms, or Android SDK, select test mode.
- Production mode
Denies all reads and writes from mobile and web clients. Your authenticated application servers (C#, Go, Java, Node.js, PHP, Python, or Ruby) can still access your database.
To get started with the C#, Go, Java, Node.js, PHP, Python, or Ruby server client library, select production mode.
Your initial set of Cloud Firestore Security Rules will apply to your default Cloud Firestore database. If you create multiple databases for your project, you can deploy Cloud Firestore Security Rules for each database.
Click Create.
When you enable Cloud Firestore, it also enables the API in the Cloud API Manager.
Set up your development environment
Add the required dependencies and client libraries to your app.
Java
- Add the Firebase Admin SDK to your app:
-
Using Gradle:
implementation 'com.google.firebase:firebase-admin:9.7.0'
-
Using Maven:
<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-admin</artifactId> <version>9.7.0</version> </dependency>
-
Using Gradle:
- Follow the instructions below to initialize Cloud Firestore with the proper credentials in your environment.
Python
- Add the Firebase Admin SDK to your Python app:
pip install --upgrade firebase-admin
- Follow the instructions below to initialize Cloud Firestore with the proper credentials in your environment.
Node.js
-
Add the Firebase Admin SDK to your app:
npm install firebase-admin --save
- Follow the instructions below to initialize Cloud Firestore with the proper credentials in your environment.
Go
- Add the Firebase Admin SDK to your Go app:
go get firebase.google.com/go
- Follow the instructions below to initialize Cloud Firestore with the proper credentials in your environment.
PHP
-
The Cloud Firestore server client libraries (Java, Node.js, Python, Go, PHP, C#, and Ruby) use
Google Application Default Credentials
for authentication.
-
To authenticate from your development environment, set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to point to a JSON service account key file. You can create a key file on the API Console Credentials page.export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json" - In your production environment, you do not need to authenticate if you run your application on App Engine or Compute Engine, using the same project that you use for Cloud Firestore. Otherwise, set up a service account.
-
To authenticate from your development environment, set the
- Install and enable the gRPC extension for PHP, which you will need to use the client library.
-
Add the Cloud Firestore PHP library to your app:
composer require google/cloud-firestore
Ruby
-
The Cloud Firestore server client libraries (Java, Node.js, Python, Go, PHP, C#, and Ruby) use
Google Application Default Credentials
for authentication.
-
To authenticate from your development environment, set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to point to a JSON service account key file. You can create a key file on the API Console Credentials page.export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/keyfile.json"
- In your production environment, you do not need to authenticate if you run your application on App Engine or Compute Engine, using the same project that you use for Cloud Firestore. Otherwise, set up a service account.
-
To authenticate from your development environment, set the
-
Add the Cloud Firestore Ruby library to your app in your
Gemfile:gem "google-cloud-firestore"
-
Install dependencies from your
Gemfileusing:bundle install
(Optional) Prototype and test with Firebase Local Emulator Suite
For mobile developers, before talking about how your app writes to and reads from Cloud Firestore, let's introduce a set of tools you can use to prototype and test Cloud Firestore functionality: Firebase Local Emulator Suite. If you're trying out different data models, optimizing your security rules, or working to find the most cost-effective way to interact with the back-end, being able to work locally without deploying live services can be a great idea.
A Cloud Firestore emulator is part of the Local Emulator Suite, which enables your app to interact with your emulated database content and config, as well as optionally your emulated project resources (functions, other databases, and security rules).
Using the Cloud Firestore emulator involves just a few steps:
- Adding a line of code to your app's test config to connect to the emulator.
- From the root of your local project directory, running
firebase emulators:start. - Making calls from your app's prototype code using a Cloud Firestore platform SDK as usual.
A detailed walkthrough involving Cloud Firestore and Cloud Functions is available. You should also have a look at the Local Emulator Suite introduction.
Initialize Cloud Firestore
Initialize an instance of Cloud Firestore:
Java
The Cloud Firestore SDK is initialized in different ways depending on your environment. Below are the most common methods. For a complete reference, see Initialize the Admin SDK.import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.firestore.Firestore; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; // Use the application default credentials GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); FirebaseOptions options = new FirebaseOptions.Builder() .setCredentials(credentials) .setProjectId(projectId) .build(); FirebaseApp.initializeApp(options); Firestore db = FirestoreClient.getFirestore();
To use the Firebase Admin SDK on your own server, use a service account.
Go to IAM & admin > Service accounts in the Google Cloud console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK:
import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.firestore.Firestore; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; // Use a service account InputStream serviceAccount = new FileInputStream("path/to/serviceAccount.json"); GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount); FirebaseOptions options = new FirebaseOptions.Builder() .setCredentials(credentials) .build(); FirebaseApp.initializeApp(options); Firestore db = FirestoreClient.getFirestore();
Python
The Cloud Firestore SDK is initialized in different ways depending on your environment. Below are the most common methods. For a complete reference, see Initialize the Admin SDK.import firebase_admin from firebase_admin import firestore # Application Default credentials are automatically created. app = firebase_admin.initialize_app() db = firestore.client()
An existing application default credential can also be used to initialize the SDK.
import firebase_admin from firebase_admin import credentials from firebase_admin import firestore # Use the application default credentials. cred = credentials.ApplicationDefault() firebase_admin.initialize_app(cred) db = firestore.client()
To use the Firebase Admin SDK on your own server, use a service account.
Go to IAM & admin > Service accounts in the Google Cloud console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK:
import firebase_admin from firebase_admin import credentials from firebase_admin import firestore # Use a service account. cred = credentials.Certificate('path/to/serviceAccount.json') app = firebase_admin.initialize_app(cred) db = firestore.client()
Python
The Cloud Firestore SDK is initialized in different ways depending on your environment. Below are the most common methods. For a complete reference, see Initialize the Admin SDK.import firebase_admin from firebase_admin import firestore_async # Application Default credentials are automatically created. app = firebase_admin.initialize_app() db = firestore_async.client()
An existing application default credential can also be used to initialize the SDK.
import firebase_admin from firebase_admin import credentials from firebase_admin import firestore_async # Use the application default credentials. cred = credentials.ApplicationDefault() firebase_admin.initialize_app(cred) db = firestore_async.client()
To use the Firebase Admin SDK on your own server, use a service account.
Go to IAM & admin > Service accounts in the Google Cloud console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK:
import firebase_admin from firebase_admin import credentials from firebase_admin import firestore_async # Use a service account. cred = credentials.Certificate('path/to/serviceAccount.json') app = firebase_admin.initialize_app(cred) db = firestore_async.client()
Node.js
The Cloud Firestore SDK is initialized in different ways depending on your environment. Below are the most common methods. For a complete reference, see Initialize the Admin SDK.-
Initialize on Cloud Functions
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app'); const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
initializeApp(); const db = getFirestore();
-
Initialize on Google Cloud
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app'); const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
initializeApp({ credential: applicationDefault() }); const db = getFirestore();
-
Initialize on your own server
To use the Firebase Admin SDK on your own server (or any other Node.js environment), use a service account. Go to IAM & admin > Service accounts in the Google Cloud console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK:
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app'); const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
const serviceAccount = require('./path/to/serviceAccountKey.json'); initializeApp({ credential: cert(serviceAccount) }); const db = getFirestore();
Go
The Cloud Firestore SDK is initialized in different ways depending on your environment. Below are the most common methods. For a complete reference, see Initialize the Admin SDK.import ( "log" firebase "firebase.google.com/go" "google.golang.org/api/option" ) // Use the application default credentials ctx := context.Background() conf := &firebase.Config{ProjectID: projectID} app, err := firebase.NewApp(ctx, conf) if err != nil { log.Fatalln(err) } client, err := app.Firestore(ctx) if err != nil { log.Fatalln(err) } defer client.Close()
To use the Firebase Admin SDK on your own server, use a service account.
Go to IAM & admin > Service accounts in the Google Cloud console. Generate a new private key and save the JSON file. Then use the file to initialize the SDK:
import ( "log" firebase "firebase.google.com/go" "google.golang.org/api/option" ) // Use a service account ctx := context.Background() sa := option.WithCredentialsFile("path/to/serviceAccount.json") app, err := firebase.NewApp(ctx, nil, sa) if err != nil { log.Fatalln(err) } client, err := app.Firestore(ctx) if err != nil { log.Fatalln(err) } defer client.Close()
PHP
PHP
For more on installing and creating a Cloud Firestore client, refer to Cloud Firestore Client Libraries.
C#
C#
For more on installing and creating a Cloud Firestore client, refer to Cloud Firestore Client Libraries.
Ruby
Add data
Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. You don't need to explicitly create collections or documents.
Create a new collection and a document using the following example code.
Java
Python
Python
Node.js
Go
PHP
PHP
For more on installing and creating a Cloud Firestore client, refer to Cloud Firestore Client Libraries.
C#
Ruby
Now add another document to the users collection. Notice that this document
includes a key-value pair (middle name) that does not appear in the first
document. Documents in a collection can contain different sets of information.
Java
Python
Python
Node.js
Go
PHP
PHP
For more on installing and creating a Cloud Firestore client, refer to Cloud Firestore Client Libraries.
C#
Ruby
Read data
Use the data viewer in the Firebase console to quickly verify that you've added data to Cloud Firestore.
You can also use the "get" method to retrieve the entire collection.
Java
Python
users_ref = db.collection("users") docs = users_ref.stream() for doc in docs: print(f"{doc.id} => {doc.to_dict()}")
Python
Node.js
Go
PHP
PHP
For more on installing and creating a Cloud Firestore client, refer to Cloud Firestore Client Libraries.
C#
Ruby
Next steps
Deepen your knowledge with the following topics:
- Data model — Learn more about how data is structured in Cloud Firestore, including hierarchical data and subcollections.
- Add data — Learn more about creating and updating data in Cloud Firestore.
- Get data — Learn more about how to retrieve data.
- Perform simple and compound queries — Learn how to run simple and compound queries.
- Order and limit queries Learn how to order and limit the data returned by your queries.