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

Xero Create Account

See more Xero Examples

Create new accounts in a Xero company.

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

// 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

let json_request_body = chilkat::JsonObject::new();
let _ = json_request_body.update_string("Code", "201");
let _ = json_request_body.update_string("Name", "Sales - clearance lines");
let _ = json_request_body.update_string("Type", "SALES");

let url = "https://api.xero.com/api.xro/2.0/Accounts".to_string();

let resp = chilkat::HttpResponse::new();
if http.http_json("PUT", &url, &json_request_body, "application/json", &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() >= 300 {
    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

// {
//   "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
//     }
//   ]
// }
// 

let id = json_response.string_of("Id").unwrap_or_default();
let mut status = json_response.string_of("Status").unwrap_or_default();
let provider_name = json_response.string_of("ProviderName").unwrap_or_default();
let date_time_utc = json_response.string_of("DateTimeUTC").unwrap_or_default();
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();
    status = json_response.string_of("Accounts[i].Status").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.string_of("Accounts[i].Class").unwrap_or_default();
    let _ = json_response.bool_of("Accounts[i].EnablePaymentsToAccount");
    let _ = json_response.bool_of("Accounts[i].ShowInExpenseClaims");
    let _ = json_response.string_of("Accounts[i].ReportingCode").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].ReportingCodeName").unwrap_or_default();
    let _ = json_response.string_of("Accounts[i].UpdatedDateUTC").unwrap_or_default();
    let _ = json_response.bool_of("Accounts[i].AddToWatchlist");
    i = i + 1;
}