Skip to content

Firebase Auth errorFirebase: “auth/invalid-api-key”, but only in production

Sign-in works on your machine, then the deployed app throws auth/invalid-api-key the moment it loads. Firebase didn’t get a key it recognises: usually the production build was made without the environment variable that holds it. Here’s what to check, in Firebase’s and Vite’s words, or hand it over at a fixed price.

Short answer

The deployed build has no Firebase API key, or the wrong one. Add the key to your host’s production environment variables using the prefix your framework exposes to the browser (VITE_ for Vite, NEXT_PUBLIC_ for Next.js, EXPO_PUBLIC_ for Expo), then rebuild and redeploy, since the value is baked in at build time. Copy it from the web app’s config in the Firebase console.

Why the live build has no valid key

  1. 01

    The variable isn’t set on the host

    “Thrown if the provided API key is invalid. Please check that you have copied it correctly from the Firebase Console.” Firebase, firebase.auth.Error

    Firebase’s reference says to check you copied the key correctly, and a missing variable is the usual way that goes wrong: your .env file isn’t deployed. Add the same variables in Vercel, Netlify, EAS or your host’s settings for the production environment, not only for previews, then trigger a new build.

  2. 02

    The variable has the wrong prefix

    “Variables prefixed with VITE_ will be exposed in client-side source code after Vite bundling.” Vite, Env variables and modes

    Bundlers only put variables with their public prefix into browser code. A Vite app reading FIREBASE_API_KEY gets undefined; it needs VITE_FIREBASE_API_KEY and globalThis._importMeta_.env.VITE_FIREBASE_API_KEY. Next.js uses NEXT_PUBLIC_ and Expo uses EXPO_PUBLIC_.

  3. 03

    The value is mangled or from another project

    Quote marks, a trailing space or a line break pasted into the host’s settings make the key invalid. So does a key from a dev Firebase project paired with production’s other config values. Copy the whole firebaseConfig for the right project and app.

  4. 04

    Don’t hide the key: it’s public by design

    “API keys restricted to Firebase services do not need to be treated as secrets, and it's safe to include them in your code or configuration files.” Firebase, Learn about using and managing API keys for Firebase

    Some builders hide the key, or leave it out, because it looks like a secret. Firebase’s web API key is meant to ship in the app; your data is protected by Security Rules and App Check, so put the rules right instead of hiding the key.

Checked against the vendor’s docs on : Firebase reference: firebase.auth.Error; Firebase docs: API keys for Firebase; Vite docs: Env variables and modes.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: Vercel environment variables undefined · Firebase No Firebase App [DEFAULT] has been created · Firebase auth/unauthorized-domain · Firebase phone auth invalid-app-credential

Questions

More on this error

How do I check what the live app actually got?

Open the deployed site, then the browser’s developer tools, and search the loaded JavaScript for your project ID. If the apiKey next to it is empty or says undefined, the build didn’t get the variable.

Should I restrict the Firebase API key?

Firebase says its provisioned keys are automatically restricted to Firebase-related APIs. Keep it that way, and use a separate key for anything else, such as Maps or the Gemini API.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; we reply by email, usually within one business day.