0

I'm working on a project where I am using shippo to create shipping labels. Shippo has a webhook to allow customers to track their order statuses. I'm never used webhooks before, and I'm finding their documentation on how to set it up a bit confusing. They say to:

  1. Setup a webhook.
  2. POST the below to https://api.goshippo.com/tracks/
{
    "carrier": "usps",
    "tracking_number": "9102969010383081813033"
}

The issue that I'm having is that I'm unsure how to POST to a url in node. My project uses react in the frontend and node in the backend with axios being used to send my api request between the two.

I found this posting to a remote url with expressjs, but the answer is from 2013, so I wasn't sure if there have been any changes since, and in general, I wasn't sure if this solution would even work for what I'm asking. I would appreciate any help or advice on how to accomplish this. Thank you!

pelotador.1
  • 195
  • 1
  • 12
  • Does this answer your question? [How is an HTTP POST request made in node.js?](https://stackoverflow.com/questions/6158933/how-is-an-http-post-request-made-in-node-js) – juzraai Aug 07 '22 at 08:22

1 Answers1

0

You can post to an endpoint using the post() function in the axios module. Syntax:

const axios = require("axios"); // Commonjs
axios.post( // Send POST request
    "https://api.goshippo.com/tracks/" /*URL*/,
    JSON.stringify({"carrier": "usps","tracking_number": "9102969010383081813033"}) /*body*/,
    {
        "headers": {
            "content-type":"application/json", /*indicate that the body is of type json*/
            "authorization": "ShippoToken <API_TOKEN>" /*Your api key*/
        }
    }
).then((response)=>{
    console.log(response.data); // The response in object form
    // Do what you want with the data
}).catch((error)=>{
    console.error(error); // The request failed
})

Edit: Changed the code to include api key

Snurf08
  • 41
  • 1