Sample code for 30+ languages & platforms
Tcl

Quickbooks Create a New Customer

See more QuickBooks Examples

Demonstrates how to create a new customer via the Quickbooks REST API.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# 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.
set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/qb-access-token.json"]

set rest [new_CkRest]

# Connect to the REST server.
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "sandbox-quickbooks.api.intuit.com" $port $bTls $bAutoReconnect]

set sbAuth [new_CkStringBuilder]

CkStringBuilder_Append $sbAuth "Bearer "
CkStringBuilder_Append $sbAuth [CkJsonObject_stringOf $jsonToken "access_token"]
CkRest_put_Authorization $rest [CkStringBuilder_getAsString $sbAuth]

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

set jsonReq [new_CkJsonObject]

CkJsonObject_UpdateString $jsonReq "FullyQualifiedName" "King Groceries"
CkJsonObject_UpdateString $jsonReq "PrimaryEmailAddr.Address" "jdrew@myemail.com"
CkJsonObject_UpdateString $jsonReq "DisplayName" "King's Groceries"
CkJsonObject_UpdateString $jsonReq "Suffix" "Jr"
CkJsonObject_UpdateString $jsonReq "Title" "Mr"
CkJsonObject_UpdateString $jsonReq "MiddleName" "B"
CkJsonObject_UpdateString $jsonReq "Notes" "Here are other details."
CkJsonObject_UpdateString $jsonReq "FamilyName" "King"
CkJsonObject_UpdateString $jsonReq "PrimaryPhone.FreeFormNumber" "(555) 555-5555"
CkJsonObject_UpdateString $jsonReq "CompanyName" "King Groceries"
CkJsonObject_UpdateString $jsonReq "BillAddr.CountrySubDivisionCode" "CA"
CkJsonObject_UpdateString $jsonReq "BillAddr.City" "Mountain View"
CkJsonObject_UpdateString $jsonReq "BillAddr.PostalCode" "94042"
CkJsonObject_UpdateString $jsonReq "BillAddr.Line1" "123 Main Street"
CkJsonObject_UpdateString $jsonReq "BillAddr.Country" "USA"
CkJsonObject_UpdateString $jsonReq "GivenName" "James"

set sbRequestBody [new_CkStringBuilder]

CkJsonObject_EmitSb $jsonReq $sbRequestBody

CkRest_AddHeader $rest "Content-Type" "application/json"
CkRest_AddHeader $rest "Accept" "application/json"
CkRest_put_AllowHeaderFolding $rest 0

set sbResponseBody [new_CkStringBuilder]

set success [CkRest_FullRequestSb $rest "POST" "/v3/company/<realmID>/customer" $sbRequestBody $sbResponseBody]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkJsonObject $jsonToken
    delete_CkRest $rest
    delete_CkStringBuilder $sbAuth
    delete_CkJsonObject $jsonReq
    delete_CkStringBuilder $sbRequestBody
    delete_CkStringBuilder $sbResponseBody
    exit
}

set respStatusCode [CkRest_get_ResponseStatusCode $rest]

# Success is indicated by a 200 response status code.
puts "response status code = $respStatusCode"

set jsonResponse [new_CkJsonObject]

CkJsonObject_LoadSb $jsonResponse $sbResponseBody
CkJsonObject_put_EmitCompact $jsonResponse 0
puts [CkJsonObject_emit $jsonResponse]

if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    puts "Failed."
    delete_CkJsonObject $jsonToken
    delete_CkRest $rest
    delete_CkStringBuilder $sbAuth
    delete_CkJsonObject $jsonReq
    delete_CkStringBuilder $sbRequestBody
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jsonResponse
    exit
}

# 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"
# }
# 

set CustomerDomain [CkJsonObject_stringOf $jsonResponse "Customer.domain"]
set CustomerPrimaryEmailAddrAddress [CkJsonObject_stringOf $jsonResponse "Customer.PrimaryEmailAddr.Address"]
set CustomerDisplayName [CkJsonObject_stringOf $jsonResponse "Customer.DisplayName"]
set CustomerCurrencyRefName [CkJsonObject_stringOf $jsonResponse "Customer.CurrencyRef.name"]
set CustomerCurrencyRefValue [CkJsonObject_stringOf $jsonResponse "Customer.CurrencyRef.value"]
set CustomerDefaultTaxCodeRefValue [CkJsonObject_stringOf $jsonResponse "Customer.DefaultTaxCodeRef.value"]
set CustomerPreferredDeliveryMethod [CkJsonObject_stringOf $jsonResponse "Customer.PreferredDeliveryMethod"]
set CustomerGivenName [CkJsonObject_stringOf $jsonResponse "Customer.GivenName"]
set CustomerFullyQualifiedName [CkJsonObject_stringOf $jsonResponse "Customer.FullyQualifiedName"]
set CustomerBillWithParent [CkJsonObject_BoolOf $jsonResponse "Customer.BillWithParent"]
set CustomerTitle [CkJsonObject_stringOf $jsonResponse "Customer.Title"]
set CustomerJob [CkJsonObject_BoolOf $jsonResponse "Customer.Job"]
set CustomerBalanceWithJobs [CkJsonObject_IntOf $jsonResponse "Customer.BalanceWithJobs"]
set CustomerPrimaryPhoneFreeFormNumber [CkJsonObject_stringOf $jsonResponse "Customer.PrimaryPhone.FreeFormNumber"]
set CustomerTaxable [CkJsonObject_BoolOf $jsonResponse "Customer.Taxable"]
set CustomerMetaDataCreateTime [CkJsonObject_stringOf $jsonResponse "Customer.MetaData.CreateTime"]
set CustomerMetaDataLastUpdatedTime [CkJsonObject_stringOf $jsonResponse "Customer.MetaData.LastUpdatedTime"]
set CustomerBillAddrCity [CkJsonObject_stringOf $jsonResponse "Customer.BillAddr.City"]
set CustomerBillAddrCountry [CkJsonObject_stringOf $jsonResponse "Customer.BillAddr.Country"]
set CustomerBillAddrLine1 [CkJsonObject_stringOf $jsonResponse "Customer.BillAddr.Line1"]
set CustomerBillAddrPostalCode [CkJsonObject_stringOf $jsonResponse "Customer.BillAddr.PostalCode"]
set CustomerBillAddrCountrySubDivisionCode [CkJsonObject_stringOf $jsonResponse "Customer.BillAddr.CountrySubDivisionCode"]
set CustomerBillAddrId [CkJsonObject_stringOf $jsonResponse "Customer.BillAddr.Id"]
set CustomerMiddleName [CkJsonObject_stringOf $jsonResponse "Customer.MiddleName"]
set CustomerNotes [CkJsonObject_stringOf $jsonResponse "Customer.Notes"]
set CustomerActive [CkJsonObject_BoolOf $jsonResponse "Customer.Active"]
set CustomerBalance [CkJsonObject_IntOf $jsonResponse "Customer.Balance"]
set CustomerSyncToken [CkJsonObject_stringOf $jsonResponse "Customer.SyncToken"]
set CustomerSuffix [CkJsonObject_stringOf $jsonResponse "Customer.Suffix"]
set CustomerCompanyName [CkJsonObject_stringOf $jsonResponse "Customer.CompanyName"]
set CustomerFamilyName [CkJsonObject_stringOf $jsonResponse "Customer.FamilyName"]
set CustomerPrintOnCheckName [CkJsonObject_stringOf $jsonResponse "Customer.PrintOnCheckName"]
set CustomerSparse [CkJsonObject_BoolOf $jsonResponse "Customer.sparse"]
set CustomerId [CkJsonObject_stringOf $jsonResponse "Customer.Id"]
set time [CkJsonObject_stringOf $jsonResponse "time"]

delete_CkJsonObject $jsonToken
delete_CkRest $rest
delete_CkStringBuilder $sbAuth
delete_CkJsonObject $jsonReq
delete_CkStringBuilder $sbRequestBody
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jsonResponse