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

Xero Get Accounts

See more Xero Examples

Download Xero accounts information

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/xero-access-token.json")
if not success then
    print(jsonToken.last_error_text)
    return
end

http.auth_token = jsonToken:string_of("access_token")

-- 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.accept = "application/json"

local resp = chilkat.HttpResponse.new()
success = http:http_no_body("GET", "https://api.xero.com/api.xro/2.0/Accounts", resp)
if not success then
    print(http.last_error_text)
    return
end

print("Response Status Code: " .. resp.status_code)

local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load(resp.body_str)
jsonResponse.emit_compact = false
print(jsonResponse:emit())

if resp.status_code ~= 200 then
    print("Failed.")
    return
end

-- 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
--     }
--   ]
-- }
--

local AccountID = ""
local Code = ""
local Name = ""
local Type = ""
local TaxType = ""
local EnablePaymentsToAccount = false
local BankAccountNumber = ""
local BankAccountType = ""
local CurrencyCode = ""
local Description = ""

local i = 0
local count_i = jsonResponse:size_of_array("Accounts")
while i < count_i do
    jsonResponse.i = i
    AccountID = jsonResponse:string_of("Accounts[i].AccountID")
    Code = jsonResponse:string_of("Accounts[i].Code")
    Name = jsonResponse:string_of("Accounts[i].Name")
    Type = jsonResponse:string_of("Accounts[i].Type")
    TaxType = jsonResponse:string_of("Accounts[i].TaxType")
    EnablePaymentsToAccount = jsonResponse:bool_of("Accounts[i].EnablePaymentsToAccount")
    BankAccountNumber = jsonResponse:string_of("Accounts[i].BankAccountNumber")
    BankAccountType = jsonResponse:string_of("Accounts[i].BankAccountType")
    CurrencyCode = jsonResponse:string_of("Accounts[i].CurrencyCode")
    Description = jsonResponse:string_of("Accounts[i].Description")
    i = i + 1
end