Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Shippo Create Customs Declaration
See more Shippo Examples
Demonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
strVal: string;
object_created: string;
object_updated: string;
object_id: string;
object_owner: string;
object_state: string;
address_importer: string;
certify_signer: string;
certify: Boolean;
non_delivery_option: string;
contents_type: string;
contents_explanation: string;
exporter_reference: string;
importer_reference: string;
invoice: string;
commercial_invoice: Boolean;
license: string;
certificate: string;
notes: string;
eel_pfc: string;
aes_itn: string;
disclaimer: string;
incoterm: string;
metadata: string;
test: Boolean;
duties_payor: string;
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/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 := TJsonObject.Create;
json.UpdateString('contents_type','MERCHANDISE');
json.UpdateString('non_delivery_option','RETURN');
json.UpdateBool('certify',True);
json.UpdateString('certify_signer','Simon Kreuz');
json.UpdateString('incoterm','DDU');
json.UpdateString('items[0].description','T-shirt');
json.UpdateInt('items[0].quantity',20);
json.UpdateString('items[0].net_weight','5');
json.UpdateString('items[0].mass_unit','lb');
json.UpdateString('items[0].value_amount','200');
json.UpdateString('items[0].value_currency','USD');
json.UpdateString('items[0].tariff_number','');
json.UpdateString('items[0].origin_country','US');
http.SetRequestHeader('Authorization','ShippoToken <API_TOKEN>');
http.SetRequestHeader('Content-Type','application/json');
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://api.goshippo.com/customs/declarations/',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_created": "2019-07-04T16:00:29.043Z",
// "object_updated": "2019-07-04T16:00:29.043Z",
// "object_id": "4a1e6ab7b1ba49ed9bc6cb1a8798e0fd",
// "object_owner": "admin@chilkatsoft.com",
// "object_state": "VALID",
// "address_importer": null,
// "certify_signer": "Simon Kreuz",
// "certify": true,
// "items": [
// "4096c68693364b7ea0af72fb869ee861"
// ],
// "non_delivery_option": "RETURN",
// "contents_type": "MERCHANDISE",
// "contents_explanation": "",
// "exporter_reference": "",
// "importer_reference": "",
// "invoice": "",
// "commercial_invoice": false,
// "license": "",
// "certificate": "",
// "notes": "",
// "eel_pfc": "",
// "aes_itn": "",
// "disclaimer": "",
// "incoterm": "DDU",
// "metadata": "",
// "test": true,
// "duties_payor": 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 := jResp.StringOf('object_created');
object_updated := jResp.StringOf('object_updated');
object_id := jResp.StringOf('object_id');
object_owner := jResp.StringOf('object_owner');
object_state := jResp.StringOf('object_state');
address_importer := jResp.StringOf('address_importer');
certify_signer := jResp.StringOf('certify_signer');
certify := jResp.BoolOf('certify');
non_delivery_option := jResp.StringOf('non_delivery_option');
contents_type := jResp.StringOf('contents_type');
contents_explanation := jResp.StringOf('contents_explanation');
exporter_reference := jResp.StringOf('exporter_reference');
importer_reference := jResp.StringOf('importer_reference');
invoice := jResp.StringOf('invoice');
commercial_invoice := jResp.BoolOf('commercial_invoice');
license := jResp.StringOf('license');
certificate := jResp.StringOf('certificate');
notes := jResp.StringOf('notes');
eel_pfc := jResp.StringOf('eel_pfc');
aes_itn := jResp.StringOf('aes_itn');
disclaimer := jResp.StringOf('disclaimer');
incoterm := jResp.StringOf('incoterm');
metadata := jResp.StringOf('metadata');
test := jResp.BoolOf('test');
duties_payor := jResp.StringOf('duties_payor');
i := 0;
count_i := jResp.SizeOfArray('items');
while i < count_i do
begin
jResp.I := i;
strVal := jResp.StringOf('items[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.