Zig Requires Chilkat v11.0.0+
Zig
ShippingEasy.com Calculate Signature for API Authentication
See more HTTP Misc Examples
Demonstrates how to calculate the shippingeasy.com API signature for authenticating requests.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First, concatenate these into a plaintext string using the following order:
//
// Capitilized method of the request. E.g. "POST"
// The URI path
// The query parameters sorted alphabetically and concatenated together into a URL friendly format: param1=ABC¶m2=XYZ
// The request body as a string if one exists
// All parts are then concatenated together with an ampersand. The result resembles something like this:
//
// "POST&/partners/api/accounts&api_key=f9a7c8ebdfd34beaf260d9b0296c7059&api_timestamp=1401803554&{ ... request body ... }"
const sb_string_to_sign = try chilkat.StringBuilder.init();
defer sb_string_to_sign.deinit();
const http_verb = "POST";
const uri_path = "/partners/api/accounts";
const query_params_str = "api_key=YOUR_API_KEY&api_timestamp=UNIX_EPOCH_TIMESTAMP";
// Build the following JSON that will be the body of the request:
// {
// "account": {
// "first_name": "Coralie",
// "last_name": "Waelchi",
// "company_name": "Hegmann, Cremin and Bradtke",
// "email": "se_greg_6d477b1e59e8ff24abadfb59d3a2de3e@shippingeasy.com",
// "phone_number": "1-381-014-3358",
// "address": "2476 Flo Inlet",
// "address2": "",
// "state": "SC",
// "city": "North Dennis",
// "postal_code": "29805",
// "country": "USA",
// "password": "abc123",
// "subscription_plan_code": "starter"
// }
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("account.first_name", "Coralie") catch {};
json.updateString("account.last_name", "Waelchi") catch {};
json.updateString("account.company_name", "Hegmann, Cremin and Bradtke") catch {};
json.updateString("account.email", "se_greg_6d477b1e59e8ff24abadfb59d3a2de3e@shippingeasy.com") catch {};
json.updateString("account.phone_number", "1-381-014-3358") catch {};
json.updateString("account.address", "2476 Flo Inlet") catch {};
json.updateString("account.address2", "") catch {};
json.updateString("account.state", "SC") catch {};
json.updateString("account.city", "North Dennis") catch {};
json.updateString("account.postal_code", "29805") catch {};
json.updateString("account.country", "USA") catch {};
json.updateString("account.password", "abc123") catch {};
json.updateString("account.subscription_plan_code", "starter") catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// First, let's get the current date/time in the Unix Epoch Timestamp format (which is just an integer)
const dt = try chilkat.DateTime.init();
defer dt.deinit();
dt.setFromCurrentSystemTime() catch {};
// Get the UTC time.
const b_local_time = false;
const unix_epoch_timestamp = try dt.getAsUnixTimeStr(alloc, b_local_time);
// Build the string to sign:
sb_string_to_sign.append(http_verb) catch {};
sb_string_to_sign.append("&") catch {};
sb_string_to_sign.append(uri_path) catch {};
sb_string_to_sign.append("&") catch {};
sb_string_to_sign.append(query_params_str) catch {};
sb_string_to_sign.append("&") catch {};
// Make sure to send the JSON body of a request in compact form..
json.setEmitCompact(true);
sb_string_to_sign.append(try json.emit(alloc)) catch {};
// Use your API key here:
const your_api_key = "f9a7c8ebdfd34beaf260d9b0296c7059";
var num_replaced: i32 = sb_string_to_sign.replace("YOUR_API_KEY", your_api_key);
num_replaced = sb_string_to_sign.replace("UNIX_EPOCH_TIMESTAMP", unix_epoch_timestamp);
// Do the HMAC-SHA256 with your API secret:
const your_api_secret = "ea210785fa4656af03c2e4ffcc2e7b5fc19f1fba577d137905cc97e74e1df53d";
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setMacAlgorithm("hmac");
crypt.setEncodingMode("hexlower");
crypt.setMacKeyString(your_api_secret) catch {};
crypt.setHashAlgorithm("sha256");
var api_signature: [:0]const u8 = try crypt.macStringENC(alloc, try sb_string_to_sign.getAsString(alloc));
std.debug.print("api_signature: {s}\n", .{api_signature});
// --------------------------------------------------------------------
// Here's an example showing how to use the signature in a request:
// Build a new string-to-sign and create a new api_signature for the actual request we'll be sending...
sb_string_to_sign.clear();
sb_string_to_sign.append("GET") catch {};
sb_string_to_sign.append("&") catch {};
sb_string_to_sign.append("/app.shippingeasy.com/api/orders") catch {};
sb_string_to_sign.append("&") catch {};
sb_string_to_sign.append(query_params_str) catch {};
sb_string_to_sign.append("&") catch {};
// There is no body for a GET request.
api_signature = try crypt.macStringENC(alloc, try sb_string_to_sign.getAsString(alloc));
const http = try chilkat.Http.init();
defer http.deinit();
const query_params = try chilkat.JsonObject.init();
defer query_params.deinit();
query_params.updateString("api_signature", api_signature) catch {};
query_params.updateString("api_timestamp", unix_epoch_timestamp) catch {};
query_params.updateString("api_key", your_api_key) catch {};
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpParams("GET", "https://app.shippingeasy.com/api/orders", query_params, 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)});
}