Sample code for 30+ languages & platforms
Zig

Shippo Create Customs Declaration

See more Shippo Examples

Demonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint.

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/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": "2019-07-04T16:00:29.043Z",
    //   "object_updated": "2019-07-04T16:00:29.043Z",
    //   "object_id": "4a1e6ab7b1ba49ed9bc6cb1a8798e0fd",
    //   "object_owner": "admin@chilkatsoft.com",
    //   "object_state": "VALID",
    //   "address_importer": null,
    //   "certify_signer": "Simon Kreuz",
    //   "certify": true,
    //   "items": [
    //     "4096c68693364b7ea0af72fb869ee861"
    //   ],
    //   "non_delivery_option": "RETURN",
    //   "contents_type": "MERCHANDISE",
    //   "contents_explanation": "",
    //   "exporter_reference": "",
    //   "importer_reference": "",
    //   "invoice": "",
    //   "commercial_invoice": false,
    //   "license": "",
    //   "certificate": "",
    //   "notes": "",
    //   "eel_pfc": "",
    //   "aes_itn": "",
    //   "disclaimer": "",
    //   "incoterm": "DDU",
    //   "metadata": "",
    //   "test": true,
    //   "duties_payor": 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 str_val: [:0]const u8 = "";

    _ = 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");
    _ = try j_resp.stringOf(alloc, "object_state");
    _ = try j_resp.stringOf(alloc, "address_importer");
    _ = try j_resp.stringOf(alloc, "certify_signer");
    _ = j_resp.boolOf("certify");
    _ = try j_resp.stringOf(alloc, "non_delivery_option");
    _ = try j_resp.stringOf(alloc, "contents_type");
    _ = try j_resp.stringOf(alloc, "contents_explanation");
    _ = try j_resp.stringOf(alloc, "exporter_reference");
    _ = try j_resp.stringOf(alloc, "importer_reference");
    _ = try j_resp.stringOf(alloc, "invoice");
    _ = j_resp.boolOf("commercial_invoice");
    _ = try j_resp.stringOf(alloc, "license");
    _ = try j_resp.stringOf(alloc, "certificate");
    _ = try j_resp.stringOf(alloc, "notes");
    _ = try j_resp.stringOf(alloc, "eel_pfc");
    _ = try j_resp.stringOf(alloc, "aes_itn");
    _ = try j_resp.stringOf(alloc, "disclaimer");
    _ = try j_resp.stringOf(alloc, "incoterm");
    _ = try j_resp.stringOf(alloc, "metadata");
    _ = j_resp.boolOf("test");
    _ = try j_resp.stringOf(alloc, "duties_payor");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("items");
    while (i < count_i) {
        j_resp.setI(i);
        str_val = try j_resp.stringOf(alloc, "items[i]");
        i = i + 1;
    }
}