skip to Main Content

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


  1. Chosen as BEST ANSWER

    I solved the issue by initializing Stripe inside the Firebase Cloud Function, rather than at the file's top level with other global imports:

    exports.myFunction = functions.https.onCall(async (data, context) => {
      const stripe = require('stripe')(process.env.STRIPE_SECRET); // Works
          // Function code here
    });
    
    
    
    const stripe = require('stripe')(process.env.STRIPE_SECRET); // Does NOT Work
    
    exports.myFunction = functions.https.onCall(async (data, context) => {
          // Function code here
    });
    
    

    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.


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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search