Tcl
Tcl
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set http [new_CkHttp]
# 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"
# }
# ]
# }
set json [new_CkJsonObject]
CkJsonObject_UpdateString $json "contents_type" "MERCHANDISE"
CkJsonObject_UpdateString $json "non_delivery_option" "RETURN"
CkJsonObject_UpdateBool $json "certify" 1
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"
set resp [new_CkHttpResponse]
set success [CkHttp_HttpJson $http "POST" "https://api.goshippo.com/customs/declarations/" $json "application/json" $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkJsonObject $json
delete_CkHttpResponse $resp
exit
}
set sbResponseBody [new_CkStringBuilder]
CkHttpResponse_GetBodySb $resp $sbResponseBody
set jResp [new_CkJsonObject]
CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0
puts "Response Body:"
puts [CkJsonObject_emit $jResp]
set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
puts "Response Header:"
puts [CkHttpResponse_header $resp]
puts "Failed."
delete_CkHttp $http
delete_CkJsonObject $json
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp
exit
}
# 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
set object_created [CkJsonObject_stringOf $jResp "object_created"]
set object_updated [CkJsonObject_stringOf $jResp "object_updated"]
set object_id [CkJsonObject_stringOf $jResp "object_id"]
set object_owner [CkJsonObject_stringOf $jResp "object_owner"]
set status [CkJsonObject_stringOf $jResp "status"]
set address_fromObject_id [CkJsonObject_stringOf $jResp "address_from.object_id"]
set address_fromIs_complete [CkJsonObject_BoolOf $jResp "address_from.is_complete"]
set address_fromCompany [CkJsonObject_stringOf $jResp "address_from.company"]
set address_fromStreet_no [CkJsonObject_stringOf $jResp "address_from.street_no"]
set address_fromName [CkJsonObject_stringOf $jResp "address_from.name"]
set address_fromStreet1 [CkJsonObject_stringOf $jResp "address_from.street1"]
set address_fromStreet2 [CkJsonObject_stringOf $jResp "address_from.street2"]
set address_fromCity [CkJsonObject_stringOf $jResp "address_from.city"]
set address_fromState [CkJsonObject_stringOf $jResp "address_from.state"]
set address_fromZip [CkJsonObject_stringOf $jResp "address_from.zip"]
set address_fromCountry [CkJsonObject_stringOf $jResp "address_from.country"]
set address_fromPhone [CkJsonObject_stringOf $jResp "address_from.phone"]
set address_fromEmail [CkJsonObject_stringOf $jResp "address_from.email"]
set address_fromIs_residential [CkJsonObject_stringOf $jResp "address_from.is_residential"]
set address_toObject_id [CkJsonObject_stringOf $jResp "address_to.object_id"]
set address_toIs_complete [CkJsonObject_BoolOf $jResp "address_to.is_complete"]
set address_toName [CkJsonObject_stringOf $jResp "address_to.name"]
set address_toStreet1 [CkJsonObject_stringOf $jResp "address_to.street1"]
set address_toCity [CkJsonObject_stringOf $jResp "address_to.city"]
set address_toState [CkJsonObject_stringOf $jResp "address_to.state"]
set address_toZip [CkJsonObject_stringOf $jResp "address_to.zip"]
set address_toCountry [CkJsonObject_stringOf $jResp "address_to.country"]
set address_toPhone [CkJsonObject_stringOf $jResp "address_to.phone"]
set address_toEmail [CkJsonObject_stringOf $jResp "address_to.email"]
set address_toIs_residential [CkJsonObject_BoolOf $jResp "address_to.is_residential"]
set address_return [CkJsonObject_stringOf $jResp "address_return"]
set shipment_date [CkJsonObject_stringOf $jResp "shipment_date"]
set extraInsuranceCurrency [CkJsonObject_stringOf $jResp "extra.insurance.currency"]
set extraInsuranceAmount [CkJsonObject_stringOf $jResp "extra.insurance.amount"]
set extraReference_1 [CkJsonObject_stringOf $jResp "extra.reference_1"]
set extraReference_2 [CkJsonObject_stringOf $jResp "extra.reference_2"]
set customs_declaration [CkJsonObject_stringOf $jResp "customs_declaration"]
set metadata [CkJsonObject_stringOf $jResp "metadata"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "parcels"]
while {$i < $count_i} {
CkJsonObject_put_I $jResp $i
set object_id [CkJsonObject_stringOf $jResp "parcels[i].object_id"]
set object_created [CkJsonObject_stringOf $jResp "parcels[i].object_created"]
set object_updated [CkJsonObject_stringOf $jResp "parcels[i].object_updated"]
set object_owner [CkJsonObject_stringOf $jResp "parcels[i].object_owner"]
set template [CkJsonObject_stringOf $jResp "parcels[i].template"]
set length [CkJsonObject_stringOf $jResp "parcels[i].length"]
set width [CkJsonObject_stringOf $jResp "parcels[i].width"]
set height [CkJsonObject_stringOf $jResp "parcels[i].height"]
set distance_unit [CkJsonObject_stringOf $jResp "parcels[i].distance_unit"]
set weight [CkJsonObject_stringOf $jResp "parcels[i].weight"]
set mass_unit [CkJsonObject_stringOf $jResp "parcels[i].mass_unit"]
set value_amount [CkJsonObject_stringOf $jResp "parcels[i].value_amount"]
set value_currency [CkJsonObject_stringOf $jResp "parcels[i].value_currency"]
set metadata [CkJsonObject_stringOf $jResp "parcels[i].metadata"]
set test [CkJsonObject_BoolOf $jResp "parcels[i].test"]
set j 0
set count_j [CkJsonObject_SizeOfArray $jResp "parcels[i].line_items"]
while {$j < $count_j} {
CkJsonObject_put_J $jResp $j
set j [expr $j + 1]
}
set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "rates"]
while {$i < $count_i} {
CkJsonObject_put_I $jResp $i
set intVal [CkJsonObject_IntOf $jResp "rates[i]"]
set j 0
set count_j [CkJsonObject_SizeOfArray $jResp "rates[i].attributes"]
while {$j < $count_j} {
CkJsonObject_put_J $jResp $j
set j [expr $j + 1]
}
set j 0
set count_j [CkJsonObject_SizeOfArray $jResp "rates[i].messages"]
while {$j < $count_j} {
CkJsonObject_put_J $jResp $j
set j [expr $j + 1]
}
set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "carrier_accounts"]
while {$i < $count_i} {
CkJsonObject_put_I $jResp $i
set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "messages"]
while {$i < $count_i} {
CkJsonObject_put_I $jResp $i
set i [expr $i + 1]
}
delete_CkHttp $http
delete_CkJsonObject $json
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp