Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Firebase Auth says the API key is not valid

ErrorFirebase: 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.

  • Applies to Firebase JS SDK 11, Next.js 15, Vite 6, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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()
  2. 2Match what you see, then jump to its fix

    1. 1The log prints undefined, but the .env file does have the key.The env variable is not exposed to browser codeFix
    2. 2The 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. 3The logged key is present but does not match your Firebase project.The key is wrong or from another projectFix
    4. 4The 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

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/null

2Fix

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_KEY

3Verify

Restart the server and check that the console log now shows the key.

npm run dev

Confirmed by nextjs.org

Did this fix it?

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 build

3Verify

Open the deployed site, run the same console.log and check that the key is present.

console.log(firebaseConfig.apiKey)

Confirmed by nextjs.org

Did this fix it?

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_ID

2Fix

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

Did this fix it?

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 restrictions

2Fix

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 restrictions

3Verify

Try to sign in again after saving the key.

signInWithEmailAndPassword(auth, email, password)

Confirmed by firebase.google.com

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.