Sample code for 30+ languages & platforms
Zig

Square API - List Catalog

See more Square Examples

Returns a list of CatalogObjects that includes all objects of a set of desired types (for example, all CatalogItem and CatalogTax objects) in the catalog.

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://connect.squareup.com/v2/catalog/list?types=category%2Ctax \
    //   -H 'Square-Version: 2020-07-22' \
    //   -H 'Authorization: Bearer ACCESS_TOKEN' \
    //   -H 'Content-Type: application/json'

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.setAuthToken("ACCESS_TOKEN");
    http.setRequestHeader("Square-Version", "2020-07-22");
    http.setRequestHeader("Content-Type", "application/json");

    // This example uses the sandbox: connect.squareupsandbox.com
    // Production should use connect.squareup.com
    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://connect.squareupsandbox.com/v2/catalog/list?types=item,category,tax,image", 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)

    // {
    //   "objects": [
    //     {
    //       "type": "CATEGORY",
    //       "id": "5ZYQZZ2IECPVJ2IJ5KQPRDC3",
    //       "updated_at": "2017-02-21T14:50:26.495Z",
    //       "version": 1487688626495,
    //       "is_deleted": false,
    //       "present_at_all_locations": true,
    //       "category_data": {
    //         "name": "Beverages"
    //       }
    //     },
    //     {
    //       "type": "TAX",
    //       "id": "L5R47DGBZOOVKCAFIXC56AEN",
    //       "updated_at": "2017-02-21T14:50:26.495Z",
    //       "version": 1487688626495,
    //       "is_deleted": false,
    //       "present_at_all_locations": true,
    //       "tax_data": {
    //         "name": "Sales Tax",
    //         "calculation_phase": "TAX_SUBTOTAL_PHASE",
    //         "inclusion_type": "ADDITIVE",
    //         "percentage": "5.0",
    //         "enabled": true
    //       }
    //     }
    //   ]
    // }

    // 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 v_type: [:0]const u8 = "";
    var id: [:0]const u8 = "";
    var updated_at: [:0]const u8 = "";
    var version: i32 = 0;
    var is_deleted: bool = false;
    var present_at_all_locations: bool = false;
    var category_data_name: [:0]const u8 = "";
    var tax_data_name: [:0]const u8 = "";
    var tax_data_calculation_phase: [:0]const u8 = "";
    var tax_data_inclusion_type: [:0]const u8 = "";
    var tax_data_percentage: [:0]const u8 = "";
    var tax_data_enabled: bool = false;

    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("objects");
    while (i < count_i) {
        j_resp.setI(i);
        v_type = try j_resp.stringOf(alloc, "objects[i].type");
        id = try j_resp.stringOf(alloc, "objects[i].id");
        updated_at = try j_resp.stringOf(alloc, "objects[i].updated_at");
        version = j_resp.intOf("objects[i].version");
        is_deleted = j_resp.boolOf("objects[i].is_deleted");
        present_at_all_locations = j_resp.boolOf("objects[i].present_at_all_locations");
        category_data_name = try j_resp.stringOf(alloc, "objects[i].category_data.name");
        tax_data_name = try j_resp.stringOf(alloc, "objects[i].tax_data.name");
        tax_data_calculation_phase = try j_resp.stringOf(alloc, "objects[i].tax_data.calculation_phase");
        tax_data_inclusion_type = try j_resp.stringOf(alloc, "objects[i].tax_data.inclusion_type");
        tax_data_percentage = try j_resp.stringOf(alloc, "objects[i].tax_data.percentage");
        tax_data_enabled = j_resp.boolOf("objects[i].tax_data.enabled");
        i = i + 1;
    }
}