Sample code for 30+ languages & platforms
Unicode C

QuickBooks - Create an Account

See more QuickBooks Examples

Demonstrates how to send an JSON request to create a QuickBooks account.

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 bAutoReconnect;
    HCkStringBuilderW sbAuth;
    HCkJsonObjectW jsonRequest;
    const wchar_t *requestBody;
    HCkStringBuilderW sbPath;
    const wchar_t *responseBody;
    HCkJsonObjectW jsonResponse;

    success = FALSE;

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

    rest = CkRestW_Create();

    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"sandbox-quickbooks.api.intuit.com",443,TRUE,bAutoReconnect);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(jsonToken);
        CkRestW_Dispose(rest);
        return;
    }

    sbAuth = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbAuth,L"Bearer ");
    CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,L"access_token"));
    CkRestW_putAuthorization(rest,CkStringBuilderW_getAsString(sbAuth));

    jsonRequest = CkJsonObjectW_Create();
    CkJsonObjectW_AppendString(jsonRequest,L"AccountType",L"Credit Card");
    CkJsonObjectW_AppendString(jsonRequest,L"Name",L"Banana Republic");
    requestBody = CkJsonObjectW_emit(jsonRequest);

    // "123146096291789" is the company ID.
    sbPath = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbPath,L"/v3/company/123146096291789/account?minorversion=45");

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

    responseBody = CkRestW_fullRequestString(rest,L"POST",CkStringBuilderW_getAsString(sbPath),requestBody);
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(jsonToken);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbAuth);
        CkJsonObjectW_Dispose(jsonRequest);
        CkStringBuilderW_Dispose(sbPath);
        return;
    }

    // We should expect a 200 response if successful.
    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"Request Header: \n");
        wprintf(L"%s\n",CkRestW_lastRequestHeader(rest));
        wprintf(L"----\n");
        wprintf(L"Response StatusCode = %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"Response StatusLine: %s\n",CkRestW_responseStatusText(rest));
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"%s\n",responseBody);
        CkJsonObjectW_Dispose(jsonToken);
        CkRestW_Dispose(rest);
        CkStringBuilderW_Dispose(sbAuth);
        CkJsonObjectW_Dispose(jsonRequest);
        CkStringBuilderW_Dispose(sbPath);
        return;
    }

    jsonResponse = CkJsonObjectW_Create();
    CkJsonObjectW_Load(jsonResponse,responseBody);
    CkJsonObjectW_putEmitCompact(jsonResponse,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse));
    wprintf(L"Success.\n");

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


    CkJsonObjectW_Dispose(jsonToken);
    CkRestW_Dispose(rest);
    CkStringBuilderW_Dispose(sbAuth);
    CkJsonObjectW_Dispose(jsonRequest);
    CkStringBuilderW_Dispose(sbPath);
    CkJsonObjectW_Dispose(jsonResponse);

    }