How to Setup Firebase Rules with Firebase tools and cli

How to Set Up Firestore Security Rules With Firebase CLI

Deploy Firestore security rules from your codebase using Firebase CLI and npx firebase-tools. Includes service account setup, testing, and deploy scripts.

Muhammad Hassaan
Muhammad HassaanMH Labs
•5 min read

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.

  1. Go to the Firebase Console.
  2. Click Project Settings (the gear icon) in the sidebar.
  3. Select the Service accounts tab at the top.
  4. Under Firebase Admin SDK, click Generate new private key.
  5. Save the downloaded .json file to your computer.

This is the same Google Service Account key used for EAS submit, FCM V1 push notifications, and firebase-admin operations. 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/ or admin/ 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 .gitignore and add google-service-account-key.json immediately.

Setting the Path in .env

Create or update your .env file and add the path to the key:

bash
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:

bash
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 them staging and production, and switch with npx firebase use staging.

Step 3: Initialize Firestore

Tell Firebase that you want to manage security rules locally from this directory:

bash
npx firebase init firestore

Follow the prompts:

  1. Use Spacebar to select Firestore: Deploy rules and create indexes and press Enter.
  2. Choose Use an existing project and select your project from the list.
  3. Press Enter to accept the default filenames (firestore.rules and firestore.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:

javascript
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:

bash
npx firebase deploy --only firestore:rules

Pro Tip: Add this to package.json for fast future deploys:

json
"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-admin and 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.