Learn how to add a new document in Firestore with our step-by-step guide. Set up Firebase, install the SDK, initialize your app, and insert documents using JavaScript.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Firebase Project
To begin, ensure you have a Firebase project. If you haven't set one up yet, follow these instructions:
Step 2: Install Firebase SDK
Integrate Firebase into your application by installing the Firebase SDK. This example uses a web application with JavaScript.
npm install firebase
Step 3: Initialize Firebase in Your Application
Before you can add documents to Firestore, you'll need to initialize Firebase using the configuration object you obtained earlier.
firebaseApp.js).
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER\_ID",
appId: "YOUR_APP_ID"
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
export { db };
Step 4: Add a New Document to Firestore
Now, ensure your Firestore database is ready to store documents and collections:
Next, add a document:
addDocument.js) in your project.'yourCollectionName' and data according to your needs:
import { db } from './firebaseApp';
import { collection, addDoc } from "firebase/firestore";
async function addNewDocument() {
try {
const docRef = await addDoc(collection(db, "yourCollectionName"), {
name: "Tokyo",
country: "Japan"
});
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
}
addNewDocument();
Step 5: Test Your Implementation
To ensure your setup works as expected:
addNewDocument function.By following these steps, you have successfully added a new document to Firestore using Firebase! Make sure to secure your Firestore rules as your app progresses to production status.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.