Crear un cliente de Firestore
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Crear un cliente de Firestore
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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\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### Go\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\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 // 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\n### Node.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\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 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\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\n### Ruby\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/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\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=firestore)."]]