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

Apr 21, 2025 - 04:14
 0
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