Zig
Zig
Shippo Create a Batch
See more Shippo Examples
Demonstrates how to create and purchase up to 10,000 shipments in a single API request.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();
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "metadata": "BATCH #170",
// "batch_shipments": [
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "965 Mission St",
// "street2": "Ste 201",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "oz"
// }
// ]
// }
// },
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "1092 Indian Summer Ct",
// "city": "San Jose",
// "state": "CA",
// "zip": "95122",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "20",
// "mass_unit": "lb"
// }
// ]
// },
// "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3",
// "servicelevel_token": "fedex_2_day"
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("default_carrier_account", "078870331023437cb917f5187429b093") catch {};
json.updateString("default_servicelevel_token", "usps_priority") catch {};
json.updateString("label_filetype", "PDF_4x6") catch {};
json.updateString("metadata", "BATCH #170") catch {};
json.updateString("batch_shipments[0].shipment.address_from.name", "Mr Hippo") catch {};
json.updateString("batch_shipments[0].shipment.address_from.street1", "965 Mission St") catch {};
json.updateString("batch_shipments[0].shipment.address_from.street2", "Ste 201") catch {};
json.updateString("batch_shipments[0].shipment.address_from.city", "San Francisco") catch {};
json.updateString("batch_shipments[0].shipment.address_from.state", "CA") catch {};
json.updateString("batch_shipments[0].shipment.address_from.zip", "94103") catch {};
json.updateString("batch_shipments[0].shipment.address_from.country", "US") catch {};
json.updateString("batch_shipments[0].shipment.address_from.phone", "4151234567") catch {};
json.updateString("batch_shipments[0].shipment.address_from.email", "mrhippo@goshippo.com") catch {};
json.updateString("batch_shipments[0].shipment.address_to.name", "Mrs Hippo") catch {};
json.updateString("batch_shipments[0].shipment.address_to.company", "") catch {};
json.updateString("batch_shipments[0].shipment.address_to.street1", "Broadway 1") catch {};
json.updateString("batch_shipments[0].shipment.address_to.street2", "") catch {};
json.updateString("batch_shipments[0].shipment.address_to.city", "New York") catch {};
json.updateString("batch_shipments[0].shipment.address_to.state", "NY") catch {};
json.updateString("batch_shipments[0].shipment.address_to.zip", "10007") catch {};
json.updateString("batch_shipments[0].shipment.address_to.country", "US") catch {};
json.updateString("batch_shipments[0].shipment.address_to.phone", "4151234567") catch {};
json.updateString("batch_shipments[0].shipment.address_to.email", "mrshippo@goshippo.com") catch {};
json.updateString("batch_shipments[0].shipment.parcels[0].length", "5") catch {};
json.updateString("batch_shipments[0].shipment.parcels[0].width", "5") catch {};
json.updateString("batch_shipments[0].shipment.parcels[0].height", "5") catch {};
json.updateString("batch_shipments[0].shipment.parcels[0].distance_unit", "in") catch {};
json.updateString("batch_shipments[0].shipment.parcels[0].weight", "2") catch {};
json.updateString("batch_shipments[0].shipment.parcels[0].mass_unit", "oz") catch {};
json.updateString("batch_shipments[1].shipment.address_from.name", "Mr Hippo") catch {};
json.updateString("batch_shipments[1].shipment.address_from.street1", "1092 Indian Summer Ct") catch {};
json.updateString("batch_shipments[1].shipment.address_from.city", "San Jose") catch {};
json.updateString("batch_shipments[1].shipment.address_from.state", "CA") catch {};
json.updateString("batch_shipments[1].shipment.address_from.zip", "95122") catch {};
json.updateString("batch_shipments[1].shipment.address_from.country", "US") catch {};
json.updateString("batch_shipments[1].shipment.address_from.phone", "4151234567") catch {};
json.updateString("batch_shipments[1].shipment.address_from.email", "mrhippo@goshippo.com") catch {};
json.updateString("batch_shipments[1].shipment.address_to.name", "Mrs Hippo") catch {};
json.updateString("batch_shipments[1].shipment.address_to.company", "") catch {};
json.updateString("batch_shipments[1].shipment.address_to.street1", "Broadway 1") catch {};
json.updateString("batch_shipments[1].shipment.address_to.street2", "") catch {};
json.updateString("batch_shipments[1].shipment.address_to.city", "New York") catch {};
json.updateString("batch_shipments[1].shipment.address_to.state", "NY") catch {};
json.updateString("batch_shipments[1].shipment.address_to.zip", "10007") catch {};
json.updateString("batch_shipments[1].shipment.address_to.country", "US") catch {};
json.updateString("batch_shipments[1].shipment.address_to.phone", "4151234567") catch {};
json.updateString("batch_shipments[1].shipment.address_to.email", "mrshippo@goshippo.com") catch {};
json.updateString("batch_shipments[1].shipment.parcels[0].length", "5") catch {};
json.updateString("batch_shipments[1].shipment.parcels[0].width", "5") catch {};
json.updateString("batch_shipments[1].shipment.parcels[0].height", "5") catch {};
json.updateString("batch_shipments[1].shipment.parcels[0].distance_unit", "in") catch {};
json.updateString("batch_shipments[1].shipment.parcels[0].weight", "20") catch {};
json.updateString("batch_shipments[1].shipment.parcels[0].mass_unit", "lb") catch {};
json.updateString("batch_shipments[1].carrier_account", "a4391cd4ab974f478f55dc08b5c8e3b3") catch {};
json.updateString("batch_shipments[1].servicelevel_token", "fedex_2_day") catch {};
http.setRequestHeader("Authorization", "ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a");
http.setRequestHeader("Content-Type", "application/json");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://api.goshippo.com/batches/", 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_id": "a015eb693cca465dbb6523ce6d2e3c65",
// "object_owner": "admin",
// "status": "VALIDATING",
// "object_created": "2016-09-12T15:25:43.465Z",
// "object_updated": "2016-09-12T15:25:43.465Z",
// "metadata": "BATCH #170",
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "batch_shipments": {
// "next": null,
// "previous": null,
// "results": [
// ]
// },
// "object_results": {
// "purchase_succeeded": 0,
// "purchase_failed": 0,
// "creation_failed": 0,
// "creation_succeeded": 0
// },
// "label_url": [
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
_ = try j_resp.stringOf(alloc, "object_id");
_ = try j_resp.stringOf(alloc, "object_owner");
_ = try j_resp.stringOf(alloc, "status");
_ = try j_resp.stringOf(alloc, "object_created");
_ = try j_resp.stringOf(alloc, "object_updated");
_ = try j_resp.stringOf(alloc, "metadata");
_ = try j_resp.stringOf(alloc, "default_carrier_account");
_ = try j_resp.stringOf(alloc, "default_servicelevel_token");
_ = try j_resp.stringOf(alloc, "label_filetype");
_ = try j_resp.stringOf(alloc, "batch_shipments.next");
_ = try j_resp.stringOf(alloc, "batch_shipments.previous");
_ = j_resp.intOf("object_results.purchase_succeeded");
_ = j_resp.intOf("object_results.purchase_failed");
_ = j_resp.intOf("object_results.creation_failed");
_ = j_resp.intOf("object_results.creation_succeeded");
var i: i32 = 0;
var count_i: i32 = j_resp.sizeOfArray("batch_shipments.results");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("label_url");
while (i < count_i) {
j_resp.setI(i);
i = i + 1;
}
}