Search code examples
easypost

Nodejs Pagination using Shipment.all and Tracker.all


Appolgies i am quite new to Nodejs and programming, so Long story short:

Using the Easypost Nodejs API, I need to retrieve all Tracking Status 3 times a day using my Nodejs/express backend.

I hit a rate limit on my very inefficient way of retrieving all my tracking status'. Which is a seperate call for each Shipmentid to get the Trackingid which then gets the most recent status. Horrendously bad i know.

So i went to the Docs and thought, cool i'll do things seperately, get all my packages (myDB) then seperately get a list of all trackers then match up using shipment ID.

Problems...

  1. i'm a newbie.

  2. how to do multiple calls with pagination -> my first thoughts are to run an initial call to see if there are multiple pages, if "has_more" is true in then run as many times as needed on as many pages as i need for the last 2 months.

  3. next problem... i seem to be missing the paging information in the response from the easypost API. In the docs there should be a response of {trackers:[all tracker info here], "has_more": true} but there is is only an array of trackers in the response. even if i set the page size to 1 or 2...

Current code for getting the list of trackers:

      .get((req, res) => {
        console.log('Recieved - ' + req.method + req.originalUrl)
        easyPostAPI.Tracker.all(req.body)
          .then( response => {
            console.log('Sent - ' + req.method + req.originalUrl)
            res.json(response)
          })
          .catch(err => {
            console.log(err)
            res.status(400).json(err)
          })
      })```


hopefully it's not just me getting it wrong.



Solution

  • EasyPost just released a 3.8.0 version of the node library in which you can access the has_more property on the array, like so:

    api.Shipment.all({ page_size: 2, ... }).then(shipment => console.log(shipment.has_more))
    

    In @easypost/api v4.0.0 there is likely to be a more convenient way to paginate, but for now, the above property should allow you to paginate.