Lua
Lua
Quickbooks Create a New Customer
See more QuickBooks Examples
Demonstrates how to create a new customer via the Quickbooks REST API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires 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()
-- Connect to the REST server.
local bTls = true
local port = 443
local bAutoReconnect = true
success = rest:connect("sandbox-quickbooks.api.intuit.com", port, bTls, bAutoReconnect)
local sbAuth = chilkat.StringBuilder.new()
sbAuth:append("Bearer ")
sbAuth:append(jsonToken:string_of("access_token"))
rest.authorization = sbAuth:get_as_string()
-- --------------------------------------------------------------------------
-- Note: The above code to setup the initial REST connection
-- can be done once. After connecting, any number of REST calls can be made.
-- If the connection is lost, the next REST method call will automatically
-- reconnect if needed.
-- --------------------------------------------------------------------------
-- Create the following JSON:
-- {
-- "FullyQualifiedName": "King Groceries",
-- "PrimaryEmailAddr": {
-- "Address": "jdrew@myemail.com"
-- },
-- "DisplayName": "King's Groceries",
-- "Suffix": "Jr",
-- "Title": "Mr",
-- "MiddleName": "B",
-- "Notes": "Here are other details.",
-- "FamilyName": "King",
-- "PrimaryPhone": {
-- "FreeFormNumber": "(555) 555-5555"
-- },
-- "CompanyName": "King Groceries",
-- "BillAddr": {
-- "CountrySubDivisionCode": "CA",
-- "City": "Mountain View",
-- "PostalCode": "94042",
-- "Line1": "123 Main Street",
-- "Country": "USA"
-- },
-- "GivenName": "James"
-- }
--
-- Use the this online tool to generate the code from sample JSON:
-- Generate Code to Create JSON
local jsonReq = chilkat.JsonObject.new()
jsonReq:update_string("FullyQualifiedName", "King Groceries")
jsonReq:update_string("PrimaryEmailAddr.Address", "jdrew@myemail.com")
jsonReq:update_string("DisplayName", "King's Groceries")
jsonReq:update_string("Suffix", "Jr")
jsonReq:update_string("Title", "Mr")
jsonReq:update_string("MiddleName", "B")
jsonReq:update_string("Notes", "Here are other details.")
jsonReq:update_string("FamilyName", "King")
jsonReq:update_string("PrimaryPhone.FreeFormNumber", "(555) 555-5555")
jsonReq:update_string("CompanyName", "King Groceries")
jsonReq:update_string("BillAddr.CountrySubDivisionCode", "CA")
jsonReq:update_string("BillAddr.City", "Mountain View")
jsonReq:update_string("BillAddr.PostalCode", "94042")
jsonReq:update_string("BillAddr.Line1", "123 Main Street")
jsonReq:update_string("BillAddr.Country", "USA")
jsonReq:update_string("GivenName", "James")
local sbRequestBody = chilkat.StringBuilder.new()
jsonReq:emit_sb(sbRequestBody)
rest:add_header("Content-Type", "application/json")
rest:add_header("Accept", "application/json")
rest.allow_header_folding = false
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/v3/company/<realmID>/customer", sbRequestBody, sbResponseBody)
if not success then
print(rest.last_error_text)
return
end
local respStatusCode = rest.response_status_code
-- Success is indicated by a 200 response status code.
print("response status code = " .. respStatusCode)
local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load_sb(sbResponseBody)
jsonResponse.emit_compact = false
print(jsonResponse:emit())
if rest.response_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
-- {
-- "Customer": {
-- "domain": "QBO",
-- "PrimaryEmailAddr": {
-- "Address": "jdrew@myemail.com"
-- },
-- "DisplayName": "King's Groceries",
-- "CurrencyRef": {
-- "name": "United States Dollar",
-- "value": "USD"
-- },
-- "DefaultTaxCodeRef": {
-- "value": "2"
-- },
-- "PreferredDeliveryMethod": "Print",
-- "GivenName": "James",
-- "FullyQualifiedName": "King's Groceries",
-- "BillWithParent": false,
-- "Title": "Mr",
-- "Job": false,
-- "BalanceWithJobs": 0,
-- "PrimaryPhone": {
-- "FreeFormNumber": "(555) 555-5555"
-- },
-- "Taxable": true,
-- "MetaData": {
-- "CreateTime": "2015-07-23T10:58:12-07:00",
-- "LastUpdatedTime": "2015-07-23T10:58:12-07:00"
-- },
-- "BillAddr": {
-- "City": "Mountain View",
-- "Country": "USA",
-- "Line1": "123 Main Street",
-- "PostalCode": "94042",
-- "CountrySubDivisionCode": "CA",
-- "Id": "112"
-- },
-- "MiddleName": "B",
-- "Notes": "Here are other details.",
-- "Active": true,
-- "Balance": 0,
-- "SyncToken": "0",
-- "Suffix": "Jr",
-- "CompanyName": "King Groceries",
-- "FamilyName": "King",
-- "PrintOnCheckName": "King Groceries",
-- "sparse": false,
-- "Id": "67"
-- },
-- "time": "2015-07-23T10:58:12.099-07:00"
-- }
--
local CustomerDomain = jsonResponse:string_of("Customer.domain")
local CustomerPrimaryEmailAddrAddress = jsonResponse:string_of("Customer.PrimaryEmailAddr.Address")
local CustomerDisplayName = jsonResponse:string_of("Customer.DisplayName")
local CustomerCurrencyRefName = jsonResponse:string_of("Customer.CurrencyRef.name")
local CustomerCurrencyRefValue = jsonResponse:string_of("Customer.CurrencyRef.value")
local CustomerDefaultTaxCodeRefValue = jsonResponse:string_of("Customer.DefaultTaxCodeRef.value")
local CustomerPreferredDeliveryMethod = jsonResponse:string_of("Customer.PreferredDeliveryMethod")
local CustomerGivenName = jsonResponse:string_of("Customer.GivenName")
local CustomerFullyQualifiedName = jsonResponse:string_of("Customer.FullyQualifiedName")
local CustomerBillWithParent = jsonResponse:bool_of("Customer.BillWithParent")
local CustomerTitle = jsonResponse:string_of("Customer.Title")
local CustomerJob = jsonResponse:bool_of("Customer.Job")
local CustomerBalanceWithJobs = jsonResponse:int_of("Customer.BalanceWithJobs")
local CustomerPrimaryPhoneFreeFormNumber = jsonResponse:string_of("Customer.PrimaryPhone.FreeFormNumber")
local CustomerTaxable = jsonResponse:bool_of("Customer.Taxable")
local CustomerMetaDataCreateTime = jsonResponse:string_of("Customer.MetaData.CreateTime")
local CustomerMetaDataLastUpdatedTime = jsonResponse:string_of("Customer.MetaData.LastUpdatedTime")
local CustomerBillAddrCity = jsonResponse:string_of("Customer.BillAddr.City")
local CustomerBillAddrCountry = jsonResponse:string_of("Customer.BillAddr.Country")
local CustomerBillAddrLine1 = jsonResponse:string_of("Customer.BillAddr.Line1")
local CustomerBillAddrPostalCode = jsonResponse:string_of("Customer.BillAddr.PostalCode")
local CustomerBillAddrCountrySubDivisionCode = jsonResponse:string_of("Customer.BillAddr.CountrySubDivisionCode")
local CustomerBillAddrId = jsonResponse:string_of("Customer.BillAddr.Id")
local CustomerMiddleName = jsonResponse:string_of("Customer.MiddleName")
local CustomerNotes = jsonResponse:string_of("Customer.Notes")
local CustomerActive = jsonResponse:bool_of("Customer.Active")
local CustomerBalance = jsonResponse:int_of("Customer.Balance")
local CustomerSyncToken = jsonResponse:string_of("Customer.SyncToken")
local CustomerSuffix = jsonResponse:string_of("Customer.Suffix")
local CustomerCompanyName = jsonResponse:string_of("Customer.CompanyName")
local CustomerFamilyName = jsonResponse:string_of("Customer.FamilyName")
local CustomerPrintOnCheckName = jsonResponse:string_of("Customer.PrintOnCheckName")
local CustomerSparse = jsonResponse:bool_of("Customer.sparse")
local CustomerId = jsonResponse:string_of("Customer.Id")
local time = jsonResponse:string_of("time")