Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)Short answer
The apiKey your app sends is either empty, wrong, or blocked. The most common reason is an environment variable the bundler never exposed to the browser, so the key is undefined. Print the config, compare the key with the Firebase console, then check the key's API restrictions.
Fix it fast
1Find out which cause you have
Print the config your app really sends. A key that is undefined or empty in the browser means an env variable problem; a full key means look at the key itself.
console.log('apiKey:', firebaseConfig.apiKey) // put this next to initializeApp()2Match what you see, then jump to its fix
- 1
The log prints undefined, but the .env file does have the key.The env variable is not exposed to browser codeFix - 2
The value works locally but is undefined on the deployed site.The .env file is in the wrong place or the app was built before it changedFix - 3
The logged key is present but does not match your Firebase project.The key is wrong or from another projectFix - 4
The key matches, but requests fail with API_KEY_SERVICE_BLOCKED or a 403 saying the API is blocked.The key is restricted and Authentication is not on its listFix
- 1
What does the error look like?
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
Cause 1 of 4
The env variable is not exposed to browser code
1Check
Next.js only exposes variables that start with NEXT_PUBLIC_, and inlines them into the bundle at build time. Vite only exposes variables that start with VITE_ and reads .env files when the server starts.
grep -n "API_KEY" .env .env.local 2>/dev/null2Fix
Rename the variable with the right prefix, read it the way the framework expects, and restart the dev server or rebuild.
# Next.js: .env.local
NEXT_PUBLIC_FIREBASE_API_KEY=your-key
// code
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY
# Vite: .env
VITE_FIREBASE_API_KEY=your-key
// code
apiKey: import.meta.env.VITE_FIREBASE_API_KEY3Verify
Restart the server and check that the console log now shows the key.
npm run devConfirmed by nextjs.org
Cause 2 of 4
The .env file is in the wrong place or the app was built before it changed
1Check
Next.js loads .env files from the project root, even when you use a src folder. Public variables are fixed at next build, so changing them afterwards has no effect on the built app.
ls -a | grep "^.env"2Fix
Put the file in the project root, set the variable in the environment where the build runs, and build again.
NEXT_PUBLIC_FIREBASE_API_KEY=your-key npm run build3Verify
Open the deployed site, run the same console.log and check that the key is present.
console.log(firebaseConfig.apiKey)Confirmed by nextjs.org
Cause 3 of 4
The key is wrong or from another project
1Check
Look up the key for your web app, either in the Firebase console under Settings, General, Your apps, or with the CLI.
firebase apps:sdkconfig WEB YOUR_FIREBASE_APP_ID2Fix
Copy the apiKey from the config for the same project you use for Authentication.
const firebaseConfig = {
apiKey: '<from the console>',
authDomain: 'your-project.firebaseapp.com',
projectId: 'your-project',
};3Verify
Retry sign-in.
signInWithEmailAndPassword(auth, email, password)Confirmed by firebase.google.com
Cause 4 of 4
The key is restricted and Authentication is not on its list
1Check
In Google Cloud console, open APIs & Services, Credentials, and open the key. A restricted key only works for the APIs on its allowlist.
# Google Cloud console > APIs & Services > Credentials > your key > API restrictions2Fix
Add the Identity Toolkit API (identitytoolkit.googleapis.com) and the Token Service API (securetoken.googleapis.com) to the allowlist.
# add identitytoolkit.googleapis.com and securetoken.googleapis.com to the key's API restrictions3Verify
Try to sign in again after saving the key.
signInWithEmailAndPassword(auth, email, password)Confirmed by firebase.google.com
How do I stop it coming back?
- Log the config once during setup so a missing value is seen on day one.
- Keep the variable names in a checked-in .env.example.
- When you add a Firebase service, add its API to the key's allowlist.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Google sign-in popup blocked by Cross-Origin-Opener-Policy
The page loses its reference to the sign-in popup window, because the opener policy severs it
If your site sends Cross-Origin-Opener-Policy: same-origin, a sign-in popup from another origin lands in a separate browsing context group, so your page cannot reach it.
3 causes, each with a fixWeb apps
FirebaseError: Expected first argument to collection() to be a CollectionReference
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
In the modular Firebase SDK, collection() is a function whose first argument must be the Firestore instance from getFirestore(app).
2 causes, each with a fix