Rust
Rust
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::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"
// }
// ]
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("contents_type", "MERCHANDISE");
let _ = json.update_string("non_delivery_option", "RETURN");
let _ = json.update_bool("certify", true);
let _ = json.update_string("certify_signer", "Simon Kreuz");
let _ = json.update_string("incoterm", "DDU");
let _ = json.update_string("items[0].description", "T-shirt");
let _ = json.update_int("items[0].quantity", 20);
let _ = json.update_string("items[0].net_weight", "5");
let _ = json.update_string("items[0].mass_unit", "lb");
let _ = json.update_string("items[0].value_amount", "200");
let _ = json.update_string("items[0].value_currency", "USD");
let _ = json.update_string("items[0].tariff_number", "");
let _ = json.update_string("items[0].origin_country", "US");
http.set_request_header("Authorization", "ShippoToken <API_TOKEN>");
http.set_request_header("Content-Type", "application/json");
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.goshippo.com/customs/declarations/", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
return;
}
// 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
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut object_created = j_resp.string_of("object_created").unwrap_or_default();
let mut object_updated = j_resp.string_of("object_updated").unwrap_or_default();
let mut object_id = j_resp.string_of("object_id").unwrap_or_default();
let mut object_owner = j_resp.string_of("object_owner").unwrap_or_default();
let status = j_resp.string_of("status").unwrap_or_default();
let address_from_object_id = j_resp.string_of("address_from.object_id").unwrap_or_default();
let address_from_is_complete = j_resp.bool_of("address_from.is_complete");
let address_from_company = j_resp.string_of("address_from.company").unwrap_or_default();
let address_from_street_no = j_resp.string_of("address_from.street_no").unwrap_or_default();
let address_from_name = j_resp.string_of("address_from.name").unwrap_or_default();
let address_from_street1 = j_resp.string_of("address_from.street1").unwrap_or_default();
let address_from_street2 = j_resp.string_of("address_from.street2").unwrap_or_default();
let address_from_city = j_resp.string_of("address_from.city").unwrap_or_default();
let address_from_state = j_resp.string_of("address_from.state").unwrap_or_default();
let address_from_zip = j_resp.string_of("address_from.zip").unwrap_or_default();
let address_from_country = j_resp.string_of("address_from.country").unwrap_or_default();
let address_from_phone = j_resp.string_of("address_from.phone").unwrap_or_default();
let address_from_email = j_resp.string_of("address_from.email").unwrap_or_default();
let address_from_is_residential = j_resp.string_of("address_from.is_residential").unwrap_or_default();
let address_to_object_id = j_resp.string_of("address_to.object_id").unwrap_or_default();
let address_to_is_complete = j_resp.bool_of("address_to.is_complete");
let address_to_name = j_resp.string_of("address_to.name").unwrap_or_default();
let address_to_street1 = j_resp.string_of("address_to.street1").unwrap_or_default();
let address_to_city = j_resp.string_of("address_to.city").unwrap_or_default();
let address_to_state = j_resp.string_of("address_to.state").unwrap_or_default();
let address_to_zip = j_resp.string_of("address_to.zip").unwrap_or_default();
let address_to_country = j_resp.string_of("address_to.country").unwrap_or_default();
let address_to_phone = j_resp.string_of("address_to.phone").unwrap_or_default();
let address_to_email = j_resp.string_of("address_to.email").unwrap_or_default();
let address_to_is_residential = j_resp.bool_of("address_to.is_residential");
let address_return = j_resp.string_of("address_return").unwrap_or_default();
let shipment_date = j_resp.string_of("shipment_date").unwrap_or_default();
let extra_insurance_currency = j_resp.string_of("extra.insurance.currency").unwrap_or_default();
let extra_insurance_amount = j_resp.string_of("extra.insurance.amount").unwrap_or_default();
let extra_reference_1 = j_resp.string_of("extra.reference_1").unwrap_or_default();
let extra_reference_2 = j_resp.string_of("extra.reference_2").unwrap_or_default();
let customs_declaration = j_resp.string_of("customs_declaration").unwrap_or_default();
let mut metadata = j_resp.string_of("metadata").unwrap_or_default();
let mut i = 0;
let mut count_i = j_resp.size_of_array("parcels");
while i < count_i {
j_resp.set_i(i);
object_id = j_resp.string_of("parcels[i].object_id").unwrap_or_default();
object_created = j_resp.string_of("parcels[i].object_created").unwrap_or_default();
object_updated = j_resp.string_of("parcels[i].object_updated").unwrap_or_default();
object_owner = j_resp.string_of("parcels[i].object_owner").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].template").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].length").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].width").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].height").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].distance_unit").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].weight").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].mass_unit").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].value_amount").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].value_currency").unwrap_or_default();
metadata = j_resp.string_of("parcels[i].metadata").unwrap_or_default();
let _ = j_resp.bool_of("parcels[i].test");
j = 0;
count_j = j_resp.size_of_array("parcels[i].line_items");
while j < count_j {
j_resp.set_j(j);
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("rates");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.int_of("rates[i]");
j = 0;
count_j = j_resp.size_of_array("rates[i].attributes");
while j < count_j {
j_resp.set_j(j);
j = j + 1;
}
j = 0;
count_j = j_resp.size_of_array("rates[i].messages");
while j < count_j {
j_resp.set_j(j);
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("carrier_accounts");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("messages");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}