Firebase auth/invalid-api-key Error
Quick answer
Firebase got an empty or malformed apiKey in initializeApp. In framework apps this is nearly always an environment variable that is undefined at build or run time, for example missing the NEXT_PUBLIC_ prefix or not set in the hosting platform.
Most likely fix: Check the key actually reaches the app
How to fix it
Try these in order — the most common fix is first.
1. Check the key actually reaches the app
- console.log(process.env.NEXT_PUBLIC_FIREBASE_API_KEY) (or import.meta.env for Vite) and confirm it is not undefined
- Use the exact prefix your framework exposes to the browser
Did this fix it?2. Restart and rebuild
- Stop the dev server and start it again after editing .env
- Redeploy after changing host env vars; build-time variables are baked into the bundle
Did this fix it?3. Set variables on your host
- Add every firebaseConfig value in your hosting dashboard's environment settings
- Remove quotes, trailing spaces and stray parentheses
Did this fix it?4. Copy a fresh config
- Firebase console > Project settings > Your apps > SDK setup and configuration
- Paste the config again and compare apiKey
Did this fix it?
Symptoms
Uncaught FirebaseError: Firebase: Error (auth/invalid-api-key), often only on the deployed site or after a rebuild.
Why it happens
- Env variable lacks the framework's public prefix (NEXT_PUBLIC_, VITE_)
- Dev server not restarted after editing .env
- Variables set locally but not in Vercel, Netlify or Cloudflare settings
- Stray characters or quotes in the key value
- Wrong casing of the variable name
This guide also covers
- auth/invalid-api-key
- firebase invalid api key next.js
- firebase api key undefined
Still not working?
If Firebase keeps failing, you're not alone. See every Firebase problem users report or the alternatives people switch to.
Have an app idea? Our small team builds any software: web and mobile apps, games, desktop, AI tools. Price agreed together.
Talk to usThe same problem in other apps
All not loading, error messages & connection guides →
- Supabase Max Client Connections Reached (Supabase)
- MongoDB Atlas Connection Timed Out (IP Not Whitelisted) (MongoDB Atlas)
Other Firebase errors
- Firestore Missing or Insufficient Permissions (permission-denied)
- Firebase auth/network-request-failed Error
- Firebase auth/too-many-requests Error
- Firebase auth/operation-not-allowed Error
- Firestore Quota Exceeded (resource-exhausted)
- Firebase Unexpected Bill on Blaze Plan
- Firebase Cloud Messaging Notifications Not Received
- Firebase Deploy Failed (Functions and Hosting)