Sample code for 30+ languages & platforms
Zig

Shippo Validate Existing Address

See more Shippo Examples

Demonstrates how to validate an existing address.

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/addresses/e9dabe9669334b84baad3a08646b2202/validate/ \
    //     -H "Authorization: ShippoToken <API_TOKEN>"

    http.setRequestHeader("Authorization", "ShippoToken <API_TOKEN>");

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.goshippo.com/addresses/e9dabe9669334b84baad3a08646b2202/validate/", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    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 = http.getLastStatus();
    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 http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "object_created": "2015-03-30T23:47:11.574Z",
    //   "object_updated": "2015-03-30T23:47:11.596Z",
    //   "object_id": "67183b2e81e9421f894bfbcdc4236b16",
    //   "is_complete": false,
    //   "validation_results": {
    //     "is_valid": false,
    //     "messages": [
    //       {
    //         "source": "USPS",
    //         "code": "Address Not Found",
    //         "type": "address_error",
    //         "text": "The address as submitted could not be found. Please check for excessive abbreviations in the street address line or in the City name."
    //       }
    //     ]
    //   },
    //   "object_owner": "shippotle@goshippo.com",
    //   "name": "Shawn Ippotle",
    //   "company": "Shippo",
    //   "street_no": "",
    //   "street1": "215 HIPPO ST.",
    //   "street2": "",
    //   "city": "SAN FRANCISCO",
    //   "state": "CA",
    //   "zip": "94107",
    //   "country": "US",
    //   "phone": ""
    // }

    // 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 source: [:0]const u8 = "";
    var code: [:0]const u8 = "";
    var v_type: [:0]const u8 = "";
    var text: [:0]const u8 = "";

    _ = try j_resp.stringOf(alloc, "object_created");
    _ = try j_resp.stringOf(alloc, "object_updated");
    _ = try j_resp.stringOf(alloc, "object_id");
    _ = j_resp.boolOf("is_complete");
    _ = j_resp.boolOf("validation_results.is_valid");
    _ = try j_resp.stringOf(alloc, "object_owner");
    _ = try j_resp.stringOf(alloc, "name");
    _ = try j_resp.stringOf(alloc, "company");
    _ = try j_resp.stringOf(alloc, "street_no");
    _ = try j_resp.stringOf(alloc, "street1");
    _ = try j_resp.stringOf(alloc, "street2");
    _ = try j_resp.stringOf(alloc, "city");
    _ = try j_resp.stringOf(alloc, "state");
    _ = try j_resp.stringOf(alloc, "zip");
    _ = try j_resp.stringOf(alloc, "country");
    _ = try j_resp.stringOf(alloc, "phone");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("validation_results.messages");
    while (i < count_i) {
        j_resp.setI(i);
        source = try j_resp.stringOf(alloc, "validation_results.messages[i].source");
        code = try j_resp.stringOf(alloc, "validation_results.messages[i].code");
        v_type = try j_resp.stringOf(alloc, "validation_results.messages[i].type");
        text = try j_resp.stringOf(alloc, "validation_results.messages[i].text");
        i = i + 1;
    }
}