4

I am trying to implement Stripe in my backend which is in node js. While calling it from front end I am getting this error:

You did not provide an API key, though you did set your Authorization header to "null". Using Bearer auth, your Authorization header should look something like 'Authorization: Bearer YOUR_SECRET_KEY'. See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/.

Here is my code:

const stripe = require("stripe")(process.env.keySecret);

if(req.body.type == 'stripe') {
        const token = req.body.token;
        const amount = req.body.amount;
        let charge = stripe.charges.create({
            amount: amount,
            source: token,
            currency: 'USD'
        },(err, chargeData) => {
            if(err) {
              console.log('error in buy premium api error... ', err);
              return;
            }
            let payment = [];
            payment.push({
              amount: chargeData.amount,
              address: chargeData.billing_details.address,
              email: chargeData.billing_details.email,
              payment_method_details: chargeData.payment_method_details.card
            })
            console.log('charge... ', charge);
            if(charge) {
              register.findByIdAndUpdate({ _id: therapist._id }, 
                {
                  $set:
                  {
                    payment_details: payment
                  }
                }, {new:true}, (e1, updatedUser) => {
                if(e1) {
                  return;
                }
  
                resolve(updatedUser);
              })
            }
        })
      }
      else {
        let err = 'No payment type define..';
        return reject(err);
      }

My req.body is:

{
"token": "tok_1F2XPBG04BYg8nGtLoWnQwRU", // coming from front end
"amount": "250",
"type":"stripe" 
}

The secret key is test secret key which is like sk_test_xxxxxxxxxxxxxxxx.

My front end is in Angular 6 and from front end I am passing my test publishable key i.e. pk_test_xxxxxxxxxxxxxxxxx. I am passing proper data to the both front end as well as backend. Where am I mistaken?

4
  • Are you sure that process.env.keySecret contains the security token (the one starting with sk_test_)? In order for this to work you have to create an environment variable called "keySecret". Commented Aug 1, 2019 at 9:01
  • Yes.. It is test secret key start with "sk_test_xx" and I made environment variable called "keySecret" Commented Aug 1, 2019 at 9:17
  • Hi @JaynaTanawala did you figure this issue out? I'm having a similar problem.. Commented Apr 16, 2020 at 18:01
  • 1
    @YuriyF, yes it was key issue.. I given wrong key.. Commented Apr 23, 2020 at 11:33

1 Answer 1

1

Since you are calling .env file, you have to include the following line before requiring stripe:

require ("dotenv").config();
const stripe = require("stripe")(process.env.keySecret);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.