Zig
Zig
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// 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"
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("contents_type", "MERCHANDISE") catch {};
json.updateString("non_delivery_option", "RETURN") catch {};
json.updateBool("certify", true) catch {};
json.updateString("certify_signer", "Simon Kreuz") catch {};
json.updateString("incoterm", "DDU") catch {};
json.updateString("items[0].description", "T-shirt") catch {};
json.updateInt("items[0].quantity", 20) catch {};
json.updateString("items[0].net_weight", "5") catch {};
json.updateString("items[0].mass_unit", "lb") catch {};
json.updateString("items[0].value_amount", "200") catch {};
json.updateString("items[0].value_currency", "USD") catch {};
json.updateString("items[0].tariff_number", "") catch {};
json.updateString("items[0].origin_country", "US") catch {};
http.setRequestHeader("Authorization", "ShippoToken <API_TOKEN>");
http.setRequestHeader("Content-Type", "application/json");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://api.goshippo.com/customs/declarations/", json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = resp.getStatusCode();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("Failed.\n", .{});
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
var template: [:0]const u8 = "";
var length: [:0]const u8 = "";
var width: [:0]const u8 = "";
var height: [:0]const u8 = "";
var distance_unit: [:0]const u8 = "";
var weight: [:0]const u8 = "";
var mass_unit: [:0]const u8 = "";
var value_amount: [:0]const u8 = "";
var value_currency: [:0]const u8 = "";
var test_: bool = false;
var j: i32 = 0;
var count_j: i32 = 0;
var int_val: i32 = 0;
var object_created: [:0]const u8 = try j_resp.stringOf(alloc, "object_created");
var object_updated: [:0]const u8 = try j_resp.stringOf(alloc, "object_updated");
var object_id: [:0]const u8 = try j_resp.stringOf(alloc, "object_id");
var object_owner: [:0]const u8 = try j_resp.stringOf(alloc, "object_owner");
_ = try j_resp.stringOf(alloc, "status");
_ = try j_resp.stringOf(alloc, "address_from.object_id");
_ = j_resp.boolOf("address_from.is_complete");
_ = try j_resp.stringOf(alloc, "address_from.company");
_ = try j_resp.stringOf(alloc, "address_from.street_no");
_ = try j_resp.stringOf(alloc, "address_from.name");
_ = try j_resp.stringOf(alloc, "address_from.street1");
_ = try j_resp.stringOf(alloc, "address_from.street2");
_ = try j_resp.stringOf(alloc, "address_from.city");
_ = try j_resp.stringOf(alloc, "address_from.state");
_ = try j_resp.stringOf(alloc, "address_from.zip");
_ = try j_resp.stringOf(alloc, "address_from.country");
_ = try j_resp.stringOf(alloc, "address_from.phone");
_ = try j_resp.stringOf(alloc, "address_from.email");
_ = try j_resp.stringOf(alloc, "address_from.is_residential");
_ = try j_resp.stringOf(alloc, "address_to.object_id");
_ = j_resp.boolOf("address_to.is_complete");
_ = try j_resp.stringOf(alloc, "address_to.name");
_ = try j_resp.stringOf(alloc, "address_to.street1");
_ = try j_resp.stringOf(alloc, "address_to.city");
_ = try j_resp.stringOf(alloc, "address_to.state");
_ = try j_resp.stringOf(alloc, "address_to.zip");
_ = try j_resp.stringOf(alloc, "address_to.country");
_ = try j_resp.stringOf(alloc, "address_to.phone");
_ = try j_resp.stringOf(alloc, "address_to.email");
_ = j_resp.boolOf("address_to.is_residential");
_ = try j_resp.stringOf(alloc, "address_return");
_ = try j_resp.stringOf(alloc, "shipment_date");
_ = try j_resp.stringOf(alloc, "extra.insurance.currency");
_ = try j_resp.stringOf(alloc, "extra.insurance.amount");
_ = try j_resp.stringOf(alloc, "extra.reference_1");
_ = try j_resp.stringOf(alloc, "extra.reference_2");
_ = try j_resp.stringOf(alloc, "customs_declaration");
var metadata: [:0]const u8 = try j_resp.stringOf(alloc, "metadata");
var i: i32 = 0;
var count_i: i32 = j_resp.sizeOfArray("parcels");
while (i < count_i) {
j_resp.setI(i);
object_id = try j_resp.stringOf(alloc, "parcels[i].object_id");
object_created = try j_resp.stringOf(alloc, "parcels[i].object_created");
object_updated = try j_resp.stringOf(alloc, "parcels[i].object_updated");
object_owner = try j_resp.stringOf(alloc, "parcels[i].object_owner");
template = try j_resp.stringOf(alloc, "parcels[i].template");
length = try j_resp.stringOf(alloc, "parcels[i].length");
width = try j_resp.stringOf(alloc, "parcels[i].width");
height = try j_resp.stringOf(alloc, "parcels[i].height");
distance_unit = try j_resp.stringOf(alloc, "parcels[i].distance_unit");
weight = try j_resp.stringOf(alloc, "parcels[i].weight");
mass_unit = try j_resp.stringOf(alloc, "parcels[i].mass_unit");
value_amount = try j_resp.stringOf(alloc, "parcels[i].value_amount");
value_currency = try j_resp.stringOf(alloc, "parcels[i].value_currency");
metadata = try j_resp.stringOf(alloc, "parcels[i].metadata");
test_ = j_resp.boolOf("parcels[i].test");
j = 0;
count_j = j_resp.sizeOfArray("parcels[i].line_items");
while (j < count_j) {
j_resp.setJ(j);
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("rates");
while (i < count_i) {
j_resp.setI(i);
int_val = j_resp.intOf("rates[i]");
j = 0;
count_j = j_resp.sizeOfArray("rates[i].attributes");
while (j < count_j) {
j_resp.setJ(j);
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("rates[i].messages");
while (j < count_j) {
j_resp.setJ(j);
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("carrier_accounts");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("messages");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
}