Create a Firestore database by using a web or mobile client library

This quickstart shows you how to set up Firestore, add data, and read data by using the Android, Apple platforms, Web, Unity, or C++ client library.

  1. 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.

  2. Open your project in the Firebase console. In the left panel, expand Build and then select Firestore database.

  3. Click Create database.

  4. 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 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.

  5. Select a starting mode for your 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.

    Locked 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 locked mode.

    Your initial set of Firestore Security Rules will apply to your default Firestore database. If you create multiple databases for your project, you can deploy Firestore Security Rules for each database.

  6. Click Create.

When you enable 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 version 9

  1. Follow the instructions to add Firebase to your Web app.
  2. Import both Firebase and Firestore:
    import { initializeApp } from "firebase/app";
    import { getFirestore } from "firebase/firestore";

Web version 8

  1. Follow the instructions to add Firebase to your Web app.
  2. Add the Firebase and Firestore libraries to your app:
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-firestore.js"></script>
    The Firestore SDK is also available as an npm package.
    npm install firebase@8.10.1 --save
    You'll need to manually require both Firebase and Firestore.
    const firebase = require("firebase");
    // Required for side-effects
    require("firebase/firestore");
Apple platforms

Follow the instructions to add Firebase to your Apple app.

Use Swift Package Manager to install and manage Firebase dependencies.

  1. In Xcode, with your app project open, navigate to File > Swift Packages > Add Package Dependency.
  2. When prompted, add the Firebase Apple platforms SDK repository:
  3.   https://github.com/firebase/firebase-ios-sdk
      
  4. Choose the Firestore library.
  5. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
Android
  1. Follow the instructions to add Firebase to your Android app.
  2. Declare the dependency for the Firestore library for Android in your module (app-level) Gradle file (usually app/build.gradle.kts or app/build.gradle):
    implementation("com.google.firebase:firebase-firestore:26.0.2")

    If your app uses multiple Firebase libraries, consider using the Firebase Android BoM, which ensures that your app's Firebase library versions are always compatible.

    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

  1. If you haven't already, configure and initialize Firebase in your Flutter app.
  2. From the root of your Flutter project, run the following command to install the plugin:
    flutter pub add cloud_firestore
  3. Once complete, rebuild your Flutter application:
    flutter run
C++
  1. Follow the instructions to add Firebase to your C++ project.
  2. 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.txt file:
              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}")
              
  3. To set up desktop integration, see Add Firebase to your C++ project.
Unity
  1. Follow the instructions to add Firebase to your Unity project.
  2. Use the Unity interface to configure your project to minify Android builds.
  3. You must minify the build to avoid the message Error while merging dex archives.

    • 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 multidex in:
      • mainTemplate.gradle if Custom Gradle Template under Player Settings is enabled
      • or, the module-level build.gradle file, if you use Android Studio to build the exported project.

Initialize Firestore in Native Mode

Initialize an instance of Firestore:

Web version 9

// Initialize Firestore through Firebase
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

const db = getFirestore();
The values for `initializeApp` can be found in your web app's `firebaseConfig`. To persist data when the device loses its connection, see the Enable Offline Data documentation.

Web version 8

// Initialize Firestore through Firebase
firebase.initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});

var db = firebase.firestore();
The values for `initializeApp` can be found in your web app's `firebaseConfig`. To persist data when the device loses its connection, see the Enable Offline Data documentation.
Swift
Note: This product is not available on watchOS and App Clip targets.
import FirebaseCore
import FirebaseFirestore

FirebaseApp.configure()

let db = Firestore.firestore()
Objective-C
Note: This product is not available on watchOS and App Clip targets.
@import FirebaseCore;
@import FirebaseFirestore;

// Use Firebase library to configure APIs
[FIRApp configure];

FIRFirestore *defaultFirestore = [FIRFirestore firestore];
  
Kotlin
Android
  // Access a Firestore instance from your Activity
  val db = Firebase.firestore.kt