Sample code for 30+ languages & platforms
Zig

Shippo POST to Transaction Endpoint

See more Shippo Examples

Demonstrates how to POST to the Transaction endpoint with your Rate object_id to purchase your international shipping label.

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>" \
    //     -d rate="ec6143e746094bcab1bdc4c17600dabf"
    //     -d label_file_type="PDF"
    //     -d async=false

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/transactions");
    req.setContentType("application/x-www-form-urlencoded");
    req.addParam("rate", "ec6143e746094bcab1bdc4c17600dabf");
    req.addParam("label_file_type", "PDF");
    req.addParam("async", "false");

    req.addHeader("Authorization", "ShippoToken <API_TOKEN>");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://api.goshippo.com/transactions", req, 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;
    }
}