Create a Firestore database by using a server client library
This quickstart shows you how to set up Firestore, add data, and read data by using the C#, Go, Java, Node.js, PHP, Python, or Ruby server client library.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Create a Firestore in Native mode database
If this is a new project, you need to create a Firestore database instance.
From the Select a database service screen, choose Firestore in Native mode.
Select a location for your Firestore.
This location setting is your project's default Google Cloud Platform (GCP) resource location. Note that this location will be used for GCP services in your project that require a location setting, specifically, your default Cloud Storage bucket and your App Engine app (which is required if you use Cloud Scheduler).
Click Create Database.
When you create a Firestore project, it also enables the API in the Cloud API Manager.
Set up authentication
To run the client library, you must first set up authentication by creating a service account and setting an environment variable.
Provide authentication credentials to your application code by setting the
environment variable GOOGLE_APPLICATION_CREDENTIALS
. This
variable applies only to your current shell session. If you want the variable
to apply to future shell sessions, set the variable in your shell startup file,
for example in the ~/.bashrc
or ~/.profile
file.
Linux or macOS
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH
"
Replace KEY_PATH
with the path of the JSON file that contains your credentials.
For example:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
Windows
For PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH
"
Replace KEY_PATH
with the path of the JSON file that contains your credentials.
For example:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
For command prompt:
set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
Replace KEY_PATH
with the path of the JSON file that contains your credentials.
Add the server client library to your app
Add the required dependencies and client libraries to your app.
Java
Add the Firestore Java library to your app:
-
Using Maven:
<dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>libraries-bom</artifactId> <version>26.49.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-firestore</artifactId> </dependency>
- If you are using Gradle or setting up without BOM, see the Firestore Client for Java README.
-
Using an IDE:
If you're using VS Code, IntelliJ, or Eclipse, you can add client libraries to your project using these IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Python
Add the Firestore Python library to your app:
pip install --upgrade google-cloud-firestore
Node.js
Add the Firestore Node.js library to your app:
npm install --save @google-cloud/firestore
Go
Install the Firestore Go library:
go get cloud.google.com/go/firestore
Add the Firestore Go library to your app:
import "cloud.google.com/go/firestore"
PHP
- Install and enable the gRPC extension for PHP, which you will need to use the client library.
-
Add the Firestore PHP library to your app:
composer require google/cloud-firestore
C#
-
Add the Firestore C# library to your app in your
.csproj
file:<ItemGroup> <PackageReference Include="Google.Cloud.Firestore" Version="1.1.0-beta01" /> </ItemGroup>
-
Add the following to your
Program.cs
file:using Google.Cloud.Firestore;
Ruby
-
Add the Firestore Ruby library to your app in your
Gemfile
:gem "google-cloud-firestore"
-
Install dependencies from your
Gemfile
using:bundle install
Initialize Firestore
Initialize an instance of Firestore:
Java
import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreOptions;
FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() .setProjectId(projectId) .setCredentials(GoogleCredentials.getApplicationDefault()) .build(); Firestore db = firestoreOptions.getService();
Python
from google.cloud import firestore # The `project` parameter is optional and represents which project the client # will act on behalf of. If not supplied, the client falls back to the default # project inferred from the environment. db = firestore.Client(project="my-project-id")
Python
(Async)
from google.cloud import firestore # The `project` parameter is optional and represents which project the client # will act on behalf of. If not supplied, the client falls back to the default # project inferred from the environment. db = firestore.AsyncClient(project="my-project-id")
Node.js
const Firestore = require('@google-cloud/firestore'); const db = new Firestore({ projectId: 'YOUR_PROJECT_ID', keyFilename: '/path/to/keyfile.json', });
Go
import ( "context" "flag" "fmt" "log" "google.golang.org/api/iterator" "cloud.google.com/go/firestore" ) func createClient(ctx context.Context) *firestore.Client { // Sets your Google Cloud Platform project ID. projectID := "YOUR_PROJECT_ID" client, err := firestore.NewClient(ctx, projectID) if err != nil { log.Fatalf("Failed to create client: %v", err) } // Close client when done with // defer client.Close() return client }
PHP
use Google\Cloud\Firestore\FirestoreClient; /** * Initialize Cloud Firestore with default project ID. */ function setup_client_create(string $projectId = null) { // Create the Cloud Firestore client if (empty($projectId)) { // The `projectId` parameter is optional and represents which project the // client will act on behalf of. If not supplied, the client falls back to // the default project inferred from the environment. $db = new FirestoreClient(); printf('Created Cloud Firestore client with default project ID.' . PHP_EOL); } else { $db = new FirestoreClient([ 'projectId' => $projectId, ]); printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId); } }
C#
Ruby
require "google/cloud/firestore" # The `project_id` parameter is optional and represents which project the # client will act on behalf of. If not supplied, the client falls back to the # default project inferred from the environment. firestore = Google::Cloud::Firestore.new project_id: project_id puts "Created Cloud Firestore client with given project ID."
Add data
Firestore stores data in Documents, which are stored in Collections. Firestore creates collections and documents implicitly the first time you add data to the document. You do not need to explicitly create collections or documents.
Create a new collection and a document using the following example code.
Java
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
(Async)
Node.js
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C#
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
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
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
(Async)
Node.js
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C#
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Read data
To quickly verify that you've added data to Firestore, use the data viewer in the Firebase console.
You can also use the get
method to retrieve the entire collection.
Java
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
(Async)
Node.js
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C#
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To authenticate to Firestore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Next steps
Deepen your knowledge with the following topics:
- Data model — Learn more about how data is structured in Firestore, including hierarchical data and subcollections.
- Add data — Learn more about creating and updating data in 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.