Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Payments and webhooks

firebase deploy fails because the Stripe client has no API key

ErrorNeither apiKey nor config.authenticator provided

Short answer

The Stripe client is built from a key that is empty at that moment. Firebase loads your code during deploy, when secrets and parameters are not available, so a new Stripe(key) at the top of the file receives undefined. Create the client inside the function, bind the secret to the function with the secrets option, and set the secret before you deploy.

  • Applies to firebase-functions v2 API, firebase-tools, Node.js 22, stripe-node 17 or later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the Stripe client is created and what key it reads. A client created at module level, outside any function, is the first suspect.

    grep -rn "new Stripe\|Stripe(" functions/src functions/index.js 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The failure happens during firebase deploy, before any request is served.The client is created in global scope, before the key existsFix
    2. 2Deploy succeeds, then calling the function throws the same error, or stripeKey.value() is an empty string.The secret is not bound to the functionFix
    3. 3Secret binding is in place, the value is still empty.The secret was never set, or was set after the last deployFix

What does the error look like?

  • Neither apiKey nor config.authenticator provided

Cause 1 of 3

The client is created in global scope, before the key exists

1Check

Firebase documents that secret values are hidden until the function executes, and that reading parameter values in global scope causes a deployment failure. stripe-node also fails when the file that creates the client is imported while the key is not there, for instance during a build step.

sed -n 1,20p functions/src/index.ts

2Fix

Create the client lazily, on first use inside the function. For initialisation that depends on a parameter, Firebase recommends onInit().

import Stripe from 'stripe';
import { defineSecret } from 'firebase-functions/params';
import { onRequest } from 'firebase-functions/v2/https';

const stripeKey = defineSecret('STRIPE_SECRET_KEY');
let stripe: Stripe | undefined;
const getStripe = () => (stripe ??= new Stripe(stripeKey.value()));

export const createCheckout = onRequest({ secrets: [stripeKey] }, async (req, res) => {
  const session = await getStripe().checkout.sessions.create({ /* ... */ });
  res.json({ id: session.id });
});

3Verify

Deploy again; the load step passes.

firebase deploy --only functions

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 3

The secret is not bound to the function

1Check

A function reads a secret only when it is listed in that function's secrets option. A function that is missing from the list sees nothing, so each Stripe function needs its own binding.

grep -n "secrets:" functions/src/*.ts

2Fix

Add the secret to the options of every function that uses Stripe.

export const stripeWebhook = onRequest({ secrets: [stripeKey] }, handler);

3Verify

Redeploy, call the function and read its log in the Firebase console; no missing-key error appears.

firebase deploy --only functions

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

The secret was never set, or was set after the last deploy

1Check

Create the secret with the CLI. Firebase notes that after you set a value you must deploy the functions that use it.

grep -rn "STRIPE_SECRET_KEY" functions/src

2Fix

Set the secret, then deploy the functions that use it.

firebase functions:secrets:set STRIPE_SECRET_KEY
firebase deploy --only functions

3Verify

Call the function again.

firebase deploy --only functions

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Keep the Stripe key in Secret Manager through defineSecret, not in .env files (they are not a secure place for secrets) or in code.
  • Do not use functions.config(): it is deprecated and Firebase says deploys that use it will fail after March 2027. Export it with firebase functions:config:export.
  • Create third-party clients lazily so a build or deploy step can import the file without the key.

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

Payments

Stripe Elements shows console warnings or errors in a Next.js checkout

Could not find Elements context; You need to wrap the part of your app that calls useElements() in an <Elements> provider.

Most React Stripe.js warnings come from four mistakes: calling loadStripe inside a component, using useStripe or useElements outside the Elements provider, using the objects before they have loaded, and a Content Security Policy that blocks js.stripe.com.

4 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.