Creating a Firestore client
Stay organized with collections
Save and categorize content based on your preferences.
Creating a Firestore client
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["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"]],[],[[["\u003cp\u003eThis page provides code samples for creating a Firestore client across multiple programming languages, including Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples in each language demonstrate how to initialize a Firestore client, with an option to specify a project ID or rely on the default project inferred from the environment.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Firestore requires setting up Application Default Credentials, with further instructions linked for local development environments.\u003c/p\u003e\n"],["\u003cp\u003eThe page directs users to the Google Cloud sample browser for more code samples and examples related to Firestore and other Google Cloud products.\u003c/p\u003e\n"]]],[],null,["Creating a Firestore client\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Add and update data](/firestore/native/docs/manage-data/add-data)\n- [Add data to Cloud Firestore](https://firebase.google.com/docs/firestore/manage-data/add-data)\n- [Create a Firestore database by using a server client library](/firestore/native/docs/create-database-server-client-library)\n- [Get data with Cloud Firestore](https://firebase.google.com/docs/firestore/query-data/get-data)\n- [Get started with Cloud Firestore](https://firebase.google.com/docs/firestore/quickstart)\n- [Getting data](/firestore/native/docs/query-data/get-data)\n\nCode sample \n\nGo\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"flag\"\n \t\"fmt\"\n \t\"log\"\n\n \t\"google.golang.org/api/iterator\"\n\n \t\"cloud.google.com/go/firestore\"\n )\n\n func createClient(ctx context.Context) *firestore.Client {\n \t// Sets your Google Cloud Platform project ID.\n \tprojectID := \"YOUR_PROJECT_ID\"\n\n \tclient, err := firestore.NewClient(ctx, projectID)\n \tif err != nil {\n \t\tlog.Fatalf(\"Failed to create client: %v\", err)\n \t}\n \t// Close client when done with\n \t// defer client.Close()\n \treturn client\n }\n\nJava\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Option 1: Initialize a Firestore client with a specific `projectId` and\n // authorization credential.\n FirestoreOptions firestoreOptions =\n FirestoreOptions.getDefaultInstance().toBuilder()\n .setProjectId(projectId)\n .setCredentials(GoogleCredentials.getApplicationDefault())\n .build();\n Firestore db = firestoreOptions.getService();\n\n // Option 2: Initialize a Firestore client with default values inferred from\n // your environment.\n Firestore db = FirestoreOptions.getDefaultInstance().getService();\n\nNode.js\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const admin = require('firebase-admin');\n\n initializeApp({\n // The `projectId` parameter is optional and represents which project the\n // client will act on behalf of. If not supplied, it falls back to the default\n // project inferred from the environment.\n projectId: 'my-project-id',\n });\n const db = getFirestore();\n\nPHP\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Firestore\\FirestoreClient;\n\n /**\n * Initialize Cloud Firestore with default project ID.\n */\n function setup_client_create(string $projectId = null)\n {\n // Create the Cloud Firestore client\n if (empty($projectId)) {\n // The `projectId` parameter is optional and represents which project the\n // client will act on behalf of. If not supplied, the client falls back to\n // the default project inferred from the environment.\n $db = new FirestoreClient();\n printf('Created Cloud Firestore client with default project ID.' . PHP_EOL);\n } else {\n $db = new FirestoreClient([\n 'projectId' =\u003e $projectId,\n ]);\n printf('Created Cloud Firestore client with project ID: %s' . PHP_EOL, $projectId);\n }\n }\n\nPython\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import firestore\n\n # The `project` parameter is optional and represents which project the client\n # will act on behalf of. If not supplied, the client falls back to the default\n # project inferred from the environment.\n db = firestore.https://cloud.google.com/python/docs/reference/firestore/latest/google.cloud.firestore_v1.client.Client.html(project=\"my-project-id\")\n\nRuby\n\n\nTo authenticate to Firestore, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"google/cloud/firestore\"\n\n # The `project_id` parameter is optional and represents which project the\n # client will act on behalf of. If not supplied, the client falls back to the\n # default project inferred from the environment.\n firestore = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-firestore-admin-v1/latest/Google-Cloud-Firestore.html.https://cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore.html project_id: project_id\n\n puts \"Created Cloud Firestore client with given project ID.\"\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]