Sample code for 30+ languages & platforms
Delphi ActiveX

UPS Tracking API

See more HTTP Misc Examples

Demonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
url: WideString;
json: TChilkatJsonObject;
sb: TChilkatStringBuilder;
resp: TChilkatHttpResponse;
statusCode: WideString;
statusDescription: WideString;
customerContext: WideString;
inquiryNumberCode: WideString;
inquiryNumberDescription: WideString;
inquiryNumberValue: WideString;
shipmentTypeCode: WideString;
shipmentTypeDescription: WideString;
shipperNumber: WideString;
serviceCode: WideString;
serviceDescription: WideString;
referenceNumberCode: WideString;
referenceNumberValue: WideString;
pickupDate: WideString;
trackingNumber: WideString;
unitOfMeasurementCode: WideString;
weight: WideString;
disclaimer: WideString;
i: Integer;
activityCount: Integer;
city: WideString;
provinceCode: WideString;
postalCode: WideString;
countryCode: WideString;
locationCode: WideString;
locationDescription: WideString;
locationSignedForByName: WideString;
activityStatusType: WideString;
activityStatusDescription: WideString;
activityStatusCode: WideString;
activityDate: WideString;
activityTime: WideString;
typeCode: Integer;
typeDescription: WideString;
documentContent: WideString;
documentFormatCode: WideString;
documentFormatDescription: WideString;
sbImagePath: TChilkatStringBuilder;
imageData: TChilkatBinData;
refnumCount: Integer;
refnumCode: WideString;
refnumValue: WideString;

begin
success := 0;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

http := TChilkatHttp.Create(Self);

// This is the testing endpoint for the tracking API:
url := 'https://wwwcie.ups.com/rest/Track';

// Send an HTTP request with the following JSON body:

// {
//   "UPSSecurity": {
//     "UsernameToken": {
//       "Username": "Your Username",
//       "Password": "Your Password"
//     },
//     "ServiceAccessToken": {
//       "AccessLicenseNumber": "Your Access License Number"
//     }
//   },
//   "TrackRequest": {
//     "Request": {
//       "RequestOption": "1",
//       "TransactionReference": {
//         "CustomerContext": "Your Test Case Summary Description"
//       }
//     },
//     "InquiryNumber": "YourTrackingNumber"
//   }
// }
// 

// Build the above JSON.
json := TChilkatJsonObject.Create(Self);
json.UpdateString('UPSSecurity.UsernameToken.Username','UPS_USERNAME');
json.UpdateString('UPSSecurity.UsernameToken.Password','UPS_PASSWORD');
json.UpdateString('UPSSecurity.ServiceAccessToken.AccessLicenseNumber','UPS_ACCESS_KEY');
// Request all activity...
json.UpdateString('TrackRequest.Request.RequestOption','activity');
json.UpdateString('TrackRequest.Request.TransactionReference.CustomerContext','Your Test Case Summary Description');
json.UpdateString('TrackRequest.InquiryNumber','1Z12345E0205271688');

sb := TChilkatStringBuilder.Create(Self);
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpJson('POST',url,json.ControlInterface,'application/json',resp.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

Memo1.Lines.Add('status = ' + IntToStr(resp.StatusCode));

// A 200 response status indicate success.
if (resp.StatusCode <> 200) then
  begin
    Memo1.Lines.Add(resp.BodyStr);
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

json.Load(resp.BodyStr);
json.EmitCompact := 0;
Memo1.Lines.Add(json.Emit());

// {
//   "TrackResponse": {
//     "Response": {
//       "ResponseStatus": {
//         "Code": "1",
//         "Description": "Success"
//       },
//       "TransactionReference": {
//         "CustomerContext": "Your Test Case Summary Description"
//       }
//     },
//     "Shipment": {
//       "InquiryNumber": {
//         "Code": "01",
//         "Description": "ShipmentIdentificationNumber",
//         "Value": "1Z12345E0205271688"
//       },
//       "ShipmentType": {
//         "Code": "01",
//         "Description": "Small Package"
//       },
//       "ShipperNumber": "12345E",
//       "Service": {
//         "Code": "002",
//         "Description": "2ND DAY AIR"
//       },
//       "ReferenceNumber": {
//         "Code": "01",
//         "Value": "LINE4AND115"
//       },
//       "PickupDate": "19990608",
//       "Package": {
//         "TrackingNumber": "1Z12345E0205271688",
//         "Activity": [
//           {
//             "ActivityLocation": {
//               "Address": {
//                 "City": "ANYTOWN",
//                 "StateProvinceCode": "GA",
// 
//                 "PostalCode": "30340",
//                 "CountryCode": "US"
//               },
//               "Code": "ML",
//               "Description": "BACK DOOR",
//               "SignedForByName": "JOHN DOE"
//             },
//             "Status": {
//               "Type": "D",
//               "Description": "DELIVERED",
//               "Code": "KM"
//             },
//             "Date": "19990610",
//             "Time": "120000",
//             "Document": {
//               "Type": {
//                 "Code": "01",
//                 "Description": "Signature Image"
//               },
//               "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=",
//               "Format": {
//                 "Code": "01",
//                 "Description": "GIF"
//               }
//             }
//           },
//           {
//             "Status": {
//               "Type": "M",
//               "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.",
//               "Code": "MP"
//             },
//             "Date": "19990608",
//             "Time": "120000"
//           }
//         ],
//         "PackageWeight": {
//           "UnitOfMeasurement": {
//             "Code": "LBS"
//           },
//           "Weight": "5.00"
//         },
//         "ReferenceNumber": [
//           {
//             "Code": "01",
//             "Value": "LINE4AND115"
//           },
//           {
//             "Code": "08",
//             "Value": "LJ67Y5"
//           }
//         ]
//       }
//     },
//     "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track"
//   }
// }

// Use the online tool at Generate JSON Parsing Code
// to generate JSON parsing code.

statusCode := json.StringOf('TrackResponse.Response.ResponseStatus.Code');
statusDescription := json.StringOf('TrackResponse.Response.ResponseStatus.Description');
Memo1.Lines.Add('statusCode: ' + statusCode);
Memo1.Lines.Add('statusDescription' + statusDescription);
customerContext := json.StringOf('TrackResponse.Response.TransactionReference.CustomerContext');
inquiryNumberCode := json.StringOf('TrackResponse.Shipment.InquiryNumber.Code');
inquiryNumberDescription := json.StringOf('TrackResponse.Shipment.InquiryNumber.Description');
inquiryNumberValue := json.StringOf('TrackResponse.Shipment.InquiryNumber.Value');
shipmentTypeCode := json.StringOf('TrackResponse.Shipment.ShipmentType.Code');
shipmentTypeDescription := json.StringOf('TrackResponse.Shipment.ShipmentType.Description');
shipperNumber := json.StringOf('TrackResponse.Shipment.ShipperNumber');
serviceCode := json.StringOf('TrackResponse.Shipment.Service.Code');
serviceDescription := json.StringOf('TrackResponse.Shipment.Service.Description');
referenceNumberCode := json.StringOf('TrackResponse.Shipment.ReferenceNumber.Code');
referenceNumberValue := json.StringOf('TrackResponse.Shipment.ReferenceNumber.Value');
pickupDate := json.StringOf('TrackResponse.Shipment.PickupDate');
trackingNumber := json.StringOf('TrackResponse.Shipment.Package.TrackingNumber');
unitOfMeasurementCode := json.StringOf('TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code');
weight := json.StringOf('TrackResponse.Shipment.Package.PackageWeight.Weight');
disclaimer := json.StringOf('TrackResponse.Disclaimer');

i := 0;
activityCount := json.SizeOfArray('TrackResponse.Shipment.Package.Activity');
Memo1.Lines.Add('activityCount: ' + IntToStr(activityCount));

while i < activityCount do
  begin
    Memo1.Lines.Add('-- activity ' + IntToStr(i));
    json.I := i;
    if (json.HasMember('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City') = 1) then
      begin
        city := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City');
        Memo1.Lines.Add('city: ' + city);
        provinceCode := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode');
        postalCode := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode');
        countryCode := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode');
      end;
    locationCode := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code');
    locationDescription := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description');
    locationSignedForByName := json.StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName');

    activityStatusType := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Status.Type');
    Memo1.Lines.Add('activityStatusType: ' + activityStatusType);
    activityStatusDescription := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Status.Description');
    Memo1.Lines.Add('activityStatusDescription: ' + activityStatusDescription);
    activityStatusCode := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Status.Code');
    Memo1.Lines.Add('activityStatusCode: ' + activityStatusCode);

    activityDate := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Date');
    activityTime := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Time');

    if (json.HasMember('TrackResponse.Shipment.Package.Activity[i].Document') = 1) then
      begin
        typeCode := json.IntOf('TrackResponse.Shipment.Package.Activity[i].Document.Type.Code');
        typeDescription := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Type.Description');
        documentContent := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Content');
        documentFormatCode := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Format.Code');
        // Format description would be something like "GIF" for a signature image.
        documentFormatDescription := json.StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Format.Description');

        // 01 - Signature Image
        // 02 - Delivery Receipt
        // 03 - Free Astray
        // 04 - POD
        if (typeCode = 1) then
          begin
            // We have a signature image.  Get the image data and save to a file.
            sbImagePath := TChilkatStringBuilder.Create(Self);
            sbImagePath.Append('qa_output/sig_');
            sbImagePath.Append(trackingNumber);
            sbImagePath.Append('.');
            sbImagePath.Append(documentFormatDescription);
            imageData := TChilkatBinData.Create(Self);
            success := imageData.AppendEncoded(documentContent,'base64');
            // Write to "qa_output/sig_1Z12345E0205271688.GIF"
            success := imageData.WriteFile(sbImagePath.GetAsString());
          end;
      end;
    i := i + 1;
  end;

i := 0;
refnumCount := json.SizeOfArray('TrackResponse.Shipment.Package.ReferenceNumber');
while i < refnumCount do
  begin
    json.I := i;
    refnumCode := json.StringOf('TrackResponse.Shipment.Package.ReferenceNumber[i].Code');
    refnumValue := json.StringOf('TrackResponse.Shipment.Package.ReferenceNumber[i].Value');
    i := i + 1;
  end;

Memo1.Lines.Add('Success.');
end;