Firebase .env Generator
Firebase has two halves: the public client config (safe in the browser) and the Admin SDK service account (server-only). This generator keeps them separated with the right NEXT_PUBLIC_ prefixes so you don't leak admin credentials.
NEXT_PUBLIC_FIREBASE_API_KEY= NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN= NEXT_PUBLIC_FIREBASE_PROJECT_ID= NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET= NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID= NEXT_PUBLIC_FIREBASE_APP_ID= FIREBASE_ADMIN_PROJECT_ID= FIREBASE_ADMIN_CLIENT_EMAIL= FIREBASE_ADMIN_PRIVATE_KEY=
Secrets regenerate on every page load. Want to combine stacks? Use the full generator.
How to use this .env in Firebase
- 1.Grab your client config from Firebase Console → Project Settings → General.
- 2.Grab the Admin SDK service account from Firebase Console → Project Settings → Service Accounts → Generate new private key.
- 3.Paste the two blocks into .env or .env.local.
- 4.FIREBASE_ADMIN_PRIVATE_KEY usually contains newlines — wrap in double quotes and keep \n escapes.
Gotchas
- ⚠The private key contains literal \n sequences — JSON.parse the wrapped version at runtime, or replace(/\\n/g, '\n') before passing to cert().
- ⚠NEXT_PUBLIC_FIREBASE_API_KEY being exposed is fine — it's not a secret, it identifies the project.
- ⚠Enable App Check in production to stop abuse since the client key is public.
Common keys explained
NEXT_PUBLIC_FIREBASE_API_KEYClient config. Safe to expose — Firebase security rules protect data.
NEXT_PUBLIC_FIREBASE_AUTH_DOMAINUsed by the Firebase Auth SDK for OAuth redirects.
NEXT_PUBLIC_FIREBASE_PROJECT_IDNeeded by both client and Admin SDK.
FIREBASE_ADMIN_CLIENT_EMAILService-account email. Server-only.
FIREBASE_ADMIN_PRIVATE_KEYService-account RSA key. Server-only. Quote it.
Loading and validating the .env in Firebase
import { cert, getApps, initializeApp } from "firebase-admin/app";
// The private key is multi-line; .env stores it with literal \n
const privateKey = process.env.FIREBASE_ADMIN_PRIVATE_KEY?.replace(/\\n/g, "\n");
export const adminApp =
getApps()[0] ??
initializeApp({
credential: cert({
projectId: process.env.FIREBASE_ADMIN_PROJECT_ID,
clientEmail: process.env.FIREBASE_ADMIN_CLIENT_EMAIL,
privateKey,
}),
});Firebase: env vars in development, staging and production
The Firebase web config (apiKey, authDomain, projectId and so on) identifies your project and is meant to be public. It ships in every Firebase web app. What protects the data is Security Rules and App Check, not hiding these values. That is why they carry NEXT_PUBLIC_ in the template.
The Admin SDK credentials (service account email and private key) are a different matter. They bypass all Security Rules. Use separate Firebase projects for development and production, each with its own service account, so a leaked dev key cannot touch production data.
Firebase: .env errors and fixes
Failed to parse private key: Error: Invalid PEM formatted messageThe newlines in FIREBASE_ADMIN_PRIVATE_KEY were lost. Keep the key on one line in double quotes with literal \n, and replace them as shown above.
FirebaseError: Firebase: Error (auth/invalid-api-key)NEXT_PUBLIC_FIREBASE_API_KEY is empty in the client bundle. Set it before building, because public variables are inlined at build time.
Credential implementation provided to initializeApp() failedThe client email and private key come from different service accounts, or the key was revoked. Download a fresh JSON key in Project settings > Service accounts.
Firebase .env FAQ
Is the Firebase apiKey a secret?
No. It identifies the project for Google's APIs. Restrict it to your domains in Google Cloud Console and enforce access with Security Rules.
Can I use the service account JSON file instead of three variables?
Yes. Set GOOGLE_APPLICATION_CREDENTIALS to the file path and call initializeApp() without arguments. Separate variables are easier on hosts like Vercel that have no file system for secrets.
Do Cloud Functions need these variables?
No. Functions running in the same project get default credentials automatically. Use defineSecret() for third-party secrets in functions.