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 <C_CkJsonObjectW.h>
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jsonToken;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStringBuilderW sbAuth;
    HCkJsonObjectW jsonReq;
    HCkStringBuilderW sbRequestBody;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;
    HCkJsonObjectW jsonResponse;
    const wchar_t *CustomerDomain;
    const wchar_t *CustomerPrimaryEmailAddrAddress;
    const wchar_t *CustomerDisplayName;
    const wchar_t *CustomerCurrencyRefName;
    const wchar_t *CustomerCurrencyRefValue;
    const wchar_t *CustomerDefaultTaxCodeRefValue;
    const wchar_t *CustomerPreferredDeliveryMethod;
    const wchar_t *CustomerGivenName;
    const wchar_t *CustomerFullyQualifiedName;
    BOOL CustomerBillWithParent;
    const wchar_t *CustomerTitle;
    BOOL CustomerJob;
    int CustomerBalanceWithJobs;
    const wchar_t *CustomerPrimaryPhoneFreeFormNumber;
    BOOL CustomerTaxable;
    const wchar_t *CustomerMetaDataCreateTime;
    const wchar_t *CustomerMetaDataLastUpdatedTime;
    const wchar_t *CustomerBillAddrCity;
    const wchar_t *CustomerBillAddrCountry;
    const wchar_t *CustomerBillAddrLine1;
    const wchar_t *CustomerBillAddrPostalCode;
    const wchar_t *CustomerBillAddrCountrySubDivisionCode;
    const wchar_t *CustomerBillAddrId;
    const wchar_t *CustomerMiddleName;
    const wchar_t *CustomerNotes;
    BOOL CustomerActive;
    int CustomerBalance;
    const wchar_t *CustomerSyncToken;
    const wchar_t *CustomerSuffix;
    const wchar_t *CustomerCompanyName;
    const wchar_t *CustomerFamilyName;
    const wchar_t *CustomerPrintOnCheckName;
    BOOL CustomerSparse;
    const wchar_t *CustomerId;
    const wchar_t *time;

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

    rest = CkRestW_Create();

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

    sbAuth = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbAuth,L"Bearer ");
    CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,L"access_token"));
    CkRestW_putAuthorization(rest,CkStringBuilderW_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

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

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(jsonReq,sbRequestBody);

    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
    CkRestW_AddHeader(rest,L"Accept",L"application/json");
    CkRestW_putAllowHeaderFolding(rest,FALSE);

    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/v3/company/<realmID>/customer",sbRequestBody,sbResponseBody);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(jsonToken);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbAuth);
        CkJsonObjectW_Dispose(jsonReq);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);

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

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jsonResponse,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse));

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"Failed.\n");
        CkJsonObjectW_Dispose(jsonToken);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbAuth);
        CkJsonObjectW_Dispose(jsonReq);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jsonResponse);
        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.

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


    CkJsonObjectW_Dispose(jsonToken);
    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbAuth);
    CkJsonObjectW_Dispose(jsonReq);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jsonResponse);

    }