Zig
Zig
Shippo Create an Order
See more Shippo Examples
Demonstrates how to create an order with all the information about your order by sending a POST request to the orders endpoint.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 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/orders/ \
// -H "Authorization: ShippoToken <API_Token>" \
// -H "Content-Type: application/json" \
// -d '{
// "to_address": {
// "city": "San Francisco",
// "company": "Shippo",
// "country": "US",
// "email": "shippotle@goshippo.com",
// "name": "Mr Hippo",
// "phone": "15553419393",
// "state": "CA",
// "street1": "215 Clayton St.",
// "zip": "94117"
// },
// "line_items": [
// {
// "quantity": 1,
// "sku": "HM-123",
// "title": "Hippo Magazines",
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
// ],
// "placed_at": "2016-09-23T01:28:12Z",
// "order_number": "#1068",
// "order_status": "PAID",
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "to_address": {
// "city": "San Francisco",
// "company": "Shippo",
// "country": "US",
// "email": "shippotle@goshippo.com",
// "name": "Mr Hippo",
// "phone": "15553419393",
// "state": "CA",
// "street1": "215 Clayton St.",
// "zip": "94117"
// },
// "line_items": [
// {
// "quantity": 1,
// "sku": "HM-123",
// "title": "Hippo Magazines",
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
// ],
// "placed_at": "2016-09-23T01:28:12Z",
// "order_number": "#1068",
// "order_status": "PAID",
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb"
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("to_address.city", "San Francisco") catch {};
json.updateString("to_address.company", "Shippo") catch {};
json.updateString("to_address.country", "US") catch {};
json.updateString("to_address.email", "shippotle@goshippo.com") catch {};
json.updateString("to_address.name", "Mr Hippo") catch {};
json.updateString("to_address.phone", "15553419393") catch {};
json.updateString("to_address.state", "CA") catch {};
json.updateString("to_address.street1", "215 Clayton St.") catch {};
json.updateString("to_address.zip", "94117") catch {};
json.updateInt("line_items[0].quantity", 1) catch {};
json.updateString("line_items[0].sku", "HM-123") catch {};
json.updateString("line_items[0].title", "Hippo Magazines") catch {};
json.updateString("line_items[0].total_price", "12.10") catch {};
json.updateString("line_items[0].currency", "USD") catch {};
json.updateString("line_items[0].weight", "0.40") catch {};
json.updateString("line_items[0].weight_unit", "lb") catch {};
json.updateString("placed_at", "2016-09-23T01:28:12Z") catch {};
json.updateString("order_number", "#1068") catch {};
json.updateString("order_status", "PAID") catch {};
json.updateString("shipping_cost", "12.83") catch {};
json.updateString("shipping_cost_currency", "USD") catch {};
json.updateString("shipping_method", "USPS First Class Package") catch {};
json.updateString("subtotal_price", "12.10") catch {};
json.updateString("total_price", "24.93") catch {};
json.updateString("total_tax", "0.00") catch {};
json.updateString("currency", "USD") catch {};
json.updateString("weight", "0.40") catch {};
json.updateString("weight_unit", "lb") 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/orders/", 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)
// {}
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
}