Sample code for 30+ languages & platforms
Zig

Shippo Label Purchase

See more Shippo Examples

Demonstrates how to create a shipping label in one API call through Shippo.

Chilkat Zig Downloads

Zig
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/transactions/  \
    //     -H "Authorization: ShippoToken <API_Token>" \
    //     -H "Content-Type: application/json"  \
    //     -d '{
    //         "shipment": {
    //             "address_from": {
    //                 "name": "Mr. Hippo",
    //                 "street1": "215 Clayton St.",
    //                 "city": "San Francisco",
    //                 "state": "CA",
    //                 "zip": "94117",
    //                 "country": "US",
    //                 "phone": "+1 555 341 9393",
    //                 "email": "support@goshippo.com"
    //             },
    //             "address_to": {
    //                 "name": "Mrs. Hippo",
    //                 "street1": "965 Mission St.",
    //                 "city": "San Francisco",
    //                 "state": "CA",
    //                 "zip": "94105",
    //                 "country": "US",
    //                 "phone": "+1 555 341 9393",
    //                 "email": "support@goshippo.com"
    //             },
    //             "parcels": [{
    //                 "length": "5",
    //                 "width": "5",
    //                 "height": "5",
    //                 "distance_unit": "in",
    //                 "weight": "2",
    //                 "mass_unit": "lb"
    //             }]
    //         },
    //         "carrier_account": "b741b99f95e841639b54272834bc478c",
    //         "servicelevel_token": "usps_priority"
    //         }'

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "shipment": {
    //     "address_from": {
    //       "name": "Mr. Hippo",
    //       "street1": "215 Clayton St.",
    //       "city": "San Francisco",
    //       "state": "CA",
    //       "zip": "94117",
    //       "country": "US",
    //       "phone": "+1 555 341 9393",
    //       "email": "support@goshippo.com"
    //     },
    //     "address_to": {
    //       "name": "Mrs. Hippo",
    //       "street1": "965 Mission St.",
    //       "city": "San Francisco",
    //       "state": "CA",
    //       "zip": "94105",
    //       "country": "US",
    //       "phone": "+1 555 341 9393",
    //       "email": "support@goshippo.com"
    //     },
    //     "parcels": [
    //       {
    //         "length": "5",
    //         "width": "5",
    //         "height": "5",
    //         "distance_unit": "in",
    //         "weight": "2",
    //         "mass_unit": "lb"
    //       }
    //     ]
    //   },
    //   "carrier_account": "b741b99f95e841639b54272834bc478c",
    //   "servicelevel_token": "usps_priority"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("shipment.address_from.name", "Mr. Hippo") catch {};
    json.updateString("shipment.address_from.street1", "215 Clayton St.") catch {};
    json.updateString("shipment.address_from.city", "San Francisco") catch {};
    json.updateString("shipment.address_from.state", "CA") catch {};
    json.updateString("shipment.address_from.zip", "94117") catch {};
    json.updateString("shipment.address_from.country", "US") catch {};
    json.updateString("shipment.address_from.phone", "+1 555 341 9393") catch {};
    json.updateString("shipment.address_from.email", "support@goshippo.com") catch {};
    json.updateString("shipment.address_to.name", "Mrs. Hippo") catch {};
    json.updateString("shipment.address_to.street1", "965 Mission St.") catch {};
    json.updateString("shipment.address_to.city", "San Francisco") catch {};
    json.updateString("shipment.address_to.state", "CA") catch {};
    json.updateString("shipment.address_to.zip", "94105") catch {};
    json.updateString("shipment.address_to.country", "US") catch {};
    json.updateString("shipment.address_to.phone", "+1 555 341 9393") catch {};
    json.updateString("shipment.address_to.email", "support@goshippo.com") catch {};
    json.updateString("shipment.parcels[0].length", "5") catch {};
    json.updateString("shipment.parcels[0].width", "5") catch {};
    json.updateString("shipment.parcels[0].height", "5") catch {};
    json.updateString("shipment.parcels[0].distance_unit", "in") catch {};
    json.updateString("shipment.parcels[0].weight", "2") catch {};
    json.updateString("shipment.parcels[0].mass_unit", "lb") catch {};
    json.updateString("carrier_account", "b741b99f95e841639b54272834bc478c") catch {};
    json.updateString("servicelevel_token", "usps_priority") 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/transactions/", 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_state": "VALID",
    //   "status": "SUCCESS",
    //   "object_created": "2013-12-27T19:14:48.273Z",
    //   "object_updated": "2013-12-27T19:14:48.273Z",
    //   "object_id": "64bba01845ef40d29374032599f22588",
    //   "object_owner": "shippotle@goshippo.com",
    //   "was_test": false,
    //   "rate": {
    //     "object_id": "cf6fea899f1848b494d9568e8266e076",
    //     "amount": "5.50",
    //     "currency": "USD",
    //     "amount_local": "5.50",
    //     "currency_local": "USD",
    //     "provider": "USPS",
    //     "servicelevel_name": "Priority Mail",
    //     "servicelevel_token": "usps_priority",
    //     "carrier_account": "078870331023437cb917f5187429b093"
    //   },
    //   "tracking_number": "ZW70QJC",
    //   "tracking_status": {
    //     "object_created": "2013-12-27T23:17:41.411Z",
    //     "object_id": "a21b3d6831c14ceaba6730179ce6e784",
    //     "status": "UNKNOWN",
    //     "status_details": "",
    //     "status_date": "2013-12-28T12:04:04.214Z"
    //   },
    //   "tracking_url_provider": "https://tools.usps.com/go/TrackConfirmAction.action?tLabels=ZW70QJC",
    //   "eta": "2013-12-30T12:00:00.000Z",
    //   "label_url": "https://shippo-delivery.s3.amazonaws.com/96.pdf?Signature=PEdWrp0mFWAGwJp7FW3b%2FeA2eyY%3D&Expires=1385930652&AWSAccessKeyId=AKIAJTHP3LLFMYAWALIA",
    //   "commercial_invoice_url": "",
    //   "metadata": "",
    //   "messages": [
    //   ]
    // }

    // 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_state");
    _ = 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, "object_id");
    _ = try j_resp.stringOf(alloc, "object_owner");
    _ = j_resp.boolOf("was_test");
    _ = try j_resp.stringOf(alloc, "rate.object_id");
    _ = try j_resp.stringOf(alloc, "rate.amount");
    _ = try j_resp.stringOf(alloc, "rate.currency");
    _ = try j_resp.stringOf(alloc, "rate.amount_local");
    _ = try j_resp.stringOf(alloc, "rate.currency_local");
    _ = try j_resp.stringOf(alloc, "rate.provider");
    _ = try j_resp.stringOf(alloc, "rate.servicelevel_name");
    _ = try j_resp.stringOf(alloc, "rate.servicelevel_token");
    _ = try j_resp.stringOf(alloc, "rate.carrier_account");
    _ = try j_resp.stringOf(alloc, "tracking_number");
    _ = try j_resp.stringOf(alloc, "tracking_status.object_created");
    _ = try j_resp.stringOf(alloc, "tracking_status.object_id");
    _ = try j_resp.stringOf(alloc, "tracking_status.status");
    _ = try j_resp.stringOf(alloc, "tracking_status.status_details");
    _ = try j_resp.stringOf(alloc, "tracking_status.status_date");
    _ = try j_resp.stringOf(alloc, "tracking_url_provider");
    _ = try j_resp.stringOf(alloc, "eta");
    _ = try j_resp.stringOf(alloc, "label_url");
    _ = try j_resp.stringOf(alloc, "commercial_invoice_url");
    _ = try j_resp.stringOf(alloc, "metadata");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("messages");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }
}