This open-source project demonstrates how to manage a Firebase project as an API provider, with a focus on secure JWT validation using Firebase Authentication. It is intended as a reference for building secure, scalable backend APIs with Firebase Functions, protected by Firebase Auth.
- Firebase Functions as API: Exposes RESTful endpoints using Express and Firebase Cloud Functions.
- JWT Validation: Uses Firebase Authentication to validate JWTs for protected API routes.
- Secret Management: Demonstrates secure use of API keys and secrets via Google Cloud Secret Manager.
- Middleware: Includes reusable middleware for authentication and logging.
- Local Development Support: Easily run and test APIs locally using Firebase Emulators.
- /functions: Source code for Cloud Functions (API endpoints, middleware, services).
- /public: Static assets for Firebase Hosting (optional).
- /docs: Documentation and setup notes.
-
Clone the repository
git clone https://github.com/YOUR_USERNAME/firebase-api-sample.git cd firebase-api-sample -
Install dependencies
cd functions npm install -
Set up Firebase project
- Create a Firebase project in the Firebase Console.
- Update your
.envor use Firebase CLI to set project ID.
-
Local Development
-
Start the Firebase emulators for local API testing:
npm run serve
-
Or, use the full emulator suite:
firebase emulators:start
-
-
Deploy to Firebase
firebase deploy --only functions
| Command | Description |
|---|---|
npm run lint |
Lint the codebase |
npm run build |
Compile TypeScript to JavaScript |
npm run serve |
Build and start local emulator for functions |
firebase emulators:start |
Start all Firebase emulators |
firebase deploy --only functions |
Deploy only Cloud Functions |
- Protected endpoints require a valid Firebase ID token in the
Authorizationheader:
Authorization: Bearer <ID_TOKEN> - Middleware verifies the token and attaches the decoded user info to the request.
- API keys and sensitive values should be stored in Google Cloud Secret Manager.
- See
/docs/commands.mdfor commands to grant secret access.
- Firebase Functions Documentation
- Firebase Authentication
- Firebase Emulator Suite
- Google Cloud Secret Manager
This project is intended for demonstration and educational purposes.