Create Client with Project ID
Stay organized with collections
Save and categorize content based on your preferences.
Create Client with Project ID
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 document provides code samples for creating a Cloud Firestore client with a specified project ID across C#, Java, PHP, and Python.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication to Firestore requires setting up Application Default Credentials, as detailed in the provided link to local development environment setup.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples demonstrate how to initialize the Firestore client, either with a provided project ID or by relying on the default project inferred from the environment.\u003c/p\u003e\n"],["\u003cp\u003eFurther documentation on managing, adding, and getting data within Cloud Firestore is accessible through the linked resources.\u003c/p\u003e\n"],["\u003cp\u003eThe google cloud sample browser can be used to find code samples for other google cloud products.\u003c/p\u003e\n"]]],[],null,["# Create Client with Project ID\n\nExplore further\n---------------\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-----------\n\n### C#\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 FirestoreDb db = FirestoreDb.Create(project);\n Console.WriteLine(\"Created Cloud Firestore client with project ID: {0}\", project);\n\n### Java\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 FirestoreOptions firestoreOptions =\n FirestoreOptions.getDefaultInstance().toBuilder()\n .setProjectId(projectId)\n .setCredentials(GoogleCredentials.getApplicationDefault())\n .build();\n Firestore db = firestoreOptions.getService();\n\n### PHP\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 a provided project ID.\n *\n * @param string $projectId Your Google Cloud Project ID\n */\n function setup_client_create_with_project_id(string $projectId): void\n {\n // Create the Cloud Firestore client with a provided project ID.\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### Python\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\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]