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

Shippo Create a Batch

See more Shippo Examples

Demonstrates how to create and purchase up to 10,000 shipments in a single API 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.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  json: TJsonObject;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  object_id: string;
  object_owner: string;
  status: string;
  object_created: string;
  object_updated: string;
  metadata: string;
  default_carrier_account: string;
  default_servicelevel_token: string;
  label_filetype: string;
  batch_shipmentsNext: string;
  batch_shipmentsPrevious: string;
  object_resultsPurchase_succeeded: Integer;
  object_resultsPurchase_failed: Integer;
  object_resultsCreation_failed: Integer;
  object_resultsCreation_succeeded: Integer;
  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;

  //  Use this online tool to generate code from sample JSON:
  //  Generate Code to Create JSON

  //  The following JSON is sent in the request body.

  //  {
  //    "default_carrier_account": "078870331023437cb917f5187429b093",
  //    "default_servicelevel_token": "usps_priority",
  //    "label_filetype": "PDF_4x6",
  //    "metadata": "BATCH #170",
  //    "batch_shipments": [
  //      {
  //        "shipment": {
  //          "address_from": {
  //            "name": "Mr Hippo",
  //            "street1": "965 Mission St",
  //            "street2": "Ste 201",
  //            "city": "San Francisco",
  //            "state": "CA",
  //            "zip": "94103",
  //            "country": "US",
  //            "phone": "4151234567",
  //            "email": "mrhippo@goshippo.com"
  //          },
  //          "address_to": {
  //            "name": "Mrs Hippo",
  //            "company": "",
  //            "street1": "Broadway 1",
  //            "street2": "",
  //            "city": "New York",
  //            "state": "NY",
  //            "zip": "10007",
  //            "country": "US",
  //            "phone": "4151234567",
  //            "email": "mrshippo@goshippo.com"
  //          },
  //          "parcels": [
  //            {
  //              "length": "5",
  //              "width": "5",
  //              "height": "5",
  //              "distance_unit": "in",
  //              "weight": "2",
  //              "mass_unit": "oz"
  //            }
  //          ]
  //        }
  //      },
  //      {
  //        "shipment": {
  //          "address_from": {
  //            "name": "Mr Hippo",
  //            "street1": "1092 Indian Summer Ct",
  //            "city": "San Jose",
  //            "state": "CA",
  //            "zip": "95122",
  //            "country": "US",
  //            "phone": "4151234567",
  //            "email": "mrhippo@goshippo.com"
  //          },
  //          "address_to": {
  //            "name": "Mrs Hippo",
  //            "company": "",
  //            "street1": "Broadway 1",
  //            "street2": "",
  //            "city": "New York",
  //            "state": "NY",
  //            "zip": "10007",
  //            "country": "US",
  //            "phone": "4151234567",
  //            "email": "mrshippo@goshippo.com"
  //          },
  //          "parcels": [
  //            {
  //              "length": "5",
  //              "width": "5",
  //              "height": "5",
  //              "distance_unit": "in",
  //              "weight": "20",
  //              "mass_unit": "lb"
  //            }
  //          ]
  //        },
  //        "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3",
  //        "servicelevel_token": "fedex_2_day"
  //      }
  //    ]
  //  }

  json := TJsonObject.Create;
  json.UpdateString('default_carrier_account','078870331023437cb917f5187429b093');
  json.UpdateString('default_servicelevel_token','usps_priority');
  json.UpdateString('label_filetype','PDF_4x6');
  json.UpdateString('metadata','BATCH #170');
  json.UpdateString('batch_shipments[0].shipment.address_from.name','Mr Hippo');
  json.UpdateString('batch_shipments[0].shipment.address_from.street1','965 Mission St');
  json.UpdateString('batch_shipments[0].shipment.address_from.street2','Ste 201');
  json.UpdateString('batch_shipments[0].shipment.address_from.city','San Francisco');
  json.UpdateString('batch_shipments[0].shipment.address_from.state','CA');
  json.UpdateString('batch_shipments[0].shipment.address_from.zip','94103');
  json.UpdateString('batch_shipments[0].shipment.address_from.country','US');
  json.UpdateString('batch_shipments[0].shipment.address_from.phone','4151234567');
  json.UpdateString('batch_shipments[0].shipment.address_from.email','mrhippo@goshippo.com');
  json.UpdateString('batch_shipments[0].shipment.address_to.name','Mrs Hippo');
  json.UpdateString('batch_shipments[0].shipment.address_to.company','');
  json.UpdateString('batch_shipments[0].shipment.address_to.street1','Broadway 1');
  json.UpdateString('batch_shipments[0].shipment.address_to.street2','');
  json.UpdateString('batch_shipments[0].shipment.address_to.city','New York');
  json.UpdateString('batch_shipments[0].shipment.address_to.state','NY');
  json.UpdateString('batch_shipments[0].shipment.address_to.zip','10007');
  json.UpdateString('batch_shipments[0].shipment.address_to.country','US');
  json.UpdateString('batch_shipments[0].shipment.address_to.phone','4151234567');
  json.UpdateString('batch_shipments[0].shipment.address_to.email','mrshippo@goshippo.com');
  json.UpdateString('batch_shipments[0].shipment.parcels[0].length','5');
  json.UpdateString('batch_shipments[0].shipment.parcels[0].width','5');
  json.UpdateString('batch_shipments[0].shipment.parcels[0].height','5');
  json.UpdateString('batch_shipments[0].shipment.parcels[0].distance_unit','in');
  json.UpdateString('batch_shipments[0].shipment.parcels[0].weight','2');
  json.UpdateString('batch_shipments[0].shipment.parcels[0].mass_unit','oz');
  json.UpdateString('batch_shipments[1].shipment.address_from.name','Mr Hippo');
  json.UpdateString('batch_shipments[1].shipment.address_from.street1','1092 Indian Summer Ct');
  json.UpdateString('batch_shipments[1].shipment.address_from.city','San Jose');
  json.UpdateString('batch_shipments[1].shipment.address_from.state','CA');
  json.UpdateString('batch_shipments[1].shipment.address_from.zip','95122');
  json.UpdateString('batch_shipments[1].shipment.address_from.country','US');
  json.UpdateString('batch_shipments[1].shipment.address_from.phone','4151234567');
  json.UpdateString('batch_shipments[1].shipment.address_from.email','mrhippo@goshippo.com');
  json.UpdateString('batch_shipments[1].shipment.address_to.name','Mrs Hippo');
  json.UpdateString('batch_shipments[1].shipment.address_to.company','');
  json.UpdateString('batch_shipments[1].shipment.address_to.street1','Broadway 1');
  json.UpdateString('batch_shipments[1].shipment.address_to.street2','');
  json.UpdateString('batch_shipments[1].shipment.address_to.city','New York');
  json.UpdateString('batch_shipments[1].shipment.address_to.state','NY');
  json.UpdateString('batch_shipments[1].shipment.address_to.zip','10007');
  json.UpdateString('batch_shipments[1].shipment.address_to.country','US');
  json.UpdateString('batch_shipments[1].shipment.address_to.phone','4151234567');
  json.UpdateString('batch_shipments[1].shipment.address_to.email','mrshippo@goshippo.com');
  json.UpdateString('batch_shipments[1].shipment.parcels[0].length','5');
  json.UpdateString('batch_shipments[1].shipment.parcels[0].width','5');
  json.UpdateString('batch_shipments[1].shipment.parcels[0].height','5');
  json.UpdateString('batch_shipments[1].shipment.parcels[0].distance_unit','in');
  json.UpdateString('batch_shipments[1].shipment.parcels[0].weight','20');
  json.UpdateString('batch_shipments[1].shipment.parcels[0].mass_unit','lb');
  json.UpdateString('batch_shipments[1].carrier_account','a4391cd4ab974f478f55dc08b5c8e3b3');
  json.UpdateString('batch_shipments[1].servicelevel_token','fedex_2_day');

  http.SetRequestHeader('Authorization','ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a');
  http.SetRequestHeader('Content-Type','application/json');

  resp := THttpResponse.Create;
  success := http.HttpJson('POST','https://api.goshippo.com/batches/',json,'application/json',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)

  //  {
  //    "object_id": "a015eb693cca465dbb6523ce6d2e3c65",
  //    "object_owner": "admin",
  //    "status": "VALIDATING",
  //    "object_created": "2016-09-12T15:25:43.465Z",
  //    "object_updated": "2016-09-12T15:25:43.465Z",
  //    "metadata": "BATCH #170",
  //    "default_carrier_account": "078870331023437cb917f5187429b093",
  //    "default_servicelevel_token": "usps_priority",
  //    "label_filetype": "PDF_4x6",
  //    "batch_shipments": {
  //      "next": null,
  //      "previous": null,
  //      "results": [
  //      ]
  //    },
  //    "object_results": {
  //      "purchase_succeeded": 0,
  //      "purchase_failed": 0,
  //      "creation_failed": 0,
  //      "creation_succeeded": 0
  //    },
  //    "label_url": [
  //    ]
  //  }

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

  object_id := jResp.StringOf('object_id');
  object_owner := jResp.StringOf('object_owner');
  status := jResp.StringOf('status');
  object_created := jResp.StringOf('object_created');
  object_updated := jResp.StringOf('object_updated');
  metadata := jResp.StringOf('metadata');
  default_carrier_account := jResp.StringOf('default_carrier_account');
  default_servicelevel_token := jResp.StringOf('default_servicelevel_token');
  label_filetype := jResp.StringOf('label_filetype');
  batch_shipmentsNext := jResp.StringOf('batch_shipments.next');
  batch_shipmentsPrevious := jResp.StringOf('batch_shipments.previous');
  object_resultsPurchase_succeeded := jResp.IntOf('object_results.purchase_succeeded');
  object_resultsPurchase_failed := jResp.IntOf('object_results.purchase_failed');
  object_resultsCreation_failed := jResp.IntOf('object_results.creation_failed');
  object_resultsCreation_succeeded := jResp.IntOf('object_results.creation_succeeded');
  i := 0;
  count_i := jResp.SizeOfArray('batch_shipments.results');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;

  i := 0;
  count_i := jResp.SizeOfArray('label_url');
  while i < count_i do
    begin
      jResp.I := i;
      i := i + 1;
    end;



  http.Free;
  json.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.