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

Xero Create Account

See more Xero Examples

Create new accounts in a Xero company.

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");

    // The following JSON is sent in the request body:

    // {
    //   "Code": "201",
    //   "Name": "Sales - clearance lines",
    //   "Type": "SALES"
    // }

    // Use this online tool to generate the code from sample JSON:
    // Generate Code to Create JSON

    const json_request_body = try chilkat.JsonObject.init();
    defer json_request_body.deinit();
    json_request_body.updateString("Code", "201") catch {};
    json_request_body.updateString("Name", "Sales - clearance lines") catch {};
    json_request_body.updateString("Type", "SALES") catch {};

    const url = "https://api.xero.com/api.xro/2.0/Accounts";

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("PUT", url, json_request_body, "application/json", 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() >= 300) {
        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

    // {
    //   "Id": "705036aa-771d-4c0a-9d66-28904022858c",
    //   "Status": "OK",
    //   "ProviderName": "Chilkat2222",
    //   "DateTimeUTC": "\/Date(1587161712234)\/",
    //   "Accounts": [
    //     {
    //       "AccountID": "54ddab14-4a8d-45cf-86be-076c99a0cea0",
    //       "Code": "201",
    //       "Name": "Sales - clearance lines",
    //       "Status": "ACTIVE",
    //       "Type": "SALES",
    //       "TaxType": "OUTPUT",
    //       "Class": "REVENUE",
    //       "EnablePaymentsToAccount": false,
    //       "ShowInExpenseClaims": false,
    //       "ReportingCode": "REV",
    //       "ReportingCodeName": "Revenue",
    //       "UpdatedDateUTC": "\/Date(1587161712283+0000)\/",
    //       "AddToWatchlist": 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 class: [:0]const u8 = "";
    var enable_payments_to_account: bool = false;
    var show_in_expense_claims: bool = false;
    var reporting_code: [:0]const u8 = "";
    var reporting_code_name: [:0]const u8 = "";
    var updated_date_utc: [:0]const u8 = "";
    var add_to_watchlist: bool = false;

    _ = try json_response.stringOf(alloc, "Id");
    var status: [:0]const u8 = try json_response.stringOf(alloc, "Status");
    _ = try json_response.stringOf(alloc, "ProviderName");
    _ = try json_response.stringOf(alloc, "DateTimeUTC");
    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");
        status = try json_response.stringOf(alloc, "Accounts[i].Status");
        type_ = try json_response.stringOf(alloc, "Accounts[i].Type");
        tax_type = try json_response.stringOf(alloc, "Accounts[i].TaxType");
        class = try json_response.stringOf(alloc, "Accounts[i].Class");
        enable_payments_to_account = json_response.boolOf("Accounts[i].EnablePaymentsToAccount");
        show_in_expense_claims = json_response.boolOf("Accounts[i].ShowInExpenseClaims");
        reporting_code = try json_response.stringOf(alloc, "Accounts[i].ReportingCode");
        reporting_code_name = try json_response.stringOf(alloc, "Accounts[i].ReportingCodeName");
        updated_date_utc = try json_response.stringOf(alloc, "Accounts[i].UpdatedDateUTC");
        add_to_watchlist = json_response.boolOf("Accounts[i].AddToWatchlist");
        i = i + 1;
    }
}