Delphi DLL
Delphi DLL
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.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;
template: PWideChar;
length: PWideChar;
width: PWideChar;
height: PWideChar;
distance_unit: PWideChar;
weight: PWideChar;
mass_unit: PWideChar;
value_amount: PWideChar;
value_currency: PWideChar;
test: Boolean;
j: Integer;
count_j: Integer;
intVal: Integer;
object_created: PWideChar;
object_updated: PWideChar;
object_id: PWideChar;
object_owner: PWideChar;
status: PWideChar;
address_fromObject_id: PWideChar;
address_fromIs_complete: Boolean;
address_fromCompany: PWideChar;
address_fromStreet_no: PWideChar;
address_fromName: PWideChar;
address_fromStreet1: PWideChar;
address_fromStreet2: PWideChar;
address_fromCity: PWideChar;
address_fromState: PWideChar;
address_fromZip: PWideChar;
address_fromCountry: PWideChar;
address_fromPhone: PWideChar;
address_fromEmail: PWideChar;
address_fromIs_residential: PWideChar;
address_toObject_id: PWideChar;
address_toIs_complete: Boolean;
address_toName: PWideChar;
address_toStreet1: PWideChar;
address_toCity: PWideChar;
address_toState: PWideChar;
address_toZip: PWideChar;
address_toCountry: PWideChar;
address_toPhone: PWideChar;
address_toEmail: PWideChar;
address_toIs_residential: Boolean;
address_return: PWideChar;
shipment_date: PWideChar;
extraInsuranceCurrency: PWideChar;
extraInsuranceAmount: PWideChar;
extraReference_1: PWideChar;
extraReference_2: PWideChar;
customs_declaration: PWideChar;
metadata: 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/customs/declarations/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -H "Content-Type: application/json" \
// -d '{
// "contents_type": "MERCHANDISE",
// "non_delivery_option": "RETURN",
// "certify": true,
// "certify_signer": "Simon Kreuz",
// "incoterm": "DDU",
// "items": [{
// "description": "T-shirt",
// "quantity": 20,
// "net_weight": "5",
// "mass_unit": "lb",
// "value_amount": "200",
// "value_currency": "USD",
// "tariff_number": "",
// "origin_country": "US"
// }]
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "contents_type": "MERCHANDISE",
// "non_delivery_option": "RETURN",
// "certify": true,
// "certify_signer": "Simon Kreuz",
// "incoterm": "DDU",
// "items": [
// {
// "description": "T-shirt",
// "quantity": 20,
// "net_weight": "5",
// "mass_unit": "lb",
// "value_amount": "200",
// "value_currency": "USD",
// "tariff_number": "",
// "origin_country": "US"
// }
// ]
// }
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'contents_type','MERCHANDISE');
CkJsonObject_UpdateString(json,'non_delivery_option','RETURN');
CkJsonObject_UpdateBool(json,'certify',True);
CkJsonObject_UpdateString(json,'certify_signer','Simon Kreuz');
CkJsonObject_UpdateString(json,'incoterm','DDU');
CkJsonObject_UpdateString(json,'items[0].description','T-shirt');
CkJsonObject_UpdateInt(json,'items[0].quantity',20);
CkJsonObject_UpdateString(json,'items[0].net_weight','5');
CkJsonObject_UpdateString(json,'items[0].mass_unit','lb');
CkJsonObject_UpdateString(json,'items[0].value_amount','200');
CkJsonObject_UpdateString(json,'items[0].value_currency','USD');
CkJsonObject_UpdateString(json,'items[0].tariff_number','');
CkJsonObject_UpdateString(json,'items[0].origin_country','US');
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/customs/declarations/',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)
// {
// "object_created": "2014-07-17T00:04:06.163Z",
// "object_updated": "2014-07-17T00:04:06.163Z",
// "object_id": "89436997a794439ab47999701e60392e",
// "object_owner": "shippotle@goshippo.com",
// "status": "SUCCESS",
// "address_from": {
// "object_id": "0943ae4e373e4120a99c337e496dcce8",
// "validation_results": {},
// "is_complete": true,
// "company": "",
// "street_no": "",
// "name": "Mr. Hippo",
// "street1": "215 Clayton St.",
// "street2": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "+15553419393",
// "email": "support@goshippo.com",
// "is_residential": null
// },
// "address_to": {
// "object_id": "4c7185d353764d0985a6a7825aed8ffb",
// "validation_results": {},
// "is_complete": true,
// "name": "Mrs. Hippo",
// "street1": "200 University Ave W",
// "city": "Waterloo",
// "state": "ON",
// "zip": "N2L 3G1",
// "country": "CA",
// "phone": "+1 555 341 9393",
// "email": "support@goshippo.com",
// "is_residential": false
// },
// "address_return": null,
// "parcels": [
// {
// "object_id": "ec952343dd4843c39b42aca620471fd5",
// "object_created": "2013-12-01T06:24:21.121Z",
// "object_updated": "2013-12-01T06:24:21.121Z",
// "object_owner": "shippotle@goshippo.com",
// "template": null,
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "lb",
// "value_amount": null,
// "value_currency": null,
// "metadata": "",
// "line_items": [
// ],
// "test": true
// }
// ],
// "shipment_date": "2013-12-03T12:00:00Z",
// "extra": {
// "insurance": {
// "currency": "",
// "amount": ""
// },
// "reference_1": "",
// "reference_2": ""
// },
// "customs_declaration": "b741b99f95e841639b54272834bc478c",
// "rates": [
// {
// "object_created": "2014-07-17T00:04:06.263Z",
// "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b",
// "object_owner": "shippotle@goshippo.com",
// "shipment": "89436997a794439ab47999701e60392e",
// "attributes": [
// ],
// "amount": "5.50",
// "currency": "USD",
// "amount_local": "5.50",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png",
// "provider_image_200": "https://cdn2.goshippo.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail",
// "token": "usps_priority",
// "terms": ""
// },
// "days": 2,
// "arrives_by": null,
// "duration_terms": "Delivery in 1 to 3 business days.",
// "messages": [
// ],
// "carrier_account": "078870331023437cb917f5187429b093",
// "test": false,
// "zone": 1
// },
// ...
// ],
// "carrier_accounts": [
// ],
// "messages": [
// ],
// "metadata": "Customer ID 123456"
// }
// 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_fromCompany := CkJsonObject__stringOf(jResp,'address_from.company');
address_fromStreet_no := CkJsonObject__stringOf(jResp,'address_from.street_no');
address_fromName := CkJsonObject__stringOf(jResp,'address_from.name');
address_fromStreet1 := CkJsonObject__stringOf(jResp,'address_from.street1');
address_fromStreet2 := CkJsonObject__stringOf(jResp,'address_from.street2');
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_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_toStreet1 := CkJsonObject__stringOf(jResp,'address_to.street1');
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_BoolOf(jResp,'address_to.is_residential');
address_return := CkJsonObject__stringOf(jResp,'address_return');
shipment_date := CkJsonObject__stringOf(jResp,'shipment_date');
extraInsuranceCurrency := CkJsonObject__stringOf(jResp,'extra.insurance.currency');
extraInsuranceAmount := CkJsonObject__stringOf(jResp,'extra.insurance.amount');
extraReference_1 := CkJsonObject__stringOf(jResp,'extra.reference_1');
extraReference_2 := CkJsonObject__stringOf(jResp,'extra.reference_2');
customs_declaration := CkJsonObject__stringOf(jResp,'customs_declaration');
metadata := CkJsonObject__stringOf(jResp,'metadata');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'parcels');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
object_id := CkJsonObject__stringOf(jResp,'parcels[i].object_id');
object_created := CkJsonObject__stringOf(jResp,'parcels[i].object_created');
object_updated := CkJsonObject__stringOf(jResp,'parcels[i].object_updated');
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);
intVal := CkJsonObject_IntOf(jResp,'rates[i]');
j := 0;
count_j := CkJsonObject_SizeOfArray(jResp,'rates[i].attributes');
while j < count_j do
begin
CkJsonObject_putJ(jResp,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,'carrier_accounts');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'messages');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
i := i + 1;
end;
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
end;