Firestore Synchronous Import Code


By Ronnie Royston

Originally Published 6/12/2018
Updated 6/12/2018


In case you have under 500 documents to write to Google's Firebase Firestore, a synchronous write is the way to go. The first step is to get your data in JavaScript Object Notation, JSON, format. Most spreadsheet applications such as Microsoft Excel or Google Sheets offer an export data as JSON option. I use Google Sheets and an add-on called "Export Sheet Data." Add a header row which will identify each column of data, for example column A is "id" and column B is "url". Export your data into a text editor and save the file with a .json file extension.

Once exported you should have a .json file that looks something like below. Save this file in your local node directory.

  "AA": {
    "id": "AA",
    "url": ""
  "BB": {
    "id": "BB",
    "url": ""
  "CC": {
    "id": "CC",
    "url": ""

Setup and Installation

Firestore offers a couple of JavaScript based methods to import data, namely the JavaScript (Web) SDK and the Firebase Admin (Node) SDK. The Admin SDK is the proper tool for this job. Installing the Admin SDK is easy. Install Node on your workspace then install the Firebase Admin SDK. Finally, in the Firebase web console, generate a service account key and save it in your local node directory. You are ready to go.

Follow the Add the Firebase Admin SDK to Your Server instructions provided by Firebase to get Node.js and the Firebase Admin SDK installed on your machine.

Node Script

The script below is run from your local Node workspace and uses the service account key to authenticate to the Firebase system. A unique key is assigned to each document and all documents are pushed to Firestore at once.

var admin = require("firebase-admin");
var serviceAccount = require("./your-service-account-secret-key.json");
var fs = require('fs');
  credential: admin.credential.cert(serviceAccount),
  databaseURL: ""
var firestore = admin.firestore();
var writeBatch = firestore.batch();
var obj = JSON.parse(fs.readFileSync('your-file.json'));
for (var key in obj) {
    var ref = firestore.collection("your-collection-name").doc();
    writeBatch.set(ref, obj[key]);
writeBatch.commit().then(function () {
.catch(function(error) {
    console.error("Error writing batch: ", error);


If you run into problems be sure and validate your JSON file. JSON is very picky. If you are missing one comma or quote, the whole process will fail.

What if I need to import more than 500 documents at once? Asynchronous batch writes enables you to write millions of documents at a rate of 500 per second. Firebase Admin SDK Bulk Import Example walks you through that process and includes a sample script.

Automate a workflow or process today with standards based, library free JavaScript in the browser, in the cloud with NodeJS, or both. High Tekk can do that! See our services page for more details.

Dialog Title

Cell Phone Number

Your order ID is . The grand total is , or bitcoin.

This action cannot be undone. Continue?

Changes will be lost. Continue?

Delete location?

A signin link will be sent to your email address.

You privacy is top priority. Email addresses are never shared with a third party. For more information see our privacy policy.

Find a bug? Experience an error? How can we do better? We appreciate your feedback.

Delete order? This action cannot be undone.

Delete order? This action cannot be undone.

This action cannot be undone. Continue?