This is in the context of a web firebase app using Stripe with firebase functions.
"dependencies": {
"stripe": "^17.2.0",
"twitter-api-v2": "^1.18.0",
"firebase-admin": "^12.4.0",
"firebase-functions": "^6",
"moment-timezone": "^0.5.45"
},
npm -g => [email protected]
All was working fine until I updated all packages to the version shown above.
Since then, when I deploy firebase functions (how come this error is not just a runtime error ??), I get error message:
Error: Neither apiKey nor config.authenticator provided
at Stripe._setAuthenticator (C:UserswebkoDocumentsAppsAndWebsitesNoderobotfunctionsnode_modulesstripecjsstripe.core.js:166:23)
at new Stripe (C:UserswebkoDocumentsAppsAndWebsitesNoderobotfunctionsnode_modulesstripecjsstripe.core.js:102:14)
at Stripe (C:UserswebkoDocumentsAppsAndWebsitesNoderobotfunctionsnode_modulesstripecjsstripe.core.js:58:20)
at Object.<anonymous> (C:UserswebkoDocumentsAppsAndWebsitesNoderobotfunctionsindex.js:37:33)
The line in question is this one:
const stripe = require('stripe')(process.env.STRIPE_SECRET);
Since the update, it refuses ‘process.env.STRIPE_SECRET’. But works fine when I hardcode the test key as string.
The secret IS set in the environement, and when I run:
firebase functions:secrets:access STRIPE_SECRET
It correctly shows the secret.
So why passing the secret as ‘process.env.STRIPE_SECRET’ has now become a problem ?
2
Answers
I solved the issue by initializing Stripe inside the Firebase Cloud Function, rather than at the file's top level with other global imports:
This approach seems necessary since Stripe version 17. While it used to work at the top level in earlier versions, this change actually aligns with best practices. It's more efficient to initialize Stripe only in functions that require it.
For those experiencing similar issues without Google Cloud Functions: check if the Stripe initialization is placed at the top level of your code. Moving it into the relevant function or method might resolve the problem.
Note: Always ensure your STRIPE_SECRET is securely stored in your environment variables.
Not sure why, but this is just happening to me with Stripe
16.9.0
, try downgrading to that version and see if it works.Also, I don’t think that’s related to Firebase, as I’m not using it.
PS: I found a related open issue.