Sample code for 30+ languages & platforms
Zig

Add order to a ShippingEasy account

See more HTTP Misc Examples

Adds an order to a ShippingEasy account (calls the ShippingEasy REST API).

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // First create the JSON for the order.
    // See https://shippingeasy.readme.io/v1.0/docs/creating-a-signature
    // for sample JSON.
    // You may use the online tool at https://tools.chilkat.io/Default.cshtml to
    // generate the following code by pasting sample JSON into the tool.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("order.external_order_identifier", "ABC-100") catch {};
    json.updateString("order.ordered_at", "2014-01-16 14:37:56 -0600") catch {};
    json.updateString("order.order_status", "awaiting_shipment") catch {};
    json.updateString("order.subtotal_including_tax", "10.00") catch {};
    json.updateString("order.total_including_tax", "10.00") catch {};
    json.updateString("order.total_excluding_tax", "10.00") catch {};
    json.updateString("order.discount_amount", "0.00") catch {};
    json.updateString("order.coupon_discount", "1.00") catch {};
    json.updateString("order.subtotal_including_tax", "0.00") catch {};
    json.updateString("order.subtotal_excluding_tax", "0.00") catch {};
    json.updateString("order.subtotal_excluding_tax", "0.00") catch {};
    json.updateString("order.subtotal_tax", "0.00") catch {};
    json.updateString("order.total_tax", "0") catch {};
    json.updateString("order.base_shipping_cost", "0.00") catch {};
    json.updateString("order.shipping_cost_including_tax", "0.00") catch {};
    json.updateString("order.shipping_cost_excluding_tax", "0.00") catch {};
    json.updateString("order.shipping_cost_tax", "0.00") catch {};
    json.updateString("order.base_handling_cost", "0.00") catch {};
    json.updateString("order.handling_cost_excluding_tax", "0.00") catch {};
    json.updateString("order.handling_cost_including_tax", "0.00") catch {};
    json.updateString("order.handling_cost_tax", "0.00") catch {};
    json.updateString("order.base_wrapping_cost", "0.00") catch {};
    json.updateString("order.wrapping_cost_excluding_tax", "0.00") catch {};
    json.updateString("order.wrapping_cost_including_tax", "0.00") catch {};
    json.updateString("order.wrapping_cost_tax", "0.00") catch {};
    json.updateString("order.notes", "Please send promptly.") catch {};
    json.updateString("order.billing_company", "Acme Inc.") catch {};
    json.updateString("order.billing_first_name", "Fred") catch {};
    json.updateString("order.billing_last_name", "Jones") catch {};
    json.updateString("order.billing_address", "1234 Street") catch {};
    json.updateString("order.billing_address2", "Suite 100") catch {};
    json.updateString("order.billing_city", "Austin") catch {};
    json.updateString("order.billing_state", "TX") catch {};
    json.updateString("order.billing_postal_code", "78701") catch {};
    json.updateString("order.billing_country", "USA") catch {};
    json.updateString("order.billing_phone_number", "512-123-1234") catch {};
    json.updateString("order.billing_email", "test@test.com") catch {};
    json.updateString("order.recipients[0].first_name", "Colin") catch {};
    json.updateString("order.recipients[0].last_name", "Homenick") catch {};
    json.updateString("order.recipients[0].company", "Wintheiser-Hickle") catch {};
    json.updateString("order.recipients[0].email", "charles.crona@okeefe.org") catch {};
    json.updateString("order.recipients[0].phone_number", "637-481-6505") catch {};
    json.updateString("order.recipients[0].residential", "true") catch {};
    json.updateString("order.recipients[0].address", "21937 Adelbert Springs") catch {};
    json.updateString("order.recipients[0].address2", "") catch {};
    json.updateString("order.recipients[0].province", "") catch {};
    json.updateString("order.recipients[0].state", "CT") catch {};
    json.updateString("order.recipients[0].city", "Terryfurt") catch {};
    json.updateString("order.recipients[0].postal_code", "93322") catch {};
    json.updateString("order.recipients[0].postal_code_plus_4", "1234") catch {};
    json.updateString("order.recipients[0].country", "Andorra") catch {};
    json.updateString("order.recipients[0].shipping_method", "Ground") catch {};
    json.updateString("order.recipients[0].base_cost", "10.00") catch {};
    json.updateString("order.recipients[0].cost_excluding_tax", "10.00") catch {};
    json.updateString("order.recipients[0].cost_tax", "0.00") catch {};
    json.updateString("order.recipients[0].base_handling_cost", "0.00") catch {};
    json.updateString("order.recipients[0].handling_cost_excluding_tax", "0.00") catch {};
    json.updateString("order.recipients[0].handling_cost_including_tax", "0.00") catch {};
    json.updateString("order.recipients[0].handling_cost_tax", "0.00") catch {};
    json.updateString("order.recipients[0].shipping_zone_id", "123") catch {};
    json.updateString("order.recipients[0].shipping_zone_name", "XYZ") catch {};
    json.updateString("order.recipients[0].items_total", "1") catch {};
    json.updateString("order.recipients[0].items_shipped", "0") catch {};
    json.updateString("order.recipients[0].line_items[0].item_name", "Pencil Holder") catch {};
    json.updateString("order.recipients[0].line_items[0].sku", "9876543") catch {};
    json.updateString("order.recipients[0].line_items[0].bin_picking_number", "7") catch {};
    json.updateString("order.recipients[0].line_items[0].unit_price", "1.30") catch {};
    json.updateString("order.recipients[0].line_items[0].total_excluding_tax", "1.30") catch {};
    json.updateString("order.recipients[0].line_items[0].weight_in_ounces", "10") catch {};
    json.updateString("order.recipients[0].line_items[0].product_options.pa_size", "large") catch {};
    json.updateString("order.recipients[0].line_items[0].product_options.Colour", "Blue") catch {};
    json.updateString("order.recipients[0].line_items[0].quantity", "1") catch {};

    // Replace these values with your actual apiKey and apiSecret.
    // (These are not valid values..)
    const store_api_key = "569dc30d267f2c4d1f85f9920b2bf9f4";
    const api_key = "f9a7c8ebdfd34beaf260d9b0296c7059";
    const api_secret = "d3e9f8737d7196b1271a725f89e2156672bc1d900a25e6e0c1364e2f7a20f605";

    // Our request will be sent to:
    // https://app.shippingeasy.com/api/stores/:store_api_key/orders
    // The path part of this request is: /api/stores/:store_api_key/orders

    // See https://shippingeasy.readme.io/v1.0/docs/authentication-signing-requests
    const sb_sig_str = try chilkat.StringBuilder.init();
    defer sb_sig_str.deinit();
    sb_sig_str.append("POST&/api/stores/") catch {};
    sb_sig_str.append(store_api_key) catch {};
    sb_sig_str.append("/orders&") catch {};
    sb_sig_str.append("api_key=") catch {};
    sb_sig_str.append(api_key) catch {};
    sb_sig_str.append("&api_timestamp=") catch {};

    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromCurrentSystemTime() catch {};
    sb_sig_str.append(try dt.getAsUnixTimeStr(alloc, false)) catch {};
    sb_sig_str.append("&") catch {};

    json.setEmitCompact(true);
    json.emitSb(sb_sig_str) catch {};

    // Calculate the HMAC-SHA256 of the API Signature String using the API Secret as the HMAC key.
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setMacAlgorithm("hmac");
    crypt.setEncodingMode("hex");
    crypt.setMacKeyString(api_secret) catch {};
    crypt.setHashAlgorithm("sha256");

    const sb_signature = try chilkat.StringBuilder.init();
    defer sb_signature.deinit();
    sb_signature.append(try crypt.macStringENC(alloc, try sb_sig_str.getAsString(alloc))) catch {};
    sb_signature.toLowercase() catch {};

    const sb_url = try chilkat.StringBuilder.init();
    defer sb_url.deinit();
    sb_url.append("https://app.shippingeasy.com/api/stores/") catch {};
    sb_url.append(store_api_key) catch {};
    sb_url.append("/orders?") catch {};
    sb_url.append("api_key=") catch {};
    sb_url.append(api_key) catch {};
    sb_url.append("&api_timestamp=") catch {};
    sb_url.append(try dt.getAsUnixTimeStr(alloc, false)) catch {};
    sb_url.append("&api_signature=") catch {};
    sb_url.append(try sb_signature.getAsString(alloc)) catch {};

    // Send a POST equivalent the following curl request:
    //     curl -H "Content-Type: application/json" --data @body.json "https://app.shippingeasy.com/api/stores/27aa472e16faa83dd13b7758d31974ed/orders?
    //     api_key=f9a7c8ebdfd34beaf260d9b0296c7059&
    //     api_timestamp=1401803554&
    //     api_signature=c65f43beed46e581939898a78acd10064cfa146845e97885ec02124d7ad648e4"

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setSessionLogFilename("qa_output/shippingEasy.txt");
    const url = try sb_url.getAsString(alloc);
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", url, json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("response status code = {d}\n", .{resp.getStatusCode()});
    std.debug.print("response body:\n", .{});
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}