Sample code for 30+ languages & platforms
Unicode C++

Quickbooks Create a New Customer

See more QuickBooks Examples

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

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // 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.
    CkJsonObjectW jsonToken;
    success = jsonToken.LoadFile(L"qa_data/tokens/qb-access-token.json");

    CkRestW rest;

    // Connect to the REST server.
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"sandbox-quickbooks.api.intuit.com",port,bTls,bAutoReconnect);

    CkStringBuilderW sbAuth;
    sbAuth.Append(L"Bearer ");
    sbAuth.Append(jsonToken.stringOf(L"access_token"));
    rest.put_Authorization(sbAuth.getAsString());

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

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

    CkStringBuilderW sbRequestBody;
    jsonReq.EmitSb(sbRequestBody);

    rest.AddHeader(L"Content-Type",L"application/json");
    rest.AddHeader(L"Accept",L"application/json");
    rest.put_AllowHeaderFolding(false);

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestSb(L"POST",L"/v3/company/<realmID>/customer",sbRequestBody,sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();

    // Success is indicated by a 200 response status code.
    wprintf(L"response status code = %d\n",respStatusCode);

    CkJsonObjectW jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);
    jsonResponse.put_EmitCompact(false);
    wprintf(L"%s\n",jsonResponse.emit());

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"Failed.\n");
        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

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    const wchar_t *CustomerDomain = jsonResponse.stringOf(L"Customer.domain");
    const wchar_t *CustomerPrimaryEmailAddrAddress = jsonResponse.stringOf(L"Customer.PrimaryEmailAddr.Address");
    const wchar_t *CustomerDisplayName = jsonResponse.stringOf(L"Customer.DisplayName");
    const wchar_t *CustomerCurrencyRefName = jsonResponse.stringOf(L"Customer.CurrencyRef.name");
    const wchar_t *CustomerCurrencyRefValue = jsonResponse.stringOf(L"Customer.CurrencyRef.value");
    const wchar_t *CustomerDefaultTaxCodeRefValue = jsonResponse.stringOf(L"Customer.DefaultTaxCodeRef.value");
    const wchar_t *CustomerPreferredDeliveryMethod = jsonResponse.stringOf(L"Customer.PreferredDeliveryMethod");
    const wchar_t *CustomerGivenName = jsonResponse.stringOf(L"Customer.GivenName");
    const wchar_t *CustomerFullyQualifiedName = jsonResponse.stringOf(L"Customer.FullyQualifiedName");
    bool CustomerBillWithParent = jsonResponse.BoolOf(L"Customer.BillWithParent");
    const wchar_t *CustomerTitle = jsonResponse.stringOf(L"Customer.Title");
    bool CustomerJob = jsonResponse.BoolOf(L"Customer.Job");
    int CustomerBalanceWithJobs = jsonResponse.IntOf(L"Customer.BalanceWithJobs");
    const wchar_t *CustomerPrimaryPhoneFreeFormNumber = jsonResponse.stringOf(L"Customer.PrimaryPhone.FreeFormNumber");
    bool CustomerTaxable = jsonResponse.BoolOf(L"Customer.Taxable");
    const wchar_t *CustomerMetaDataCreateTime = jsonResponse.stringOf(L"Customer.MetaData.CreateTime");
    const wchar_t *CustomerMetaDataLastUpdatedTime = jsonResponse.stringOf(L"Customer.MetaData.LastUpdatedTime");
    const wchar_t *CustomerBillAddrCity = jsonResponse.stringOf(L"Customer.BillAddr.City");
    const wchar_t *CustomerBillAddrCountry = jsonResponse.stringOf(L"Customer.BillAddr.Country");
    const wchar_t *CustomerBillAddrLine1 = jsonResponse.stringOf(L"Customer.BillAddr.Line1");
    const wchar_t *CustomerBillAddrPostalCode = jsonResponse.stringOf(L"Customer.BillAddr.PostalCode");
    const wchar_t *CustomerBillAddrCountrySubDivisionCode = jsonResponse.stringOf(L"Customer.BillAddr.CountrySubDivisionCode");
    const wchar_t *CustomerBillAddrId = jsonResponse.stringOf(L"Customer.BillAddr.Id");
    const wchar_t *CustomerMiddleName = jsonResponse.stringOf(L"Customer.MiddleName");
    const wchar_t *CustomerNotes = jsonResponse.stringOf(L"Customer.Notes");
    bool CustomerActive = jsonResponse.BoolOf(L"Customer.Active");
    int CustomerBalance = jsonResponse.IntOf(L"Customer.Balance");
    const wchar_t *CustomerSyncToken = jsonResponse.stringOf(L"Customer.SyncToken");
    const wchar_t *CustomerSuffix = jsonResponse.stringOf(L"Customer.Suffix");
    const wchar_t *CustomerCompanyName = jsonResponse.stringOf(L"Customer.CompanyName");
    const wchar_t *CustomerFamilyName = jsonResponse.stringOf(L"Customer.FamilyName");
    const wchar_t *CustomerPrintOnCheckName = jsonResponse.stringOf(L"Customer.PrintOnCheckName");
    bool CustomerSparse = jsonResponse.BoolOf(L"Customer.sparse");
    const wchar_t *CustomerId = jsonResponse.stringOf(L"Customer.Id");
    const wchar_t *time = jsonResponse.stringOf(L"time");
    }