The Shipping Package RESTful API gives your applications many ways to manage the shipment of packages to their destination. UPS offers a range of delivery time frames from same day to standard ground transportation. Shipments may be within the United States or international, and they may range from documents to large packages. UPS also supports many valued added services for shipments, including collect on delivery (COD), declared value, delivery confirmation, and automatic notification of delivery status.
- You will need to have Maven and the Java Development Kit installed.
- You can either download a local copy or clone the repository:
git clone https://github.com/UPS-API/java-api-examples.git- Update your OAuth Client information in /src/main/resources/application.properties file. These values can be found in the UPS Developer Portal under Apps and your specific application's information.
| Property Name |
|---|
api.oauth.partner.client.id |
api.oauth.partner.secret |
- Update your client information in any tags marked with "< >" within the /src/main/resources/ json files. For this API there are the following fields:
| Property Name |
|---|
<Shipper Number> |
<your email address> |
<your From EMail Address> |
<your Account Number> |
<your UndeliverableEMailAddress address> |
<youre Tracking Number> |
< PreDeparture ITN Number> |
<Tax Identification Number> |
<Invoice Number> |
<Purchase Order Number> |
<Entry Number> |
- Build the project using Maven
cd <project home>
mvn clean package- Run the project that is generated in the /target directory.
java -jar shipping-x.x.x.jar- Check the console for the application's REST response.
There are 3 notable class in this tutorial, namely com.ups.api.app.AppConfig and com.ups.api.app.PickupDemo. The AppConfig class is a configuration class leveraging Spring injection to incorporate the property value from src/main/resources/application.properties file. The TnTDemo is to illustrate how to use the TNT api.
Get an access token via OAuth client_credentials grant type.
// Prepare shipping api access.
final String accessToken = Util.getAccessToken(appConfig, restTemplate);
ShipApi shipApi = api.get();
if (null == shipApi) {
shipApi = new ShipApi(new ApiClient(restTemplate));
shipApi.getApiClient().setBasePath(appConfig.getShippingBaseUrl());
api.set(shipApi);
}
shipApi.getApiClient().addDefaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);initializeShipApi function is to create a ship api object with the base url and populated the HTTP Authorization header with the access token.
##Shipping code
//this code for shipmentReques
SHIPRequestWrapper shipmentRequest = Util.createRequestFromJsonFile(
entry.getValue().get(AppConfig.SCENARIO_PROPERTIES_JSON_FILE_NAME), SHIPRequestWrapper.class,
appConfig);
final String transId = UUID.randomUUID().toString().replaceAll("-", "");
final ShipApi shipApi = getShipApi(restTemplate, appConfig);
SHIPResponseWrapper shipResponseWrapper = Util.jsonResultPreprocess(
shipApi.shipment(appConfig.getShippingVersion(), shipmentRequest, transId,
appConfig.getTransactionSrc(), null),
Util.getJsonToObjectConversionMap(), SHIPResponseWrapper.class);
ShipmentResponse shipmentResponse = shipResponseWrapper.getShipmentResponse();
if (trackingNumber == null) {
trackingNumber = shipmentResponse.getShipmentResults().getPackageResults().getTrackingNumber();
shipmentIdentificationNumber = shipmentResponse.getShipmentResults()
.getShipmentIdentificationNumber();
}It reconstructs a SHIPRequestWrapper object from a json file. In a typical application, a SHIPRequestWrapper object would be created via a default constructor and calling setter to populate the attribute instead. A SHIPResponseWrapper will be returned from backend server for a particular shipment specified in the SHIPResponseWrapper object. The SHIPResponseWrapper would have a validation error if there is any as well as emsResponse, which includes a list of available UPS service and service detail, to a successful request.
##Label Recovery
LABELRECOVERYRequestWrapper labelRecoveryRequest = Util.createRequestFromJsonFile(
entry.getValue().get(AppConfig.SCENARIO_PROPERTIES_JSON_FILE_NAME),
LABELRECOVERYRequestWrapper.class, appConfig);
final String transId = UUID.randomUUID().toString().replaceAll("-", "");
final ShipApi shipApi = getShipApi(restTemplate, appConfig);
LABELRECOVERYResponseWrapper labelrecoveryResponseWrapper = shipApi.labelRecovery(
appConfig.getShippingVersion(), labelRecoveryRequest, transId, appConfig.getTransactionSrc());
appConfig.getTransactionSrc(), trackingNumber);It reconstructs a LABELRECOVERYRequestWrapper object from a json file. In a typical application, a LABELRECOVERYRequestWrapper object would be created via a default constructor and calling setter to populate the attribute instead. A LABELRECOVERYResponseWrapper will be returned from backend server for a particular shipment specified in the LABELRECOVERYResponseWrapper object. The LABELRECOVERYResponseWrapper would have a validation error if there is any as well as LABELRECOVERYResponseWrapper, which includes a list of available UPS service and service detail, to a successful request.
##Label Recovery
final ShipApi shipApi = getShipApi(restTemplate, appConfig);
final String transId = UUID.randomUUID().toString().replace("-", "");
VOIDSHIPMENTResponseWrapper voidshipmentResponseWrapper = shipApi.voidShipment(
appConfig.getShippingVersion(), shipmentIdentificationNumber, transId,
appConfig.getTransactionSrc(), trackingNumber);A VOIDSHIPMENTResponseWrapper will be returned from backend server for a particular shipment specified in the VOIDSHIPMENTResponseWrapper object. The LABELRECOVERYResponseWrapper would have a validation error if there is any as well as VOIDSHIPMENTResponseWrapper, which includes a list of available UPS service and service detail, to a successful request.
- The Shipping Package RESTful API provides access to Published Rates and Negotiated Rates. A negotiated rate is established by contract between the customer and UPS.
Sample Request Negotiated Rates
Sample Response Negotiated Rates
Shipping Request with International Forms
Sample Response International Forms
Shipping Request Shipping Dry Ice or Lithium Batteries
Sample Response Shipping Dry Ice or Lithium Batteries
Customers must computer generate the UPS Hazardous Materials Shipping Paper or Shipper's Declaration. There is no validation for a hazardous materials shipment request
Shipping Request Shipping Hazmat Goods Request
Sample Response Shipping Hazmat Goods
Shipping Request Billing Third Party
Sample Response Billing Third Party
Shipping Request Multi-Piece Shipping
Sample Response Multi-Piece Shipping
Shipping Request Ship to a UPS Access Point
Sample Response Ship to a UPS Access Point
Shipping Request Proactive Response Shipping
Sample Response Proactive Response Shipping
UPS Worldwide Economy (WWE) allows shippers to manifest low weight, low value goods destined for international consignees. Shippers can either consolidate their WWE shipments into a larger “Master Carton” for shipping to an in country UPS Export Facility; or they can provide their own transportation of the WWE shipments to the UPS Export Facility. WWE shipments that have been consolidated into a Master Carton are linked via a MasterCartonID, which provides enhanced tracking capability between the WWE shipments contained within and the Master Carton itself.
Shipping Request WorldWide Economy Shipping
Sample Response WorldWide Economy Shipping