Perl
Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $http = Chilkat2::Http->new;
# 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"
# }
# ]
# }
my $json = Chilkat2::JsonObject->new;
$json->UpdateString('contents_type', 'MERCHANDISE');
$json->UpdateString('non_delivery_option', 'RETURN');
$json->UpdateBool('certify', 1);
$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');
my $resp = Chilkat2::HttpResponse->new;
my $success = $http->HttpJson('POST', 'https://api.goshippo.com/customs/declarations/', $json, 'application/json', $resp);
if (!$success) {
print $http->LastErrorText, "\n";
exit;
}
my $sbResponseBody = Chilkat2::StringBuilder->new;
$resp->GetBodySb($sbResponseBody);
my $jResp = Chilkat2::JsonObject->new;
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact(0);
print "Response Body:\n";
print $jResp->Emit(), "\n";
my $respStatusCode = $resp->StatusCode;
print 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
print "Response Header:\n";
print $resp->Header, "\n";
print "Failed.\n";
exit;
}
# 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
my $strVal = '';
my $object_created = $jResp->StringOf('object_created');
my $object_updated = $jResp->StringOf('object_updated');
my $object_id = $jResp->StringOf('object_id');
my $object_owner = $jResp->StringOf('object_owner');
my $object_state = $jResp->StringOf('object_state');
my $address_importer = $jResp->StringOf('address_importer');
my $certify_signer = $jResp->StringOf('certify_signer');
my $certify = $jResp->BoolOf('certify');
my $non_delivery_option = $jResp->StringOf('non_delivery_option');
my $contents_type = $jResp->StringOf('contents_type');
my $contents_explanation = $jResp->StringOf('contents_explanation');
my $exporter_reference = $jResp->StringOf('exporter_reference');
my $importer_reference = $jResp->StringOf('importer_reference');
my $invoice = $jResp->StringOf('invoice');
my $commercial_invoice = $jResp->BoolOf('commercial_invoice');
my $license = $jResp->StringOf('license');
my $certificate = $jResp->StringOf('certificate');
my $notes = $jResp->StringOf('notes');
my $eel_pfc = $jResp->StringOf('eel_pfc');
my $aes_itn = $jResp->StringOf('aes_itn');
my $disclaimer = $jResp->StringOf('disclaimer');
my $incoterm = $jResp->StringOf('incoterm');
my $metadata = $jResp->StringOf('metadata');
my $test = $jResp->BoolOf('test');
my $duties_payor = $jResp->StringOf('duties_payor');
my $i = 0;
my $count_i = $jResp->SizeOfArray('items');
while ($i < $count_i) {
$jResp->I($i);
$strVal = $jResp->StringOf('items[i]');
$i = $i + 1;
}