This quickstart shows you how to set up Cloud Firestore , add data, then view the data you just added in the Firebase console.
Cloud Firestore supports mobile or web SDKs and server client libraries:
-
Cloud Firestore supports SDKs for Android, iOS, and web and more. Combined with Cloud Firestore Security Rules and Firebase Authentication , the mobile and web SDKs support serverless app architectures where clients connect directly to your Cloud Firestore database.
-
Cloud Firestore supports server client librariesfor C#, Go, Java, Node.js, PHP, Python, and Ruby. Use these client libraries to set up privileged server environments with full access to your database. Learn more about these libraries in the Quickstart for server client libraries .
Create a Cloud Firestore database
-
If you haven't already, create a Firebase project: In the Firebase console , click Add project, then follow the on-screen instructions to create a Firebase project or to add Firebase services to an existing Google Cloud project.
-
Open your project in the Firebase console. In the left panel, expand Buildand then select Firestore database .
-
Click Create database.
-
Select a location for your database.
If you aren't able to select a location, then your project's "location for default Google Cloud resources" has already been set. Some of your project's resources (like the default Cloud Firestore instance) share a common location dependency, and their location can be set either during project creation or when setting up another service that shares this location dependency.
-
Select a starting mode for your Cloud Firestore Security Rules :
- Test mode
-
Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Secure your data section.
-
To get started with the web, Apple platforms, or Android SDK, select test mode.
- Production mode
-
Denies all reads and writes from mobile and web clients. Your authenticated application servers (C#, Go, Java, Node.js, PHP, Python, or Ruby) can still access your database.
-
To get started with the C#, Go, Java, Node.js, PHP, Python, or Ruby server client library, select production mode.
Your initial set of Cloud Firestore Security Rules will apply to your default Cloud Firestore database. If you create multiple databases for your project, you can deploy Cloud Firestore Security Rules for each database.
-
Click Create.
When you enable Cloud Firestore , it also enables the API in the Cloud API Manager .
Set up your development environment
Add the required dependencies and client libraries to your app.
Web
- Follow the instructions to add Firebase to your web app .
- The Cloud Firestore
SDK is available as an npm package.
You'll need to import both Firebase and Cloud Firestore .npm install firebase @12.8.0 -- save
import { initializeApp } from "firebase/app" ; import { getFirestore } from "firebase/firestore" ;
Web
- Follow the instructions to add Firebase to your web app .
- Add the Firebase and Cloud Firestore
libraries to your app:
The Cloud Firestore SDK is also available as an npm package.<script src="https://www.gstatic.com/firebasejs/12.8.0/firebase-app-compat.js"></script> <script src="https://www.gstatic.com/firebasejs/12.8.0/firebase-firestore-compat.js"></script>
You'll need to manually require both Firebase and Cloud Firestore .npm install firebase @12.8.0 -- save
import firebase from "firebase/compat/app" ; // Required for side - effects import "firebase/firestore" ;
iOS+
Follow the instructions to add Firebase to your Apple app .
Use Swift Package Manager to install and manage Firebase dependencies.
- In Xcode, with your app project open, navigate to File > Swift Packages > Add Package Dependency .
- When prompted, add the Firebase Apple platforms SDK repository:
- Choose the Firestore library.
- When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
https://github.com/firebase/firebase-ios-sdk
Android
- Follow the instructions to add Firebase to your Android app .
- Using the Firebase Android BoM
,
declare the dependency for the Cloud Firestore
library for Android in
your module (app-level) Gradle file
(usually
app/build.gradle.ktsorapp/build.gradle).dependencies { // Import the BoM for the Firebase platform implementation ( platform ( "com.google.firebase:firebase-bom:34.8.0" )) // Declare the dependency for the Cloud Firestore library // When using the BoM, you don't specify versions in Firebase library dependencies implementation ( "com.google.firebase:firebase-firestore" ) }
By using the Firebase Android BoM , your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM , you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
dependencies { // Declare the dependency for the Cloud Firestore library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation ( "com.google.firebase:firebase-firestore:26.1.0" ) }
Looking for a Kotlin-specific library module? Starting with the October 2023 release , both Kotlin and Java developers can depend on the main library module (for details, see the FAQ about this initiative ).
Dart
- If you haven't already, configure and initialize Firebase in your Flutter app.
- From the root of your Flutter project, run the following command to
install the plugin:
flutter pub add cloud_firestore
- Once complete, rebuild your Flutter application:
flutter run
- Optional:
Improve iOS & macOS build times by including the
pre-compiled framework.
Currently, the Firestore SDK for iOS depends on code that can take upwards of 5 minutes to build in Xcode. To reduce build times significantly, you can use a pre-compiled version by adding this line to the
target 'Runner' doblock in your Podfile:target 'Runner' do use_frameworks! use_modular_headers! pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => ' IOS_SDK_VERSION ' flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) target 'RunnerTests' do inherit! :search_paths end end
Replace IOS_SDK_VERSION with the version of the Firebase iOS SDK specified in
firebase_core'sfirebase_sdk_version.rbfile. If you're not using the latest version offirebase_core, look for this file in your local Pub package cache (usually~/.pub-cache).Additionally, ensure that you have upgraded CocoaPods to 1.9.1 or higher:
gem install cocoapods
For more information see the issue on GitHub .
C++
- Follow the instructions to add Firebase to your C++ project .
- C++ interface for Android.
- Gradle dependencies.Add the following to your module
(app-level) Gradle file (usually
app/build.gradle):android . defaultConfig . externalNativeBuild . cmake { arguments "-DFIREBASE_CPP_SDK_DIR=$gradle.firebase_cpp_sdk_dir" } apply from : "$gradle.firebase_cpp_sdk_dir/Android/firebase_dependencies.gradle" firebaseCpp . dependencies { // earlier entries auth firestore }
- Binary dependencies.Similarly, the recommended way to get
the binary dependencies is to add the following to your
CMakeLists.txtfile:add_subdirectory ( $ { FIREBASE_CPP_SDK_DIR } bin / EXCLUDE_FROM_ALL ) set ( firebase_libs firebase_auth firebase_firestore firebase_app ) # Replace the target name below with the actual name of your target, # for example, "native-lib". target_link_libraries ( $ { YOUR_TARGET_NAME_HERE } "${firebase_libs}" )
- To set up desktop integration, see Add Firebase to your C++ project .
Unity
- Follow the instructions to add Firebase to your Unity project .
- Use the Unity interface to configure your project to minify Android builds.
- The option can be found in Player Settings > Android > Publishing Settings > Minify.
- The options may differ in different versions of Unity so refer to the official Unity documentation and the Firebase Unity Build Debug Guide .
- If, after enabling minification, the number of referenced methods still
exceeds the limit, another option is to enable
multidexin:-
mainTemplate.gradleif Custom Gradle Templateunder Player Settingsis enabled - or, the module-level
build.gradlefile, if you use Android Studio to build the exported project.
-
You must minify the build to avoid the message Error while merging dex archives
.
(Optional) Prototype and test with Firebase Local Emulator Suite
For mobile developers, before talking about how your app writes to and reads from Cloud Firestore , let's introduce a set of tools you can use to prototype and test Cloud Firestore functionality: Firebase Local Emulator Suite . If you're trying out different data models, optimizing your security rules, or working to find the most cost-effective way to interact with the back-end, being able to work locally without deploying live services can be a great idea.
A Cloud Firestore emulator is part of the Local Emulator Suite , which enables your app to interact with your emulated database content and config, as well as optionally your emulated project resources (functions, other databases, and security rules).
Using the Cloud Firestore emulator involves just a few steps:
- Adding a line of code to your app's test config to connect to the emulator.
- From the root of your local project directory, running
firebase emulators:start. - Making calls from your app's prototype code using a Cloud Firestore platform SDK as usual.
A detailed walkthrough involving Cloud Firestore and Cloud Functions is available. You should also have a look at the Local Emulator Suite introduction .
Initialize Cloud Firestore
Initialize an instance of Cloud Firestore :
Web
import { initializeApp } from "firebase/app" ; import { getFirestore } from "firebase/firestore" ; // TODO : Replace the following with your app 's Firebase project configuration // See : https : // support . google . com / firebase / answer / 7015592 const firebaseConfig = { FIREBASE_CONFIGURATION }; // Initialize Firebase const app = initializeApp ( firebaseConfig ); // Initialize Cloud Firestore and get a reference to the service const db = getFirestore ( app );
Replace FIREBASE_CONFIGURATION
with your web app's firebaseConfig
.
To persist data when the device loses its connection, see the Enable Offline Data documentation.
Web
import firebase from "firebase/app" ; import "firebase/firestore" ; // TODO : Replace the following with your app 's Firebase project configuration // See : https : // support . google . com / firebase / answer / 7015592 const firebaseConfig = { FIREBASE_CONFIGURATION }; // Initialize Firebase firebase . initializeApp ( firebaseConfig ); // Initialize Cloud Firestore and get a reference to the service const db = firebase . firestore ();
Replace FIREBASE_CONFIGURATION
with your web app's firebaseConfig
.
To persist data when the device loses its connection, see the Enable Offline Data documentation.
Swift
import FirebaseCore import FirebaseFirestore
FirebaseApp . configure () let db = Firestore . firestore ()
Objective-C
@import FirebaseCore ; @import FirebaseFirestore ; // Use Firebase library to configure APIs [ FIRApp configure ];
FIRFirestore * defaultFirestore = [ FIRFirestore firestore ];
Kotlin
// Access a Cloud Firestore
instance from your Activity
val db = Firebase . firestore . kt

