Skip to main content
This guide walks you through creating an Express.js app, connecting it to Firestore, and making your first API requests.
1

Create an Express project

Create a new directory and install the required dependencies:
mkdir my-app && cd my-app
npm init -y
npm install express cors body-parser
npm install -D nodemon
npm install firebase-express-sdk
2

Create a Firebase project and get a service account

  1. Go to the Firebase console and create a new project.
  2. In the left sidebar, click Build > Firestore Database, then click Create database and follow the prompts to enable Cloud Firestore.
  3. Go to Project Settings (gear icon) > Service accounts.
  4. Click Generate new private key and confirm. Firebase downloads a JSON file.
  5. Rename the downloaded file to serviceAccount.json and move it to your project root.
Your project structure should look like this:
.
├── node_modules/
├── serviceAccount.json
├── index.js
├── package.json
└── package-lock.json
3

Initialize the SDK

Create index.js in your project root with the following code:
index.js
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const { FirebaseExpressSdk } = require("firebase-express-sdk");
const serviceAccountFile = require("./serviceAccount.json");

const app = express();
const port = 3001;

app.use(cors());
app.use(bodyParser.json());

const collections = {
  users: {
    documentAttributes: ["name", "age", "email"],
  },
};

const firebaseExpressSdk = new FirebaseExpressSdk({
  app,
  serviceAccountFile,
  collections,
  port,
});

firebaseExpressSdk.addActions([
  {
    collection: "users",
    endpoint: "/api/getUsers",
    request: { type: "GET" },
  },
  {
    collection: "users",
    endpoint: "/api/addUser",
    request: { type: "POST" },
  },
]);
The named export is FirebaseExpressSdk — note the lowercase k at the end. Use this exact casing when importing from the package.
4

Add a start script and run

Open package.json and update the scripts field:
package.json
"scripts": {
  "start": "nodemon index.js"
}
Then start the server:
npm start
Your server is now running at http://localhost:3001.
5

Test your endpoints

Add a user with a POST request:
POST http://localhost:3001/api/addUser
Content-Type: application/json

{
  "name": "Jane Doe",
  "age": 28,
  "email": "jane@example.com"
}
Expected response:
{ "status": "success", "data": { "id": "abc123xyz" } }
Fetch all users with a GET request:
GET http://localhost:3001/api/getUsers
Expected response:
{
  "status": "success",
  "data": [
    { "id": "abc123xyz", "name": "Jane Doe", "age": 28, "email": "jane@example.com" }
  ]
}
Ready to add more endpoint types? The Guides section covers GET, POST, PUT, PATCH, DELETE, filtering, and sorting with complete examples.