Delphi DLL
Delphi DLL
Shippo Create Shipment Object
See more Shippo Examples
Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, HttpResponse, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
json: HCkJsonObject;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
object_state: PWideChar;
template: PWideChar;
length: PWideChar;
width: PWideChar;
height: PWideChar;
distance_unit: PWideChar;
weight: PWideChar;
mass_unit: PWideChar;
value_amount: PWideChar;
value_currency: PWideChar;
j: Integer;
count_j: Integer;
shipment: PWideChar;
amount: PWideChar;
currency: PWideChar;
amount_local: PWideChar;
currency_local: PWideChar;
provider: PWideChar;
provider_image_75: PWideChar;
provider_image_200: PWideChar;
servicelevelName: PWideChar;
servicelevelToken: PWideChar;
servicelevelTerms: PWideChar;
estimated_days: Integer;
arrives_by: PWideChar;
duration_terms: PWideChar;
carrier_account: PWideChar;
zone: PWideChar;
strVal: PWideChar;
source: PWideChar;
code: PWideChar;
text: PWideChar;
object_created: PWideChar;
object_updated: PWideChar;
object_id: PWideChar;
object_owner: PWideChar;
status: PWideChar;
address_fromObject_id: PWideChar;
address_fromIs_complete: Boolean;
address_fromName: PWideChar;
address_fromCompany: PWideChar;
address_fromStreet_no: PWideChar;
address_fromStreet1: PWideChar;
address_fromStreet2: PWideChar;
address_fromStreet3: PWideChar;
address_fromCity: PWideChar;
address_fromState: PWideChar;
address_fromZip: PWideChar;
address_fromCountry: PWideChar;
address_fromPhone: PWideChar;
address_fromEmail: PWideChar;
address_fromIs_residential: PWideChar;
address_fromTest: Boolean;
address_toObject_id: PWideChar;
address_toIs_complete: Boolean;
address_toName: PWideChar;
address_toCompany: PWideChar;
address_toStreet_no: PWideChar;
address_toStreet1: PWideChar;
address_toStreet2: PWideChar;
address_toStreet3: PWideChar;
address_toCity: PWideChar;
address_toState: PWideChar;
address_toZip: PWideChar;
address_toCountry: PWideChar;
address_toPhone: PWideChar;
address_toEmail: PWideChar;
address_toIs_residential: PWideChar;
address_toTest: Boolean;
shipment_date: PWideChar;
address_returnObject_id: PWideChar;
address_returnIs_complete: Boolean;
address_returnName: PWideChar;
address_returnCompany: PWideChar;
address_returnStreet_no: PWideChar;
address_returnStreet1: PWideChar;
address_returnStreet2: PWideChar;
address_returnStreet3: PWideChar;
address_returnCity: PWideChar;
address_returnState: PWideChar;
address_returnZip: PWideChar;
address_returnCountry: PWideChar;
address_returnPhone: PWideChar;
address_returnEmail: PWideChar;
address_returnIs_residential: PWideChar;
address_returnTest: Boolean;
alternate_address_to: PWideChar;
customs_declaration: PWideChar;
metadata: PWideChar;
test: Boolean;
order: PWideChar;
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 := CkHttp_Create();
// Implements the following CURL command:
// curl https://api.goshippo.com/shipments/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -H "Content-Type: application/json" \
// -d '{
// "address_from":{
// "name":"Mr. Hippo",
// "street1":"215 Clayton St.",
// "city":"San Francisco",
// "state":"CA",
// "zip":"94117",
// "country":"US"
// },
// "address_to":{
// "name":"Mrs. Hippo",
// "street1":"965 Mission St.",
// "city":"San Francisco",
// "state":"CA",
// "zip":"94105",
// "country":"US"
// },
// "parcels":[{
// "length":"5",
// "width":"5",
// "height":"5",
// "distance_unit":"in",
// "weight":"2",
// "mass_unit":"lb"
// }],
// "async": false
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "address_from": {
// "name": "Mr. Hippo",
// "street1": "215 Clayton St.",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US"
// },
// "address_to": {
// "name": "Mrs. Hippo",
// "street1": "965 Mission St.",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94105",
// "country": "US"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "lb"
// }
// ],
// "async": false
// }
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'address_from.name','Mr. Hippo');
CkJsonObject_UpdateString(json,'address_from.street1','215 Clayton St.');
CkJsonObject_UpdateString(json,'address_from.city','San Francisco');
CkJsonObject_UpdateString(json,'address_from.state','CA');
CkJsonObject_UpdateString(json,'address_from.zip','94117');
CkJsonObject_UpdateString(json,'address_from.country','US');
CkJsonObject_UpdateString(json,'address_to.name','Mrs. Hippo');
CkJsonObject_UpdateString(json,'address_to.street1','965 Mission St.');
CkJsonObject_UpdateString(json,'address_to.city','San Francisco');
CkJsonObject_UpdateString(json,'address_to.state','CA');
CkJsonObject_UpdateString(json,'address_to.zip','94105');
CkJsonObject_UpdateString(json,'address_to.country','US');
CkJsonObject_UpdateString(json,'parcels[0].length','5');
CkJsonObject_UpdateString(json,'parcels[0].width','5');
CkJsonObject_UpdateString(json,'parcels[0].height','5');
CkJsonObject_UpdateString(json,'parcels[0].distance_unit','in');
CkJsonObject_UpdateString(json,'parcels[0].weight','2');
CkJsonObject_UpdateString(json,'parcels[0].mass_unit','lb');
CkJsonObject_UpdateBool(json,'async',False);
CkHttp_SetRequestHeader(http,'Authorization','ShippoToken <API_TOKEN>');
CkHttp_SetRequestHeader(http,'Content-Type','application/json');
resp := CkHttpResponse_Create();
success := CkHttp_HttpJson(http,'POST','https://api.goshippo.com/shipments/',json,'application/json',resp);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));
respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkHttpResponse__header(resp));
Memo1.Lines.Add('Failed.');
Exit;
end;
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "carrier_accounts": [
// ],
// "object_created": "2019-06-28T17:45:20.441Z",
// "object_updated": "2019-06-28T17:45:20.511Z",
// "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
// "object_owner": "admin@chilkatsoft.com",
// "status": "SUCCESS",
// "address_from": {
// "object_id": "427489906b604f498f7bd2429ab7d9a1",
// "is_complete": true,
// "name": "Mr. Hippo",
// "company": "",
// "street_no": "",
// "street1": "215 Clayton St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "address_to": {
// "object_id": "3d7166269a2844b7842ef9ace942ec7c",
// "is_complete": true,
// "name": "Mrs. Hippo",
// "company": "",
// "street_no": "",
// "street1": "965 Mission St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94105",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "parcels": [
// {
// "object_state": "VALID",
// "object_created": "2019-06-28T17:45:20.411Z",
// "object_updated": "2019-06-28T17:45:20.455Z",
// "object_id": "5af766ff15684a4186b0e3c833348fac",
// "object_owner": "admin@chilkatsoft.com",
// "template": null,
// "extra": {},
// "length": "5.0000",
// "width": "5.0000",
// "height": "5.0000",
// "distance_unit": "in",
// "weight": "2.0000",
// "mass_unit": "lb",
// "value_amount": null,
// "value_currency": null,
// "metadata": "",
// "line_items": [
// ],
// "test": true
// }
// ],
// "shipment_date": "2019-06-28T17:45:20.511Z",
// "address_return": {
// "object_id": "427489906b604f498f7bd2429ab7d9a1",
// "is_complete": true,
// "name": "Mr. Hippo",
// "company": "",
// "street_no": "",
// "street1": "215 Clayton St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "alternate_address_to": null,
// "customs_declaration": null,
// "extra": {},
// "rates": [
// {
// "object_created": "2019-06-28T17:45:20.789Z",
// "object_id": "dd7556c284e8444294d2ab7099e662d2",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// "FASTEST"
// ],
// "amount": "22.78",
// "currency": "USD",
// "amount_local": "22.78",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail Express",
// "token": "usps_priority_express",
// "terms": ""
// },
// "estimated_days": 2,
// "arrives_by": null,
// "duration_terms": "Overnight delivery to most U.S. locations.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// },
// {
// "object_created": "2019-06-28T17:45:20.786Z",
// "object_id": "ec11cc297e2f4583986097f6d409e5c6",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// "BESTVALUE",
// "CHEAPEST",
// "FASTEST"
// ],
// "amount": "6.95",
// "currency": "USD",
// "amount_local": "6.95",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail",
// "token": "usps_priority",
// "terms": ""
// },
// "estimated_days": 2,
// "arrives_by": null,
// "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// },
// {
// "object_created": "2019-06-28T17:45:20.785Z",
// "object_id": "d256586f539f4c93a2f02cc11f4942ac",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// ],
// "amount": "7.32",
// "currency": "USD",
// "amount_local": "7.32",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Parcel Select",
// "token": "usps_parcel_select",
// "terms": ""
// },
// "estimated_days": 7,
// "arrives_by": null,
// "duration_terms": "Delivery in 2 to 8 days.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// }
// ],
// "messages": [
// {
// "source": "DHLExpress",
// "code": "",
// "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
// }
// ],
// "metadata": "",
// "test": true,
// "order": null
// }
// 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_created := CkJsonObject__stringOf(jResp,'object_created');
object_updated := CkJsonObject__stringOf(jResp,'object_updated');
object_id := CkJsonObject__stringOf(jResp,'object_id');
object_owner := CkJsonObject__stringOf(jResp,'object_owner');
status := CkJsonObject__stringOf(jResp,'status');
address_fromObject_id := CkJsonObject__stringOf(jResp,'address_from.object_id');
address_fromIs_complete := CkJsonObject_BoolOf(jResp,'address_from.is_complete');
address_fromName := CkJsonObject__stringOf(jResp,'address_from.name');
address_fromCompany := CkJsonObject__stringOf(jResp,'address_from.company');
address_fromStreet_no := CkJsonObject__stringOf(jResp,'address_from.street_no');
address_fromStreet1 := CkJsonObject__stringOf(jResp,'address_from.street1');
address_fromStreet2 := CkJsonObject__stringOf(jResp,'address_from.street2');
address_fromStreet3 := CkJsonObject__stringOf(jResp,'address_from.street3');
address_fromCity := CkJsonObject__stringOf(jResp,'address_from.city');
address_fromState := CkJsonObject__stringOf(jResp,'address_from.state');
address_fromZip := CkJsonObject__stringOf(jResp,'address_from.zip');
address_fromCountry := CkJsonObject__stringOf(jResp,'address_from.country');
address_fromPhone := CkJsonObject__stringOf(jResp,'address_from.phone');
address_fromEmail := CkJsonObject__stringOf(jResp,'address_from.email');
address_fromIs_residential := CkJsonObject__stringOf(jResp,'address_from.is_residential');
address_fromTest := CkJsonObject_BoolOf(jResp,'address_from.test');
address_toObject_id := CkJsonObject__stringOf(jResp,'address_to.object_id');
address_toIs_complete := CkJsonObject_BoolOf(jResp,'address_to.is_complete');
address_toName := CkJsonObject__stringOf(jResp,'address_to.name');
address_toCompany := CkJsonObject__stringOf(jResp,'address_to.company');
address_toStreet_no := CkJsonObject__stringOf(jResp,'address_to.street_no');
address_toStreet1 := CkJsonObject__stringOf(jResp,'address_to.street1');
address_toStreet2 := CkJsonObject__stringOf(jResp,'address_to.street2');
address_toStreet3 := CkJsonObject__stringOf(jResp,'address_to.street3');
address_toCity := CkJsonObject__stringOf(jResp,'address_to.city');
address_toState := CkJsonObject__stringOf(jResp,'address_to.state');
address_toZip := CkJsonObject__stringOf(jResp,'address_to.zip');
address_toCountry := CkJsonObject__stringOf(jResp,'address_to.country');
address_toPhone := CkJsonObject__stringOf(jResp,'address_to.phone');
address_toEmail := CkJsonObject__stringOf(jResp,'address_to.email');
address_toIs_residential := CkJsonObject__stringOf(jResp,'address_to.is_residential');
address_toTest := CkJsonObject_BoolOf(jResp,'address_to.test');
shipment_date := CkJsonObject__stringOf(jResp,'shipment_date');
address_returnObject_id := CkJsonObject__stringOf(jResp,'address_return.object_id');
address_returnIs_complete := CkJsonObject_BoolOf(jResp,'address_return.is_complete');
address_returnName := CkJsonObject__stringOf(jResp,'address_return.name');
address_returnCompany := CkJsonObject__stringOf(jResp,'address_return.company');
address_returnStreet_no := CkJsonObject__stringOf(jResp,'address_return.street_no');
address_returnStreet1 := CkJsonObject__stringOf(jResp,'address_return.street1');
address_returnStreet2 := CkJsonObject__stringOf(jResp,'address_return.street2');
address_returnStreet3 := CkJsonObject__stringOf(jResp,'address_return.street3');
address_returnCity := CkJsonObject__stringOf(jResp,'address_return.city');
address_returnState := CkJsonObject__stringOf(jResp,'address_return.state');
address_returnZip := CkJsonObject__stringOf(jResp,'address_return.zip');
address_returnCountry := CkJsonObject__stringOf(jResp,'address_return.country');
address_returnPhone := CkJsonObject__stringOf(jResp,'address_return.phone');
address_returnEmail := CkJsonObject__stringOf(jResp,'address_return.email');
address_returnIs_residential := CkJsonObject__stringOf(jResp,'address_return.is_residential');
address_returnTest := CkJsonObject_BoolOf(jResp,'address_return.test');
alternate_address_to := CkJsonObject__stringOf(jResp,'alternate_address_to');
customs_declaration := CkJsonObject__stringOf(jResp,'customs_declaration');
metadata := CkJsonObject__stringOf(jResp,'metadata');
test := CkJsonObject_BoolOf(jResp,'test');
order := CkJsonObject__stringOf(jResp,'order');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'carrier_accounts');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'parcels');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
object_state := CkJsonObject__stringOf(jResp,'parcels[i].object_state');
object_created := CkJsonObject__stringOf(jResp,'parcels[i].object_created');
object_updated := CkJsonObject__stringOf(jResp,'parcels[i].object_updated');
object_id := CkJsonObject__stringOf(jResp,'parcels[i].object_id');
object_owner := CkJsonObject__stringOf(jResp,'parcels[i].object_owner');
template := CkJsonObject__stringOf(jResp,'parcels[i].template');
length := CkJsonObject__stringOf(jResp,'parcels[i].length');
width := CkJsonObject__stringOf(jResp,'parcels[i].width');
height := CkJsonObject__stringOf(jResp,'parcels[i].height');
distance_unit := CkJsonObject__stringOf(jResp,'parcels[i].distance_unit');
weight := CkJsonObject__stringOf(jResp,'parcels[i].weight');
mass_unit := CkJsonObject__stringOf(jResp,'parcels[i].mass_unit');
value_amount := CkJsonObject__stringOf(jResp,'parcels[i].value_amount');
value_currency := CkJsonObject__stringOf(jResp,'parcels[i].value_currency');
metadata := CkJsonObject__stringOf(jResp,'parcels[i].metadata');
test := CkJsonObject_BoolOf(jResp,'parcels[i].test');
j := 0;
count_j := CkJsonObject_SizeOfArray(jResp,'parcels[i].line_items');
while j < count_j do
begin
CkJsonObject_putJ(jResp,j);
j := j + 1;
end;
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'rates');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
object_created := CkJsonObject__stringOf(jResp,'rates[i].object_created');
object_id := CkJsonObject__stringOf(jResp,'rates[i].object_id');
object_owner := CkJsonObject__stringOf(jResp,'rates[i].object_owner');
shipment := CkJsonObject__stringOf(jResp,'rates[i].shipment');
amount := CkJsonObject__stringOf(jResp,'rates[i].amount');
currency := CkJsonObject__stringOf(jResp,'rates[i].currency');
amount_local := CkJsonObject__stringOf(jResp,'rates[i].amount_local');
currency_local := CkJsonObject__stringOf(jResp,'rates[i].currency_local');
provider := CkJsonObject__stringOf(jResp,'rates[i].provider');
provider_image_75 := CkJsonObject__stringOf(jResp,'rates[i].provider_image_75');
provider_image_200 := CkJsonObject__stringOf(jResp,'rates[i].provider_image_200');
servicelevelName := CkJsonObject__stringOf(jResp,'rates[i].servicelevel.name');
servicelevelToken := CkJsonObject__stringOf(jResp,'rates[i].servicelevel.token');
servicelevelTerms := CkJsonObject__stringOf(jResp,'rates[i].servicelevel.terms');
estimated_days := CkJsonObject_IntOf(jResp,'rates[i].estimated_days');
arrives_by := CkJsonObject__stringOf(jResp,'rates[i].arrives_by');
duration_terms := CkJsonObject__stringOf(jResp,'rates[i].duration_terms');
carrier_account := CkJsonObject__stringOf(jResp,'rates[i].carrier_account');
test := CkJsonObject_BoolOf(jResp,'rates[i].test');
zone := CkJsonObject__stringOf(jResp,'rates[i].zone');
j := 0;
count_j := CkJsonObject_SizeOfArray(jResp,'rates[i].attributes');
while j < count_j do
begin
CkJsonObject_putJ(jResp,j);
strVal := CkJsonObject__stringOf(jResp,'rates[i].attributes[j]');
j := j + 1;
end;
j := 0;
count_j := CkJsonObject_SizeOfArray(jResp,'rates[i].messages');
while j < count_j do
begin
CkJsonObject_putJ(jResp,j);
j := j + 1;
end;
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'messages');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
source := CkJsonObject__stringOf(jResp,'messages[i].source');
code := CkJsonObject__stringOf(jResp,'messages[i].code');
text := CkJsonObject__stringOf(jResp,'messages[i].text');
i := i + 1;
end;
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
end;