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

Xero Get Accounts

See more Xero Examples

Download Xero accounts information

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/xero-access-token.json").is_err() {
    println!("{}", json_token.last_error_text());
    return;
}

http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

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

http.set_accept("application/json");

let resp = chilkat::HttpResponse::new();
if http.http_no_body("GET", "https://api.xero.com/api.xro/2.0/Accounts", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

println!("Response Status Code: {}", resp.status_code());

let json_response = chilkat::JsonObject::new();
let _ = json_response.load(&resp.body_str());
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

if resp.status_code() != 200 {
    println!("Failed.");
    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
//     }
//   ]
// }
// 

let mut i = 0;
let count_i = json_response.size_of_array("Accounts");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("Accounts[i].AccountID").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].Code").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].Name").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].Type").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].TaxType").unwrap_or_default();
    let _ = json_response.bool_of("Accounts[i].EnablePaymentsToAccount");
    let _ = json_response.string_of("Accounts[i].BankAccountNumber").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].BankAccountType").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].CurrencyCode").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].Description").unwrap_or_default();
    i = i + 1;
}