Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Shippo Adding Metadata

See more Shippo Examples

Demonstrates how to add metadata to the tracking request through a POST request.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpRequest,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  req: THttpRequest;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  object_created: string;
  object_updated: string;
  object_id: string;
  status: string;
  status_details: string;
  status_date: string;
  substatus: string;
  locationCity: string;
  locationState: string;
  locationZip: string;
  locationCountry: string;
  carrier: string;
  tracking_number: string;
  address_fromCity: string;
  address_fromState: string;
  address_fromZip: string;
  address_fromCountry: string;
  address_toCity: string;
  address_toState: string;
  address_toZip: string;
  address_toCountry: string;
  eta: string;
  original_eta: string;
  servicelevelToken: string;
  servicelevelName: string;
  metadata: string;
  tracking_statusObject_created: string;
  tracking_statusObject_updated: string;
  tracking_statusObject_id: string;
  tracking_statusStatus: string;
  tracking_statusStatus_details: string;
  tracking_statusStatus_date: string;
  tracking_statusSubstatus: string;
  tracking_statusLocationCity: string;
  tracking_statusLocationState: string;
  tracking_statusLocationZip: string;
  tracking_statusLocationCountry: string;
  transaction: string;
  test: Boolean;
  i: Integer;
  count_i: Integer;

begin
  success := False;

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

  http := THttp.Create;

  //  Implements the following CURL command:

  //  curl https://api.goshippo.com/tracks/ \
  //      -H "Authorization: ShippoToken <API_TOKEN>" \
  //      -d carrier="shippo" \
  //      -d tracking_number="SHIPPO_TRANSIT" \
  //      -d metadata="Order 000123"

  req := THttpRequest.Create;
  req.HttpVerb := 'POST';
  req.Path := '/tracks/';
  req.ContentType := 'application/x-www-form-urlencoded';
  req.AddParam('carrier','shippo');
  req.AddParam('tracking_number','SHIPPO_TRANSIT');
  req.AddParam('metadata','Order 000123');

  req.AddHeader('Authorization','ShippoToken <API_TOKEN>');

  resp := THttpResponse.Create;
  success := http.HttpReq('https://api.goshippo.com/tracks/',req,resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;

  //  Sample JSON response:
  //  (Sample code for parsing the JSON response is shown below)

  //  {
  //    "messages": [
  //    ],
  //    "carrier": "shippo",
  //    "tracking_number": "SHIPPO_TRANSIT",
  //    "address_from": {
  //      "city": "San Francisco",
  //      "state": "CA",
  //      "zip": "94103",
  //      "country": "US"
  //    },
  //    "address_to": {
  //      "city": "Chicago",
  //      "state": "IL",
  //      "zip": "60611",
  //      "country": "US"
  //    },
  //    "eta": "2019-07-07T17:07:44.989Z",
  //    "original_eta": "2019-07-07T17:07:44.989Z",
  //    "servicelevel": {
  //      "token": "shippo_priority",
  //      "name": "Priority Mail"
  //    },
  //    "metadata": "Shippo test tracking",
  //    "tracking_status": {
  //      "object_created": "2019-07-04T17:07:45.003Z",
  //      "object_updated": null,
  //      "object_id": "ee35fb56f5d04021b36168abedc04573",
  //      "status": "TRANSIT",
  //      "status_details": "Your shipment has departed from the origin.",
  //      "status_date": "2019-07-03T15:02:45.003Z",
  //      "substatus": null,
  //      "location": {
  //        "city": "San Francisco",
  //        "state": "CA",
  //        "zip": "94103",
  //        "country": "US"
  //      }
  //    },
  //    "tracking_history": [
  //      {
  //        "object_created": "2019-07-04T17:07:45.005Z",
  //        "object_updated": null,
  //        "object_id": "2121a59f53ed42e0ae0436f636179156",
  //        "status": "UNKNOWN",
  //        "status_details": "The carrier has received the electronic shipment information.",
  //        "status_date": "2019-07-02T12:57:45.005Z",
  //        "substatus": null,
  //        "location": {
  //          "city": "San Francisco",
  //          "state": "CA",
  //          "zip": "94103",
  //          "country": "US"
  //        }
  //      },
  //      {
  //        "object_created": "2019-07-04T17:07:45.005Z",
  //        "object_updated": null,
  //        "object_id": "06f949db1a8245beaa28df264b368a76",
  //        "status": "TRANSIT",
  //        "status_details": "Your shipment has departed from the origin.",
  //        "status_date": "2019-07-03T15:02:45.005Z",
  //        "substatus": null,
  //        "location": {
  //          "city": "San Francisco",
  //          "state": "CA",
  //          "zip": "94103",
  //          "country": "US"
  //        }
  //      }
  //    ],
  //    "transaction": null,
  //    "test": true
  //  }

  //  Sample code for parsing the JSON response...
  //  Use the following online tool to generate parsing code from sample JSON:
  //  Generate Parsing Code from JSON

  carrier := jResp.StringOf('carrier');
  tracking_number := jResp.StringOf('tracking_number');
  address_fromCity := jResp.StringOf('address_from.city');
  address_fromState := jResp.StringOf('address_from.state');
  address_fromZip := jResp.StringOf('address_from.zip');
  address_fromCountry := jResp.StringOf('address_from.country');
  address_toCity := jResp.StringOf('address_to.city');
  address_toState := jResp.StringOf('address_to.state');
  address_toZip := jResp.StringOf('address_to.zip');
  address_toCountry := jResp.StringOf('address_to.country');
  eta := jResp.StringOf('eta');
  original_eta := jResp.StringOf('original_eta');
  servicelevelToken := jResp.StringOf('servicelevel.token');
  servicelevelName := jResp.StringOf('servicelevel.name');
  metadata := jResp.StringOf('metadata');
  tracking_statusObject_created := jResp.StringOf('tracking_status.object_created');
  tracking_statusObject_updated := jResp.StringOf('tracking_status.object_updated');
  tracking_statusObject_id := jResp.StringOf('tracking_status.object_id');
  tracking_statusStatus := jResp.StringOf('tracking_status.status');
  tracking_statusStatus_details := jResp.StringOf('tracking_status.status_details');
  tracking_statusStatus_date := jResp.StringOf('tracking_status.status_date');
  tracking_statusSubstatus := jResp.StringOf('tracking_status.substatus');
  tracking_statusLocationCity := jResp.StringOf('tracking_status.location.city');
  tracking_statusLocationState := jResp.StringOf('tracking_status.location.state');
  tracking_statusLocationZip := jResp.StringOf('tracking_status.location.zip');
  tracking_statusLocationCountry := jResp.StringOf('tracking_status.location.country');
  transaction := jResp.StringOf('transaction');
  test := jResp.BoolOf('test');
  i := 0;
  count_i := jResp.SizeOfArray('messages');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('tracking_history');
  while i < count_i do
    begin
      jResp.I := i;
      object_created := jResp.StringOf('tracking_history[i].object_created');
      object_updated := jResp.StringOf('tracking_history[i].object_updated');
      object_id := jResp.StringOf('tracking_history[i].object_id');
      status := jResp.StringOf('tracking_history[i].status');
      status_details := jResp.StringOf('tracking_history[i].status_details');
      status_date := jResp.StringOf('tracking_history[i].status_date');
      substatus := jResp.StringOf('tracking_history[i].substatus');
      locationCity := jResp.StringOf('tracking_history[i].location.city');
      locationState := jResp.StringOf('tracking_history[i].location.state');
      locationZip := jResp.StringOf('tracking_history[i].location.zip');
      locationCountry := jResp.StringOf('tracking_history[i].location.country');
      i := i + 1;
    end;



  http.Free;
  req.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.