Zig
Zig
Shippo Create Shipment Object
See more Shippo Examples
Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address.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/shipments/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -H "Content-Type: application/json" \
// -d '{
// "address_from":{
// "name":"Mr. Hippo",
// "street1":"215 Clayton St.",
// "city":"San Francisco",
// "state":"CA",
// "zip":"94117",
// "country":"US"
// },
// "address_to":{
// "name":"Mrs. Hippo",
// "street1":"965 Mission St.",
// "city":"San Francisco",
// "state":"CA",
// "zip":"94105",
// "country":"US"
// },
// "parcels":[{
// "length":"5",
// "width":"5",
// "height":"5",
// "distance_unit":"in",
// "weight":"2",
// "mass_unit":"lb"
// }],
// "async": false
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "address_from": {
// "name": "Mr. Hippo",
// "street1": "215 Clayton St.",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US"
// },
// "address_to": {
// "name": "Mrs. Hippo",
// "street1": "965 Mission St.",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94105",
// "country": "US"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "lb"
// }
// ],
// "async": false
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("address_from.name", "Mr. Hippo") catch {};
json.updateString("address_from.street1", "215 Clayton St.") catch {};
json.updateString("address_from.city", "San Francisco") catch {};
json.updateString("address_from.state", "CA") catch {};
json.updateString("address_from.zip", "94117") catch {};
json.updateString("address_from.country", "US") catch {};
json.updateString("address_to.name", "Mrs. Hippo") catch {};
json.updateString("address_to.street1", "965 Mission St.") catch {};
json.updateString("address_to.city", "San Francisco") catch {};
json.updateString("address_to.state", "CA") catch {};
json.updateString("address_to.zip", "94105") catch {};
json.updateString("address_to.country", "US") catch {};
json.updateString("parcels[0].length", "5") catch {};
json.updateString("parcels[0].width", "5") catch {};
json.updateString("parcels[0].height", "5") catch {};
json.updateString("parcels[0].distance_unit", "in") catch {};
json.updateString("parcels[0].weight", "2") catch {};
json.updateString("parcels[0].mass_unit", "lb") catch {};
json.updateBool("async", false) 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/shipments/", 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)
// {
// "carrier_accounts": [
// ],
// "object_created": "2019-06-28T17:45:20.441Z",
// "object_updated": "2019-06-28T17:45:20.511Z",
// "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
// "object_owner": "admin@chilkatsoft.com",
// "status": "SUCCESS",
// "address_from": {
// "object_id": "427489906b604f498f7bd2429ab7d9a1",
// "is_complete": true,
// "name": "Mr. Hippo",
// "company": "",
// "street_no": "",
// "street1": "215 Clayton St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "address_to": {
// "object_id": "3d7166269a2844b7842ef9ace942ec7c",
// "is_complete": true,
// "name": "Mrs. Hippo",
// "company": "",
// "street_no": "",
// "street1": "965 Mission St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94105",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "parcels": [
// {
// "object_state": "VALID",
// "object_created": "2019-06-28T17:45:20.411Z",
// "object_updated": "2019-06-28T17:45:20.455Z",
// "object_id": "5af766ff15684a4186b0e3c833348fac",
// "object_owner": "admin@chilkatsoft.com",
// "template": null,
// "extra": {},
// "length": "5.0000",
// "width": "5.0000",
// "height": "5.0000",
// "distance_unit": "in",
// "weight": "2.0000",
// "mass_unit": "lb",
// "value_amount": null,
// "value_currency": null,
// "metadata": "",
// "line_items": [
// ],
// "test": true
// }
// ],
// "shipment_date": "2019-06-28T17:45:20.511Z",
// "address_return": {
// "object_id": "427489906b604f498f7bd2429ab7d9a1",
// "is_complete": true,
// "name": "Mr. Hippo",
// "company": "",
// "street_no": "",
// "street1": "215 Clayton St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "alternate_address_to": null,
// "customs_declaration": null,
// "extra": {},
// "rates": [
// {
// "object_created": "2019-06-28T17:45:20.789Z",
// "object_id": "dd7556c284e8444294d2ab7099e662d2",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// "FASTEST"
// ],
// "amount": "22.78",
// "currency": "USD",
// "amount_local": "22.78",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail Express",
// "token": "usps_priority_express",
// "terms": ""
// },
// "estimated_days": 2,
// "arrives_by": null,
// "duration_terms": "Overnight delivery to most U.S. locations.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// },
// {
// "object_created": "2019-06-28T17:45:20.786Z",
// "object_id": "ec11cc297e2f4583986097f6d409e5c6",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// "BESTVALUE",
// "CHEAPEST",
// "FASTEST"
// ],
// "amount": "6.95",
// "currency": "USD",
// "amount_local": "6.95",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail",
// "token": "usps_priority",
// "terms": ""
// },
// "estimated_days": 2,
// "arrives_by": null,
// "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// },
// {
// "object_created": "2019-06-28T17:45:20.785Z",
// "object_id": "d256586f539f4c93a2f02cc11f4942ac",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// ],
// "amount": "7.32",
// "currency": "USD",
// "amount_local": "7.32",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Parcel Select",
// "token": "usps_parcel_select",
// "terms": ""
// },
// "estimated_days": 7,
// "arrives_by": null,
// "duration_terms": "Delivery in 2 to 8 days.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// }
// ],
// "messages": [
// {
// "source": "DHLExpress",
// "code": "",
// "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
// }
// ],
// "metadata": "",
// "test": true,
// "order": 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
var object_state: [:0]const u8 = "";
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 j: i32 = 0;
var count_j: i32 = 0;
var shipment: [:0]const u8 = "";
var amount: [:0]const u8 = "";
var currency: [:0]const u8 = "";
var amount_local: [:0]const u8 = "";
var currency_local: [:0]const u8 = "";
var provider: [:0]const u8 = "";
var provider_image_75: [:0]const u8 = "";
var provider_image_200: [:0]const u8 = "";
var servicelevel_name: [:0]const u8 = "";
var servicelevel_token: [:0]const u8 = "";
var servicelevel_terms: [:0]const u8 = "";
var estimated_days: i32 = 0;
var arrives_by: [:0]const u8 = "";
var duration_terms: [:0]const u8 = "";
var carrier_account: [:0]const u8 = "";
var zone: [:0]const u8 = "";
var str_val: [:0]const u8 = "";
var source: [:0]const u8 = "";
var code: [:0]const u8 = "";
var text: [:0]const u8 = "";
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.name");
_ = try j_resp.stringOf(alloc, "address_from.company");
_ = try j_resp.stringOf(alloc, "address_from.street_no");
_ = try j_resp.stringOf(alloc, "address_from.street1");
_ = try j_resp.stringOf(alloc, "address_from.street2");
_ = try j_resp.stringOf(alloc, "address_from.street3");
_ = 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");
_ = j_resp.boolOf("address_from.test");
_ = 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.company");
_ = try j_resp.stringOf(alloc, "address_to.street_no");
_ = try j_resp.stringOf(alloc, "address_to.street1");
_ = try j_resp.stringOf(alloc, "address_to.street2");
_ = try j_resp.stringOf(alloc, "address_to.street3");
_ = 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");
_ = try j_resp.stringOf(alloc, "address_to.is_residential");
_ = j_resp.boolOf("address_to.test");
_ = try j_resp.stringOf(alloc, "shipment_date");
_ = try j_resp.stringOf(alloc, "address_return.object_id");
_ = j_resp.boolOf("address_return.is_complete");
_ = try j_resp.stringOf(alloc, "address_return.name");
_ = try j_resp.stringOf(alloc, "address_return.company");
_ = try j_resp.stringOf(alloc, "address_return.street_no");
_ = try j_resp.stringOf(alloc, "address_return.street1");
_ = try j_resp.stringOf(alloc, "address_return.street2");
_ = try j_resp.stringOf(alloc, "address_return.street3");
_ = try j_resp.stringOf(alloc, "address_return.city");
_ = try j_resp.stringOf(alloc, "address_return.state");
_ = try j_resp.stringOf(alloc, "address_return.zip");
_ = try j_resp.stringOf(alloc, "address_return.country");
_ = try j_resp.stringOf(alloc, "address_return.phone");
_ = try j_resp.stringOf(alloc, "address_return.email");
_ = try j_resp.stringOf(alloc, "address_return.is_residential");
_ = j_resp.boolOf("address_return.test");
_ = try j_resp.stringOf(alloc, "alternate_address_to");
_ = try j_resp.stringOf(alloc, "customs_declaration");
var metadata: [:0]const u8 = try j_resp.stringOf(alloc, "metadata");
var test_: bool = j_resp.boolOf("test");
_ = try j_resp.stringOf(alloc, "order");
var i: i32 = 0;
var count_i: i32 = j_resp.sizeOfArray("carrier_accounts");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("parcels");
while (i < count_i) {
j_resp.setI(i);
object_state = try j_resp.stringOf(alloc, "parcels[i].object_state");
object_created = try j_resp.stringOf(alloc, "parcels[i].object_created");
object_updated = try j_resp.stringOf(alloc, "parcels[i].object_updated");
object_id = try j_resp.stringOf(alloc, "parcels[i].object_id");
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);
object_created = try j_resp.stringOf(alloc, "rates[i].object_created");
object_id = try j_resp.stringOf(alloc, "rates[i].object_id");
object_owner = try j_resp.stringOf(alloc, "rates[i].object_owner");
shipment = try j_resp.stringOf(alloc, "rates[i].shipment");
amount = try j_resp.stringOf(alloc, "rates[i].amount");
currency = try j_resp.stringOf(alloc, "rates[i].currency");
amount_local = try j_resp.stringOf(alloc, "rates[i].amount_local");
currency_local = try j_resp.stringOf(alloc, "rates[i].currency_local");
provider = try j_resp.stringOf(alloc, "rates[i].provider");
provider_image_75 = try j_resp.stringOf(alloc, "rates[i].provider_image_75");
provider_image_200 = try j_resp.stringOf(alloc, "rates[i].provider_image_200");
servicelevel_name = try j_resp.stringOf(alloc, "rates[i].servicelevel.name");
servicelevel_token = try j_resp.stringOf(alloc, "rates[i].servicelevel.token");
servicelevel_terms = try j_resp.stringOf(alloc, "rates[i].servicelevel.terms");
estimated_days = j_resp.intOf("rates[i].estimated_days");
arrives_by = try j_resp.stringOf(alloc, "rates[i].arrives_by");
duration_terms = try j_resp.stringOf(alloc, "rates[i].duration_terms");
carrier_account = try j_resp.stringOf(alloc, "rates[i].carrier_account");
test_ = j_resp.boolOf("rates[i].test");
zone = try j_resp.stringOf(alloc, "rates[i].zone");
j = 0;
count_j = j_resp.sizeOfArray("rates[i].attributes");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "rates[i].attributes[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("messages");
while (i < count_i) {
j_resp.setI(i);
source = try j_resp.stringOf(alloc, "messages[i].source");
code = try j_resp.stringOf(alloc, "messages[i].code");
text = try j_resp.stringOf(alloc, "messages[i].text");
i = i + 1;
}
}