Lua
Lua
QuickBooks - Create an Account
See more QuickBooks Examples
Demonstrates how to send an JSON request to create a QuickBooks account.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- First get our previously obtained OAuth2 access token.
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/qb-access-token.json")
local rest = chilkat.Rest.new()
local bAutoReconnect = true
success = rest:connect("sandbox-quickbooks.api.intuit.com", 443, true, bAutoReconnect)
if not success then
print(rest.last_error_text)
return
end
local sbAuth = chilkat.StringBuilder.new()
sbAuth:append("Bearer ")
sbAuth:append(jsonToken:string_of("access_token"))
rest.authorization = sbAuth:get_as_string()
local jsonRequest = chilkat.JsonObject.new()
jsonRequest:append_string("AccountType", "Credit Card")
jsonRequest:append_string("Name", "Banana Republic")
local requestBody = jsonRequest:emit()
-- "123146096291789" is the company ID.
local sbPath = chilkat.StringBuilder.new()
sbPath:append("/v3/company/123146096291789/account?minorversion=45")
rest:add_header("Content-Type", "application/json")
rest:add_header("Accept", "application/json")
rest.allow_header_folding = false
local responseBody = rest:full_request_string("POST", sbPath:get_as_string(), requestBody)
if not rest.last_method_success then
print(rest.last_error_text)
return
end
-- We should expect a 200 response if successful.
if rest.response_status_code ~= 200 then
print("Request Header: ")
print(rest.last_request_header)
print("----")
print("Response StatusCode = " .. rest.response_status_code)
print("Response StatusLine: " .. rest.response_status_text)
print("Response Header:")
print(rest.response_header)
print(responseBody)
return
end
local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load(responseBody)
jsonResponse.emit_compact = false
print(jsonResponse:emit())
print("Success.")
-- A sample JSON response:
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
-- {
-- "Account": {
-- "Name": "Banana Republic",
-- "SubAccount": false,
-- "FullyQualifiedName": "Banana Republic",
-- "Active": true,
-- "Classification": "Liability",
-- "AccountType": "Credit Card",
-- "AccountSubType": "CreditCard",
-- "CurrentBalance": 0,
-- "CurrentBalanceWithSubAccounts": 0,
-- "CurrencyRef": {
-- "value": "USD",
-- "name": "United States Dollar"
-- },
-- "domain": "QBO",
-- "sparse": false,
-- "Id": "97",
-- "SyncToken": "0",
-- "MetaData": {
-- "CreateTime": "2016-10-25T05:07:12-07:00",
-- "LastUpdatedTime": "2016-10-25T05:07:12-07:00"
-- }
-- },
-- "time": "2016-10-25T05:07:11.714-07:00"
-- }