Chilkat Online Tools

unicodeC / New FreshBooks / Trial Balance

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW queryParams;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *account_name;
    const wchar_t *account_number;
    const wchar_t *account_sub_name;
    const wchar_t *account_sub_number;
    const wchar_t *Amount;
    const wchar_t *Code;
    const wchar_t *debitAmount;
    const wchar_t *debitCode;
    int sub_accountid;
    const wchar_t *Company_name;
    const wchar_t *Currency_code;
    const wchar_t *Download_token;
    const wchar_t *End_date;
    const wchar_t *Start_date;
    int i;
    int count_i;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    queryParams = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(queryParams,L"start_date",L"2019-01-01");
    CkJsonObjectW_UpdateString(queryParams,L"end_date",L"2019-12-31");
    CkJsonObjectW_UpdateString(queryParams,L"currency_code",L"USD");

    // Adds the "Authorization: Bearer <access_token>" header.
    CkHttpW_putAuthToken(http,L"<access_token>");

    resp = CkHttpW_QuickRequestParams(http,L"GET",L"https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/trial_balance",queryParams);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(queryParams);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(queryParams);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "response": {
    //     "result": {
    //       "trial_balance": {
    //         "company_name": "Postman Sandbox",
    //         "currency_code": "USD",
    //         "data": [
    //           {
    //             "account_name": "Cash",
    //             "account_number": "1000",
    //             "account_sub_name": "Petty Cash",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "6350.55",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 635972
    //           },
    //           {
    //             "account_name": "Accounts Receivable",
    //             "account_number": "1200",
    //             "account_sub_name": "Accounts Receivable",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "4435.00",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 635974
    //           },
    //           {
    //             "account_name": "Accounts Receivable",
    //             "account_number": "1200",
    //             "account_sub_name": "Customer Deposits",
    //             "account_sub_number": "2",
    //             "credit": {
    //               "amount": "400.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 635976
    //           },
    //           {
    //             "account_name": "Taxes Payable",
    //             "account_number": "2001",
    //             "account_sub_name": "HST",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "211.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 636766
    //           },
    //           {
    //             "account_name": "Customer Credit",
    //             "account_number": "2003",
    //             "account_sub_name": "Customer Credit",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "3120.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 635990
    //           },
    //           {
    //             "account_name": "Revenue",
    //             "account_number": "4000",
    //             "account_sub_name": "Sales",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "14215.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 636000
    //           },
    //           {
    //             "account_name": "Operating Expenses",
    //             "account_number": "6000",
    //             "account_sub_name": "Other Expenses",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "6195.57",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 636768
    //           },
    //           {
    //             "account_name": "Operating Expenses",
    //             "account_number": "6000",
    //             "account_sub_name": "Car & Truck Expenses",
    //             "account_sub_number": "3",
    //             "credit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "141.12",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 849638
    //           },
    //           {
    //             "account_name": "Taxes Paid",
    //             "account_number": "6001",
    //             "account_sub_name": "HST",
    //             "account_sub_number": "1",
    //             "credit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "805.42",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 636770
    //           },
    //           {
    //             "account_name": "Taxes Paid",
    //             "account_number": "6001",
    //             "account_sub_name": "other tax",
    //             "account_sub_number": "2",
    //             "credit": {
    //               "amount": "0.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "18.34",
    //               "code": "USD"
    //             },
    //             "sub_accountid": 849640
    //           },
    //           {
    //             "account_name": "total",
    //             "account_number": null,
    //             "account_sub_name": null,
    //             "account_sub_number": null,
    //             "credit": {
    //               "amount": "17946.00",
    //               "code": "USD"
    //             },
    //             "debit": {
    //               "amount": "17946.00",
    //               "code": "USD"
    //             },
    //             "sub_accountid": null
    //           }
    //         ],
    //         "download_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50aWQiOiJ3a01kMmciLCJ1c2VyaWQiOjEsImNvbnRhY3RpZCI6LTEsImxldmVsIjozLCJ0eXBlIjoicmVwb3J0Iiwib2JqZWN0aWQiOiJ0cmlhbF9iYWxhbmNlIiwicGFyYW1zIjp7InN0YXJ0X2RhdGUiOiIyMDE5LTAxLTAxIiwiZW5kX2RhdGUiOiIyMDE5LTEyLTMxIiwiY3VycmVuY3lfY29kZSI6IlVTRCJ9LCJpYXQiOjE1NTYxMzA3MTUsImV4cCI6MTU1ODcyMjcxNX0.NEZhIwWtFcRvvENs2voz9pR5PrOP_n7CbBYgf1sznGg",
    //         "end_date": "2019-12-31",
    //         "start_date": "2019-01-01"
    //       }
    //     }
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    Company_name = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.company_name");
    Currency_code = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.currency_code");
    Download_token = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.download_token");
    End_date = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.end_date");
    Start_date = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.start_date");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"response.result.trial_balance.data");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        account_name = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].account_name");
        account_number = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].account_number");
        account_sub_name = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].account_sub_name");
        account_sub_number = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].account_sub_number");
        Amount = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].credit.amount");
        Code = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].credit.code");
        debitAmount = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].debit.amount");
        debitCode = CkJsonObjectW_stringOf(jResp,L"response.result.trial_balance.data[i].debit.code");
        sub_accountid = CkJsonObjectW_IntOf(jResp,L"response.result.trial_balance.data[i].sub_accountid");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(queryParams);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -G -d "start_date=2019-01-01"
	-d "end_date=2019-12-31"
	-d "currency_code=USD"
	-H "Authorization: Bearer <access_token>"
https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/trial_balance

Postman Collection Item JSON

{
  "name": "Trial Balance",
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/trial_balance?start_date=2019-01-01&end_date=2019-12-31&currency_code=USD",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "reports",
        "accounting",
        "trial_balance"
      ],
      "query": [
        {
          "key": "start_date",
          "value": "2019-01-01"
        },
        {
          "key": "end_date",
          "value": "2019-12-31"
        },
        {
          "key": "currency_code",
          "value": "USD"
        }
      ]
    },
    "description": "A quick gut check to make sure your books are balanced."
  },
  "response": [
    {
      "name": "Trial Balance",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/reports/accounting/trial_balance?start_date=2019-01-01&end_date=2019-12-31&currency_code=USD",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "reports",
            "accounting",
            "trial_balance"
          ],
          "query": [
            {
              "key": "start_date",
              "value": "2019-01-01"
            },
            {
              "key": "end_date",
              "value": "2019-12-31"
            },
            {
              "key": "currency_code",
              "value": "USD"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gWVEZcEBIQWFEBWw1NVkBbXQJLBEEOQRYXFBtRSlMWAwhSU1MDBANUVwkOClNQU1AcTwlOGgkEVwVcDgNVWVEAVgUCV1NBFFVRCBIHag=="
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:31:55 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Age",
          "value": "0"
        },
        {
          "key": "Age",
          "value": "0"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:31:55 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"trial_balance\": {\n                \"company_name\": \"Postman Sandbox\",\n                \"currency_code\": \"USD\",\n                \"data\": [\n                    {\n                        \"account_name\": \"Cash\",\n                        \"account_number\": \"1000\",\n                        \"account_sub_name\": \"Petty Cash\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"6350.55\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 635972\n                    },\n                    {\n                        \"account_name\": \"Accounts Receivable\",\n                        \"account_number\": \"1200\",\n                        \"account_sub_name\": \"Accounts Receivable\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"4435.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 635974\n                    },\n                    {\n                        \"account_name\": \"Accounts Receivable\",\n                        \"account_number\": \"1200\",\n                        \"account_sub_name\": \"Customer Deposits\",\n                        \"account_sub_number\": \"2\",\n                        \"credit\": {\n                            \"amount\": \"400.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 635976\n                    },\n                    {\n                        \"account_name\": \"Taxes Payable\",\n                        \"account_number\": \"2001\",\n                        \"account_sub_name\": \"HST\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"211.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 636766\n                    },\n                    {\n                        \"account_name\": \"Customer Credit\",\n                        \"account_number\": \"2003\",\n                        \"account_sub_name\": \"Customer Credit\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"3120.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 635990\n                    },\n                    {\n                        \"account_name\": \"Revenue\",\n                        \"account_number\": \"4000\",\n                        \"account_sub_name\": \"Sales\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"14215.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 636000\n                    },\n                    {\n                        \"account_name\": \"Operating Expenses\",\n                        \"account_number\": \"6000\",\n                        \"account_sub_name\": \"Other Expenses\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"6195.57\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 636768\n                    },\n                    {\n                        \"account_name\": \"Operating Expenses\",\n                        \"account_number\": \"6000\",\n                        \"account_sub_name\": \"Car & Truck Expenses\",\n                        \"account_sub_number\": \"3\",\n                        \"credit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"141.12\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 849638\n                    },\n                    {\n                        \"account_name\": \"Taxes Paid\",\n                        \"account_number\": \"6001\",\n                        \"account_sub_name\": \"HST\",\n                        \"account_sub_number\": \"1\",\n                        \"credit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"805.42\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 636770\n                    },\n                    {\n                        \"account_name\": \"Taxes Paid\",\n                        \"account_number\": \"6001\",\n                        \"account_sub_name\": \"other tax\",\n                        \"account_sub_number\": \"2\",\n                        \"credit\": {\n                            \"amount\": \"0.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"18.34\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": 849640\n                    },\n                    {\n                        \"account_name\": \"total\",\n                        \"account_number\": null,\n                        \"account_sub_name\": null,\n                        \"account_sub_number\": null,\n                        \"credit\": {\n                            \"amount\": \"17946.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"debit\": {\n                            \"amount\": \"17946.00\",\n                            \"code\": \"USD\"\n                        },\n                        \"sub_accountid\": null\n                    }\n                ],\n                \"download_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50aWQiOiJ3a01kMmciLCJ1c2VyaWQiOjEsImNvbnRhY3RpZCI6LTEsImxldmVsIjozLCJ0eXBlIjoicmVwb3J0Iiwib2JqZWN0aWQiOiJ0cmlhbF9iYWxhbmNlIiwicGFyYW1zIjp7InN0YXJ0X2RhdGUiOiIyMDE5LTAxLTAxIiwiZW5kX2RhdGUiOiIyMDE5LTEyLTMxIiwiY3VycmVuY3lfY29kZSI6IlVTRCJ9LCJpYXQiOjE1NTYxMzA3MTUsImV4cCI6MTU1ODcyMjcxNX0.NEZhIwWtFcRvvENs2voz9pR5PrOP_n7CbBYgf1sznGg\",\n                \"end_date\": \"2019-12-31\",\n                \"start_date\": \"2019-01-01\"\n            }\n        }\n    }\n}"
    }
  ]
}