Package Tracking with Easypost Java API

blog-banner

There are number of courier companies in US which provide service to customers to send packages from one location to another location. Some of shipping companies are FedEx, UPS, DHL Express, USPS, etc. They also provide tracking service to customer to track their order/package. There are number of third party vendors available too, which provides package tracking service by their well defined API. Some of them are easypost, aftership, stamps, endicia, etc.

Easypost provides well defined and easy to understand API. Using API you can create shipping label and add tracking programmatically in package without worrying about to go at shipping site and get label. If you run a business like courier or sending customer stuff from one location to another and you want to allow your customer to track their packages then you are at the right place. You can implement tracking feature for your website and mobile application using easypost API . In this article you can see how to add tracking in package using Easypost Java API.

Implementation  :

To get shipping label for your package using API follow below functional diagram. Diagram represents steps to generate shipping label for package tracking.

Step 1 : Create ‘To Address’ and ‘From Address’, easypost also provides address verification process to verify addresses before generating shipping label.

Step 2 : After successfully verifying addresses there will be step to create Parcel. To create Parcel there are some properties need to be set i.e. weight, height, etc. Easypost provides predefined parcels which have predefined dimensions and weight i.e. Card, Letter, Flat, Parcel, etc. Select one which qualifies your requirement while creating parcel.

Steps 3 : Create shipment using combination of To address, From address and Parcel. On successful shipment creation, it returns a set of rate for various services i.e. First Class, Priority, etc.

Steps 4 : Buy shipment for particular rate and get shipping label link.

Steps 5 : Download shipping label from the provided link, print it out and put it on your package. It also provide tracking ID with shipping label link to track package.

Below is the sample JAVA program for the same.

  • SamplePackageTracking.java

package com.st.easypost.web;

import com.easypost.EasyPost;

import com.easypost.model.Address;

import com.easypost.model.Parcel;

import com.easypost.model.Shipment;

public class SamplePackageTracking {

   public static void main(String[] args) {

      EasyPost.apiKey = "YOUR_API_KEY";

      ShippingUtil shippingUtil = new ShippingUtil();

      Address fromAddress = shippingUtil.getFromAddress();

      Address toAddress = shippingUtil.getToAddress();

      String predefinedParcel = "Parcel";

      float weight = 1.3f;

      Parcel parcel = shippingUtil.getParcel(predefinedParcel, weight);

      Shipment shipment = shippingUtil.getShipment(fromAddress, toAddress, parcel);

      shipment = shippingUtil.generateTrackingCodeAndLabel(shipment.getId());

      System.out.println(" LabelUrl ==> " +shipment.getPostageLabel().getLabelUrl());

      System.out.println(" TrackingCode ==> " +shipment.getTrackingCode());

   }

}

  • ShippingUtil.java

package com.st.easypost.web;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.easypost.exception.EasyPostException;

import com.easypost.model.Address;

import com.easypost.model.AddressVerification;

import com.easypost.model.Error;

import com.easypost.model.Parcel;

import com.easypost.model.Rate;

import com.easypost.model.Shipment;

public class ShippingUtil {

   public Address getToAddress() {

      List<String> verificationList = new ArrayList<>();

      verificationList.add("delivery");

      Map<String, Object> toAddressHash = new HashMap<String, Object>();

      toAddressHash.put("name", "SurekhaTech");

      toAddressHash.put("street1", "53 woodward st");

      toAddressHash.put("state", "California");

      toAddressHash.put("zip", "94103");

      toAddressHash.put("country", "US");

      toAddressHash.put("verify", verificationList);

      Address toAddress = null;

      try {

          toAddress = Address.create(toAddressHash);

      } catch (EasyPostException e) {

          e.printStackTrace();

      }

      addressVerification(toAddress);

      return toAddress;

   }

   public Address getFromAddress() {

      List<String> verificationList = new ArrayList<>();

      verificationList.add("delivery");

      Map<String, Object> fromAddressHash = new HashMap<String, Object>();

      fromAddressHash.put("name", "SurekhaTech");

      fromAddressHash.put("company", "SurekhaTech");

      fromAddressHash.put("street1", "9250 Bendix Rd");

      fromAddressHash.put("city", "Columbia");

      fromAddressHash.put("state", "MD");

      fromAddressHash.put("zip", "21045");

      fromAddressHash.put("country", "US");

      fromAddressHash.put("verify", verificationList);

      Address fromAddress = null;    

      try {

          fromAddress = Address.create(fromAddressHash);

      } catch (EasyPostException e) {

          e.printStackTrace();

      }

      addressVerification(fromAddress);

      return fromAddress;

   }

   public Parcel getParcel(String predefinedParcel,float weight) {

      Map<String, Object> parcelMap = new HashMap<String, Object>();

      parcelMap.put("predefined_package", predefinedParcel);

      parcelMap.put("weight", weight);

      Parcel parcel = null;

      try {

          parcel = Parcel.create(parcelMap);

      } catch (EasyPostException e) {

          e.printStackTrace();

      }

      return parcel;

   }

   public Shipment getShipment(Address fromAddress, Address toAddress, Parcel parcel) {

      Shipment shipment = null;

      try {

          Map<String, Object> shipmentMap = new HashMap<String, Object>();

          shipmentMap.put("to_address", toAddress);

          shipmentMap.put("from_address", fromAddress);

          shipmentMap.put("parcel", parcel);

          shipment = Shipment.create(shipmentMap);

      } catch (EasyPostException e) {

          e.printStackTrace();

      }

      return shipment;

   }

   public Shipment generateTrackingCodeAndLabel(String shippingId) {

      Shipment shipment = null;

      try {

          shipment = Shipment.retrieve(shippingId);

          List<String> buyCarriers = new ArrayList<String>();

          buyCarriers.add("USPS");

          List<String> buyServices = new ArrayList<String>();

          buyServices.add("First");

          Rate lowestRate = shipment.lowestRate(buyCarriers, buyServices);

          if (shipment.getTrackingCode() == null) {

              shipment = shipment.buy(lowestRate);

          }

      } catch (EasyPostException e) {

          e.printStackTrace();

      }

      return shipment;

   }

   public List<Error> addressVerification(Address address) {

      List<Error> errors = null;

      AddressVerification addressVerification = address.getVerifications().get("delivery");

      if (addressVerification != null) {

          boolean success = addressVerification.getSuccess();

          if (!success) {

              errors = addressVerification.getErrors();

          }

      }

      return errors;

   }

}

We can help you if you want to allow your customer to track their packages in your application.

Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium