Explore the World of UPS APIs

If you are searching for a reliable logistics and transportation partner, you must have heard about UPS – one of the world's largest package delivery companies. UPS not only offers an extensive range of shipping services but also provides developers with easy access to its shipping APIs through the UPS Developer Kit. With the UPS API, you can track packages, get shipping rates, print shipping labels, and much more.

In this blog post, we will walk you through the UPS API and demonstrate how to integrate it with the help of JavaScript.

Getting Started

Before we dive deep into the JavaScript code examples, let's first understand how to get started with the UPS API.

To use the UPS API, you need to get an access key from the UPS Developer Kit website. Once you create an account and log in, you can generate an access key by following these steps:

  1. Click on the "Get Keys" button on the main page.

  2. Select the "My UPS Account" option and provide your account information.

  3. Choose the services and API versions you want to access.

  4. Generate your access key.

Once you have obtained your access key, you can start using the UPS API.

JavaScript Code Examples

Here are some JavaScript code examples that illustrate how to use the UPS APIs.

1. Track a Package

const apiKey = "YOUR_API_KEY_HERE";
const trackingNo = "YOUR_TRACKING_NUMBER_HERE";

const url = `https://wwwcie.ups.com/track/v1/details/${trackingNo}?` +
  `access_license_number=${apiKey}`

fetch(url)
  .then((response) => {
    if (response.status !== 200) {
      console.log(`Error: ${response.status}`);
      return;
    }

    response.json().then((data) => {
      console.log(data);
    });
  })
  .catch((error) => {
    console.log(`Error: ${error}`);
  });

This code fetches the tracking details for a package using the UPS API. Replace the placeholders with your own API key and tracking number to test it out.

2. Get Shipping Rates

const apiKey = "YOUR_API_KEY_HERE";

const data = {
  "pickup_date": "20220609",
  "pickup_time": "1000",
  "documents_only": "1",
  "shipper": {
    "postal_code": "90210",
    "country_code": "US",
    "residential": false
  },
  "destination": {
    "postal_code": "10001",
    "country_code": "US",
    "residential": false
  },
  "packages": [
    {
      "weight": "10",
      "dimensions": {
        "length": "12",
        "width": "10",
        "height": "8"
      }
    }
  ]
};

fetch("https://wwwcie.ups.com/ship/v1/rating/rates", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "AccessLicenseNumber": apiKey
  },
  body: JSON.stringify(data)
})
.then((response) => {
  if (response.status !== 200) {
    console.log(`Error: ${response.status}`);
    return;
  }

  response.json().then((data) => {
    console.log(data);
  });
})
.catch((error) => {
  console.log(`Error: ${error}`);
});

This code fetches the shipping rates for a package using the UPS API. Replace the API key and package details with your own to test it out.

3. Print Shipping Label

const apiKey = "YOUR_API_KEY_HERE";
const shipmentId = "YOUR_SHIPMENT_ID_HERE";

const url = `https://wwwcie.ups.com/ship/v1/shipments/${shipmentId}/labels`;
const data = {
  "label_format_type": "GIF",
  "label_print_type": "STREAM",
  "label_delivery_method": "PRINT",
  "label_stock_size": {
    "height": "4",
    "width": "6",
    "units": "IN"
  }
};

fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "AccessLicenseNumber": apiKey
  },
  body: JSON.stringify(data)
})
.then((response) => {
  if (response.status !== 200) {
    console.log(`Error: ${response.status}`);
    return;
  }

  const blob = response.blob();
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = `label_${shipmentId}.gif`;
  document.body.appendChild(a);
  a.click();
})
.catch((error) => {
  console.log(`Error: ${error}`);
});

This code generates and downloads the shipping label for a package using the UPS API. Replace the placeholders with your own API key and shipment ID to test it out.

Conclusion

In conclusion, the UPS API is a powerful tool for developers who want to add shipping functionality to their applications. We hope these code examples have helped you understand how to integrate the UPS API with JavaScript. If you have any questions or suggestions, please leave them in the comments section.

Related APIs