Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Xero Get Accounts

See more Xero Examples

Download Xero accounts information

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/xero-access-token.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    // Replace the value here with an actual tenant ID obtained from this example:
    // Get Xero Tenant IDs
    http.setRequestHeader("Xero-tenant-id", "83299b9e-5747-4a14-a18a-a6c94f824eb7");

    http.setAccept("application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://api.xero.com/api.xro/2.0/Accounts", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(try resp.getBodyStr(alloc)) catch {};
    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    if (resp.getStatusCode() != 200) {
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample output...
    // (See the parsing code below..)
    //
    // Use the this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "Accounts": [
    //     {
    //       "AccountID": "ebd06280-af70-4bed-97c6-7451a454ad85",
    //       "Code": "091",
    //       "Name": "Business Savings Account",
    //       "Type": "BANK",
    //       "TaxType": "NONE",
    //       "EnablePaymentsToAccount": false,
    //       "BankAccountNumber": "0209087654321050",
    //       "BankAccountType": "BANK",
    //       "CurrencyCode": "NZD"
    //     },
    //     {
    //       "AccountID": "7d05a53d-613d-4eb2-a2fc-dcb6adb80b80",
    //       "Code": "200",
    //       "Name": "Sales",
    //       "Type": "REVENUE",
    //       "TaxType": "OUTPUT2",
    //       "Description": "Income from any normal business activity",
    //       "EnablePaymentsToAccount": false
    //     }
    //   ]
    // }
    //

    var account_id: [:0]const u8 = "";
    var code: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var type_: [:0]const u8 = "";
    var tax_type: [:0]const u8 = "";
    var enable_payments_to_account: bool = false;
    var bank_account_number: [:0]const u8 = "";
    var bank_account_type: [:0]const u8 = "";
    var currency_code: [:0]const u8 = "";
    var description: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = json_response.sizeOfArray("Accounts");
    while (i < count_i) {
        json_response.setI(i);
        account_id = try json_response.stringOf(alloc, "Accounts[i].AccountID");
        code = try json_response.stringOf(alloc, "Accounts[i].Code");
        name = try json_response.stringOf(alloc, "Accounts[i].Name");
        type_ = try json_response.stringOf(alloc, "Accounts[i].Type");
        tax_type = try json_response.stringOf(alloc, "Accounts[i].TaxType");
        enable_payments_to_account = json_response.boolOf("Accounts[i].EnablePaymentsToAccount");
        bank_account_number = try json_response.stringOf(alloc, "Accounts[i].BankAccountNumber");
        bank_account_type = try json_response.stringOf(alloc, "Accounts[i].BankAccountType");
        currency_code = try json_response.stringOf(alloc, "Accounts[i].CurrencyCode");
        description = try json_response.stringOf(alloc, "Accounts[i].Description");
        i = i + 1;
    }
}