0
votes

I am working with the Easy Post API (Node.js) and I am able to trigger the API to call and respond with a single tracking number.

How do I run multiple tracking numbers through the API?

All help is greatly appreciated.

Node.js

    app.get("/api/tracking/retrieve", (req, res) => {

    const apiKey = 'My_API_Key';
    const Easypost = require('@easypost/api');
    const api = new Easypost(apiKey);

Test Parameters (Provided By Easy Post)

    tracking = ['EZ6000000006', 'EZ6000000006'];
    carrier = ['UPS', 'UPS'];

Tracker Object (Provided By Easy Post)

        const tracker = new api.Tracker({

        tracking_code: tracking,
        carrier: carrier
    });

tracker.save().then(console.log);

})
}
2

2 Answers

1
votes

If I understand your question correctly, you need to call new api.Tracker({...}).save... in a loop:

const Easypost = require('@easypost/api');
const api = new Easypost('<YOUR_TEST/PRODUCTION_API_KEY>');

const trackingCodes = ['9400110898825022579493', '9400110898825022579494'];

trackingCodes.forEach(trackingCode => {
  const tracker = new api.Tracker({
    tracking_code: trackingCode,
    carrier: 'USPS',
  });

  tracker.save().then(console.log);
});


0
votes

@kfunk This worked, but I will look into the other method for the alternative format you suggested.

Here's the code:

  `trackingCodes.forEach((trackingCode) => {
        carrierCodes.forEach((carrierCode) => {
            const tracker = new api.Tracker({
                tracking_code: trackingCode,
                carrier: carrierCode
            });

            tracker.save().then(console.log);

        });
    });`