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 <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkStringBuilderW.h>

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

    CkRestW rest;

    bool bAutoReconnect = true;
    success = rest.Connect(L"sandbox-quickbooks.api.intuit.com",443,true,bAutoReconnect);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

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

    CkJsonObjectW jsonRequest;
    jsonRequest.AppendString(L"AccountType",L"Credit Card");
    jsonRequest.AppendString(L"Name",L"Banana Republic");
    const wchar_t *requestBody = jsonRequest.emit();

    // "123146096291789" is the company ID.
    CkStringBuilderW sbPath;
    sbPath.Append(L"/v3/company/123146096291789/account?minorversion=45");

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

    const wchar_t *responseBody = rest.fullRequestString(L"POST",sbPath.getAsString(),requestBody);
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    // We should expect a 200 response if successful.
    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"Request Header: \n");
        wprintf(L"%s\n",rest.lastRequestHeader());
        wprintf(L"----\n");
        wprintf(L"Response StatusCode = %d\n",rest.get_ResponseStatusCode());
        wprintf(L"Response StatusLine: %s\n",rest.responseStatusText());
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"%s\n",responseBody);
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.Load(responseBody);
    jsonResponse.put_EmitCompact(false);
    wprintf(L"%s\n",jsonResponse.emit());
    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"
    // }
    }