Chapter 12

Integrating a Carrier

Carrier integration is essential for tracking the status of a package. Whether your customers are using USPS, UPS, FedEx, or another shipping carrier, the goal is to provide real-time updates on their package status. However, each carrier varies in its API implementation, requiring different authorization schemes and sometimes complex access protocols to retrieve tracking information.

Tracking integration typically serves one primary purpose: Given a tracking number, provide real-time status updates of the package. However, extracting detailed tracking history, such as the ship date, transit statuses, and expected delivery date, can provide valuable insights. This information helps your chatbot respond to customers in a more informed and precise manner, and the Company Policy Processor can use it to guide responses based on the current state of the shipment.

Example with UPS

Let’s take UPS as an example to demonstrate how you might implement a generic JavaScript integration to retrieve tracking information.

UPS provides a well-documented API, but you’ll need an API key and authentication setup to access it. Once authenticated, you can query their API to retrieve the package status and history.

Here’s a simple implementation of a function to get tracking information from UPS:

const axios = require('axios');

const UPS_API_URL = 'https://onlinetools.ups.com/track/v1/details';  
const UPS_ACCESS_KEY = 'your_access_key';  
const UPS_USERNAME = 'your_username';  
const UPS_PASSWORD = 'your_password';

// Function to get tracking info from UPS  
async function getTrackingInfo(trackingNumber) {  
    try {  
        const response = await axios.post(UPS_API_URL, {  
            "UPSSecurity": {  
                "UsernameToken": {  
                    "Username": UPS_USERNAME,  
                    "Password": UPS_PASSWORD  
                },  
                "ServiceAccessToken": {  
                    "AccessLicenseNumber": UPS_ACCESS_KEY  
                }  
            },  
            "TrackRequest": {  
                "Request": {  
                    "RequestOption": "1"  
                },  
                "InquiryNumber": trackingNumber  
            }  
        }, {  
            headers: {  
                'Content-Type': 'application/json'  
            }  
        });

        const trackingData = response.data.TrackResponse.Shipment;  
        return parseTrackingData(trackingData);  
    } catch (error) {  
        console.error('Error fetching tracking info:', error);  
        throw error;  
    }  
}

// Helper function to parse tracking data  
function parseTrackingData(trackingData) {  
    const status = trackingData.Package.Activity[0].Status.Description;  
    const shipDate = trackingData.ShipmentInformation.ShipDate;  
    const lastScan = trackingData.Package.Activity[0].Date;  
    const expectedDeliveryDate = trackingData.ScheduledDeliveryDate;  
    const transitHistory = trackingData.Package.Activity.map(activity => ({  
        status: activity.Status.Description,  
        date: activity.Date,  
        location: `${activity.ActivityLocation.City}, ${activity.ActivityLocation.StateProvinceCode}`  
    }));

    return {  
        status,  
        shipDate,  
        lastScan,  
        expectedDeliveryDate,  
        transitHistory  
    };  
}

// Example usage  
(async () => {  
    const trackingNumber = '1Z12345E0205271688'; // Example tracking number

    const trackingInfo = await getTrackingInfo(trackingNumber);  
    console.log('Tracking Info:', trackingInfo);  
})();

Key Information to Retrieve

The core information we want to retrieve from the UPS tracking API (or any carrier API) includes:

  • Ship Date: The date the package was shipped.
  • Tracking Status: Whether the package is in transit, delivered, or another state (e.g., label created).
  • Last Scan: The most recent scan or checkpoint the package passed through.
  • Expected Delivery Date: The estimated date the package is expected to arrive.
  • Transit History: A detailed history of the package's journey, including each checkpoint it passed through.

Enhancing the Company Policy Processor

The information retrieved from the carrier can directly influence how your chatbot responds to customer inquiries. For example, if a package is delayed and hasn't moved in several days, the Company Policy Processor might escalate the issue to a human agent or provide the customer with a more detailed update.

This integration allows for more personalized and relevant responses, helping to reassure customers during their wait times and giving them accurate information based on real-time tracking updates.

By integrating with different carriers’ APIs, you ensure that your chatbot can handle various tracking requests, no matter which service was used to ship the order. This flexibility is key to delivering a consistent customer experience across all orders.

← CH11: Integrating an eCommerce Platform
CH13: Designing a Company Policy Processor →