DEV Community

Cover image for How to Use Firebase Functions With Express and Firestore for a Full Backend API
HexShift
HexShift

Posted on

1 1 1

How to Use Firebase Functions With Express and Firestore for a Full Backend API

If you're looking to deploy a full-featured serverless backend using Firebase, pairing Express with Firestore inside Firebase Cloud Functions is a flexible and powerful setup. This guide shows you how to deploy an Express API using Firebase Functions, backed by Firestore as your NoSQL database.

Step 1: Initialize Firebase Project

npm install -g firebase-tools
firebase login
firebase init functions

Choose:

  • Functions (yes to TypeScript or JavaScript)
  • Firestore if you want to test locally too
  • Install dependencies

Step 2: Set Up Express in Your Functions

// functions/index.js or functions/src/index.ts
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");

admin.initializeApp();
const db = admin.firestore();

const app = express();

app.get("/users", async (req, res) => {
  const snapshot = await db.collection("users").get();
  const users = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
  res.json(users);
});

app.post("/users", async (req, res) => {
  const user = req.body;
  const ref = await db.collection("users").add(user);
  res.status(201).json({ id: ref.id });
});

exports.api = functions.https.onRequest(app);

Step 3: Enable Firestore Rules

In firestore.rules, allow reads/writes for testing (don’t use in prod):

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Step 4: Deploy Your Backend

firebase deploy --only functions

You’ll get a URL like:

https://your-region-your-project.cloudfunctions.net/api/users

Step 5: Test It

# GET users
curl https://your-url/api/users

# POST user
curl -X POST https://your-url/api/users -H "Content-Type: application/json" -d '{"name":"Jane"}'

Conclusion

This setup gives you a scalable, serverless backend with full CRUD API capabilities, using Firebase’s managed services. You can layer on authentication, validation, and complex querying with minimal infrastructure concerns.

If this helped you, you can support my writing here: buymeacoffee.com/hexshift

Build fast. Secure faster. Bill instantly.

Build fast. Secure faster. Bill instantly.

Modern apps ship in hours, not sprints. Kinde gives you plug-and-play auth, RBAC, and billing that scale with you.

Get a free account

Top comments (0)

Developer-first embedded dashboards

Developer-first embedded dashboards

Embed in minutes, load in milliseconds, extend infinitely. Import any chart, connect to any database, embed anywhere. Scale elegantly, monitor effortlessly, CI/CD & version control.

Get early access

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay