Firebase gives you an optional 30 days in test mode when you create a Firestore database, but you must configure proper security rules before going to production. This guide walks through deploying Firestore security rules using firebase-tools and npx directly from your project.
Want an AI coding agent to handle this setup for you? Copy the ready-to-use prompt from Firebase & Firestore AI Prompts: Set Up Firestore Rules With Firebase CLI into Cursor, Claude Code, or Copilot and let your agent follow this exact process.
The Firebase Console has its own version history for rules, which is fine for quick fixes. If you are serious about your project (whether it is an Expo React Native app, a Next.js web app, or a backend service), managing rules from your codebase gives you Git history, local testing, and repeatable deploys.
If you are already familiar with Firebase setup, skip ahead using the table of contents.
Step 1: Download Your Google Service Account Key
First, download your Google Service Account JSON key from Firebase so your local tooling can authenticate with your project.
- Go to the Firebase Console.
- Click Project Settings (the gear icon) in the sidebar.
- Select the Service accounts tab at the top.
- Under Firebase Admin SDK, click Generate new private key.
- Save the downloaded
.jsonfile to your computer.
This is the same Google Service Account key used for EAS submit, FCM V1 push notifications, and
firebase-adminoperations. One key, many uses.
Handling the Key
Rename the downloaded file to google-service-account-key.json:
- Standard projects: Place it in your project root directory.
- Monorepos: Place it in your
backend/oradmin/directory, wherever your server-side code lives.
Caution: Never commit this file to GitHub. It contains private keys with full access to your database. Open
.gitignoreand addgoogle-service-account-key.jsonimmediately.
Setting the Path in .env
Create or update your .env file and add the path to the key:
FIREBASE_SERVICE_ACCOUNT_PATH=./google-service-account-key.json
This ensures your scripts and tools know where to find the key.
Step 2: Log In via Firebase CLI
Use npx to run the Firebase CLI without installing it globally, which avoids version conflicts across machines.
Open your terminal in your project root and run:
npx firebase login
This opens your browser so you can sign in with the Google account tied to your Firebase project.
If you manage multiple Firebase projects (such as staging and production), add aliases with
npx firebase use --add, name themstagingandproduction, and switch withnpx firebase use staging.
Step 3: Initialize Firestore
Tell Firebase that you want to manage security rules locally from this directory:
npx firebase init firestore
Follow the prompts:
- Use Spacebar to select
Firestore: Deploy rules and create indexesand press Enter. - Choose
Use an existing projectand select your project from the list. - Press Enter to accept the default filenames (
firestore.rulesandfirestore.indexes.json).
This creates two files in your project:
firestore.rules: Where you write your security rules.firebase.json: Tells the CLI where your rules and indexes live.
For full reference on managing and deploying rules, see the Firebase Rules Deployment docs.
Step 4: Write Your Starter Rules
Open firestore.rules and replace its contents with a clean starter template:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { // Default: Lock everything match /{document=**} { allow read, write: if false; } // System data: readable by all, writable only via Admin SDK match /app_config/{configId} { allow read: if true; allow write: if false; } // User data: only the owner can read/write their own document match /users/{userId} { allow read, write: if request.auth != null && request.auth.uid == userId; } } }
Always use
rules_version = '2', which supports collection group queries and modern security semantics. See Get Started with Firestore Security Rules for syntax details.
This starter covers two core patterns: public system data and private user data. For deeper patterns, validation, and third-party auth bridges, read Mastering Firestore Security Rules.
Step 5: Deploy Your Rules
Push your local firestore.rules to Cloud Firestore:
npx firebase deploy --only firestore:rules
Pro Tip: Add this to package.json for fast future deploys:
"scripts": { "deploy:rules": "npx firebase deploy --only firestore:rules" }
Now you can run npm run deploy:rules whenever your rules change.
Safety Notes
- CLI Overwrites Console: Deploying from the CLI replaces whatever rules are currently in the Firebase Console. Pull any manual console edits into your local file first.
- Admin SDK Bypasses Rules:
firebase-adminand the CLI bypass security rules. These rules protect your database from direct client-side access. - Test First: Use the Rules Simulator in the Firebase Console before deploying to production.
Next Steps
Learn how to structure complex rules and data validation in Mastering Firestore Security Rules, or skip firebase-tools entirely in Deploy Firestore Rules With Firebase Admin SDK.
Insights, Playbooks, Tips & Kits
Want real patterns from my journey building and selling SaaS & apps. DIY playbooks, code-ready starter kits, and the operational moves that let you scale without the overhead. For builders who learn best from firsthand mistakes, real wins & experience.




