Home > Mobile >  Error in Axios API Post request in Nodejs
Error in Axios API Post request in Nodejs

Time:01-10

I am trying to make an API call to Paystack API. I am using Axios package for node.js. I can't seems to get it to work.

Here is my code:

  const response = await axios({
    method: 'post',
    url:'https://api.paystack.co/transaction/initialize',
    data: paymentDetails,
    headers : {
        Authorization: 'mysecretKey',
          'content-type': 'application/json',
          'cache-control': 'no-cache'
      },
})
return res.json(response)

On postman, I simply provided the payment details via the body as requested by Paystack:

"id": 1,
"username": "kings",
"email": "emailaddress",
"amount": 100

I am getting this error: AxiosError: Request failed with status code 401

CodePudding user response:

You probably have the Authorization wrong.

Try this:

const response = await axios({
    method: 'post',
    url:'https://api.paystack.co/transaction/initialize',
    data: paymentDetails,
    headers : {
        Authorization: `Bearer ${mysecretKey}`,
          'content-type': 'application/json',
          'cache-control': 'no-cache'
      },
})
return res.json(response)

Notice the Authorization: Bearer {secret}

https://paystack.com/docs/api/#authentication

  • Related