Zig Requires Chilkat v11.0.0+
Zig
Xero Update Account Details
See more Xero Examples
Update some details of an account in a Xero company (Accounting API)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.
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:
// {
// "AccountID": "54ddab14-4a8d-45cf-86be-076c99a0cea0",
// "Name": "Sales account",
// "Type": "REVENUE",
// "TaxType": "OUTPUT",
// "Description": "Income from any normal business trading activity",
// "EnablePaymentsToAccount": "false",
// "ShowInExpenseClaims": "false"
// }
// 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("AccountID", "54ddab14-4a8d-45cf-86be-076c99a0cea0") catch {};
json_request_body.updateString("Name", "Sales account") catch {};
json_request_body.updateString("Type", "REVENUE") catch {};
json_request_body.updateString("TaxType", "OUTPUT") catch {};
json_request_body.updateString("Description", "Income from any normal business trading activity") catch {};
json_request_body.updateString("EnablePaymentsToAccount", "false") catch {};
json_request_body.updateString("ShowInExpenseClaims", "false") catch {};
const url = "https://api.xero.com/api.xro/2.0/Accounts/54ddab14-4a8d-45cf-86be-076c99a0cea0";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", 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": "430e92a8-de02-41d5-a00e-3ef899188aea",
// "Status": "OK",
// "ProviderName": "Chilkat2222",
// "DateTimeUTC": "\/Date(1587162517005)\/",
// "Accounts": [
// {
// "AccountID": "54ddab14-4a8d-45cf-86be-076c99a0cea0",
// "Code": "201",
// "Name": "Sales account",
// "Status": "ACTIVE",
// "Type": "REVENUE",
// "TaxType": "OUTPUT",
// "Description": "Income from any normal business trading activity",
// "Class": "REVENUE",
// "EnablePaymentsToAccount": false,
// "ShowInExpenseClaims": false,
// "ReportingCode": "REV",
// "ReportingCodeName": "Revenue",
// "UpdatedDateUTC": "\/Date(1587162517090+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 description: [: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");
description = try json_response.stringOf(alloc, "Accounts[i].Description");
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;
}
}