Google.Cloud.Firestoreis a.NET client library for theFirestore API.
Note:
This documentation is for version3.10.0of the library.
Some samples may not work with other versions.
Installation
Install theGoogle.Cloud.Firestorepackage from NuGet. Add it to
your project in the normal way (for example by right-clicking on the
project in Visual Studio and choosing "Manage NuGet Packages...").
Authentication
When running on Google Cloud, no action needs to be taken to authenticate.
Otherwise, the simplest way of authenticating your API calls is to
set up Application Default Credentials.
The credentials will automatically be used to authenticate. SeeSet up Application Default Credentialsfor more details.
Getting started
Everything starts withFirestoreDb.
Create an instance using theCreateorCreateAsyncmethods, passing in your Google Cloud project ID.
The default credentials will be used to authenticate with the
server.FirestoreDbis thread-safe, and we recommend using a
single instance across your application where possible.
From there, you can create, fetch and modify documents, and run queries.
For customization of credentials and other settings, create aFirestoreDbBuilder, set the appropriate properties, and callBuildto create aFirestoreDb.
Sample code
FirestoreDb db = FirestoreDb.Create(projectId);
// Create a document with a random ID in the "users" collection.
CollectionReference collection = db.Collection("users");
DocumentReference document = await collection.AddAsync(new { Name = new { First = "Ada", Last = "Lovelace" }, Born = 1815 });
// A DocumentReference doesn't contain the data - it's just a path.
// Let's fetch the current document.
DocumentSnapshot snapshot = await document.GetSnapshotAsync();
// We can access individual fields by dot-separated path
Console.WriteLine(snapshot.GetValue<string>("Name.First"));
Console.WriteLine(snapshot.GetValue<string>("Name.Last"));
Console.WriteLine(snapshot.GetValue<int>("Born"));
// Or deserialize the whole document into a dictionary
Dictionary<string, object> data = snapshot.ToDictionary();
Dictionary<string, object> name = (Dictionary<string, object>) data["Name"];
Console.WriteLine(name["First"]);
Console.WriteLine(name["Last"]);
// See the "data model" guide for more options for data handling.
// Query the collection for all documents where doc.Born < 1900.
Query query = collection.WhereLessThan("Born", 1900);
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
foreach (DocumentSnapshot queryResult in querySnapshot.Documents)
{
string firstName = queryResult.GetValue<string>("Name.First");
string lastName = queryResult.GetValue<string>("Name.Last");
int born = queryResult.GetValue<int>("Born");
Console.WriteLine($"{firstName} {lastName}; born {born}");
}
UseChannelCredentials.Insecureas the channel credentials
Specify an "Authorization: Bearer owner" header on each request
As of version 1.1.0-beta02, the library has support for detecting
the emulator via the environment variable and connecting to it
automatically, if requested. This is configured viaFirestoreDbBuilder, which can also be used to configure custom
credentials easily.
The following code creates aFirestoreDbwhich will use the
emulator when the environment variables are present, but will
otherwise connect to the production environment.
FirestoreDb db = new FirestoreDbBuilder
{
ProjectId = projectId,
EmulatorDetection = EmulatorDetection.EmulatorOrProduction
}.Build();
// Use db as normal
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eGoogle.Cloud.Firestore\u003c/code\u003e library is a .NET client for the Firestore API, with the latest version being 3.10.0.\u003c/p\u003e\n"],["\u003cp\u003eInstallation of the library is done through the \u003ccode\u003eGoogle.Cloud.Firestore\u003c/code\u003e NuGet package, which can be added via Visual Studio.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication on Google Cloud requires no action, while other environments can leverage Application Default Credentials for authentication.\u003c/p\u003e\n"],["\u003cp\u003eInteraction with Firestore starts with a \u003ccode\u003eFirestoreDb\u003c/code\u003e instance, created using \u003ccode\u003eCreate\u003c/code\u003e or \u003ccode\u003eCreateAsync\u003c/code\u003e, and is thread-safe for single-instance use.\u003c/p\u003e\n"],["\u003cp\u003eThe library supports connecting to the Firestore Emulator via environment variables and \u003ccode\u003eFirestoreDbBuilder\u003c/code\u003e, with automatic detection as of version 1.1.0-beta02.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [3.10.0 (latest)](/dotnet/docs/reference/Google.Cloud.Firestore/latest)\n- [3.9.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.9.0)\n- [3.8.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.8.0)\n- [3.7.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.7.0)\n- [3.6.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.6.0)\n- [3.5.1](/dotnet/docs/reference/Google.Cloud.Firestore/3.5.1)\n- [3.4.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.4.0)\n- [3.3.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.3.0)\n- [3.2.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.2.0)\n- [3.1.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.1.0)\n- [3.0.0](/dotnet/docs/reference/Google.Cloud.Firestore/3.0.0)\n- [2.5.0](/dotnet/docs/reference/Google.Cloud.Firestore/2.5.0)\n- [2.4.0](/dotnet/docs/reference/Google.Cloud.Firestore/2.4.0)\n- [2.3.1](/dotnet/docs/reference/Google.Cloud.Firestore/2.3.1) \n\nGoogle.Cloud.Firestore\n======================\n\n`Google.Cloud.Firestore` is a.NET client library for the [Firestore API](https://firebase.google.com/docs/firestore/).\n\nNote:\nThis documentation is for version `3.10.0` of the library.\nSome samples may not work with other versions.\n\nInstallation\n------------\n\nInstall the `Google.Cloud.Firestore` package from NuGet. Add it to\nyour project in the normal way (for example by right-clicking on the\nproject in Visual Studio and choosing \"Manage NuGet Packages...\").\n\nAuthentication\n--------------\n\nWhen running on Google Cloud, no action needs to be taken to authenticate.\n\nOtherwise, the simplest way of authenticating your API calls is to\nset up Application Default Credentials.\nThe credentials will automatically be used to authenticate. See\n[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc) for more details.\n\nGetting started\n---------------\n\nEverything starts with [FirestoreDb](/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.FirestoreDb).\nCreate an instance using the `Create` or `CreateAsync` methods, passing in your Google Cloud project ID.\nThe default credentials will be used to authenticate with the\nserver. `FirestoreDb` is thread-safe, and we recommend using a\nsingle instance across your application where possible.\n\nFrom there, you can create, fetch and modify documents, and run queries.\n\nFor customization of credentials and other settings, create a\n`FirestoreDbBuilder`, set the appropriate properties, and call\n`Build` to create a `FirestoreDb`.\n\nSample code\n-----------\n\n FirestoreDb db = FirestoreDb.Create(projectId);\n\n // Create a document with a random ID in the \"users\" collection.\n CollectionReference collection = db.Collection(\"users\");\n DocumentReference document = await collection.AddAsync(new { Name = new { First = \"Ada\", Last = \"Lovelace\" }, Born = 1815 });\n\n // A DocumentReference doesn't contain the data - it's just a path.\n // Let's fetch the current document.\n DocumentSnapshot snapshot = await document.GetSnapshotAsync();\n\n // We can access individual fields by dot-separated path\n Console.WriteLine(snapshot.GetValue\u003cstring\u003e(\"Name.First\"));\n Console.WriteLine(snapshot.GetValue\u003cstring\u003e(\"Name.Last\"));\n Console.WriteLine(snapshot.GetValue\u003cint\u003e(\"Born\"));\n\n // Or deserialize the whole document into a dictionary\n Dictionary\u003cstring, object\u003e data = snapshot.ToDictionary();\n Dictionary\u003cstring, object\u003e name = (Dictionary\u003cstring, object\u003e) data[\"Name\"];\n Console.WriteLine(name[\"First\"]);\n Console.WriteLine(name[\"Last\"]);\n\n // See the \"data model\" guide for more options for data handling.\n\n // Query the collection for all documents where doc.Born \u003c 1900.\n Query query = collection.WhereLessThan(\"Born\", 1900);\n QuerySnapshot querySnapshot = await query.GetSnapshotAsync();\n foreach (DocumentSnapshot queryResult in querySnapshot.Documents)\n {\n string firstName = queryResult.GetValue\u003cstring\u003e(\"Name.First\");\n string lastName = queryResult.GetValue\u003cstring\u003e(\"Name.Last\");\n int born = queryResult.GetValue\u003cint\u003e(\"Born\");\n Console.WriteLine($\"{firstName} {lastName}; born {born}\");\n }\n\nSee the [user guide](/dotnet/docs/reference/Google.Cloud.Firestore/latest/userguide) for more samples.\n\nConnecting to the emulator\n--------------------------\n\nTo connect to the [Firestore\nEmulator](https://cloud.google.com/sdk/gcloud/reference/beta/emulators/firestore/),\nyou need to:\n\n- Connect to the emulator endpoint\n- Use `ChannelCredentials.Insecure` as the channel credentials\n- Specify an \"Authorization: Bearer owner\" header on each request\n\nAs of version 1.1.0-beta02, the library has support for detecting\nthe emulator via the environment variable and connecting to it\nautomatically, if requested. This is configured via\n`FirestoreDbBuilder`, which can also be used to configure custom\ncredentials easily.\n\nThe following code creates a `FirestoreDb` which will use the\nemulator when the environment variables are present, but will\notherwise connect to the production environment. \n\n FirestoreDb db = new FirestoreDbBuilder\n {\n ProjectId = projectId,\n EmulatorDetection = EmulatorDetection.EmulatorOrProduction\n }.Build();\n // Use db as normal\n\nSee the\n[EmulatorDetection property](/dotnet/docs/reference/Google.Cloud.Firestore/latest/Google.Cloud.Firestore.FirestoreDbBuilder#Google_Cloud_Firestore_FirestoreDbBuilder_EmulatorDetection)\nfor more details."]]