Chilkat Online Tools

SQL Server / New FreshBooks / Get Bills

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @queryParams int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'search[date_min]', '2021-10-09'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'search[date_max]', 'search[date_min]'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequestParams', @resp OUT, 'GET', 'https://api.freshbooks.com/accounting/account/5pyBd/bills/bills', @queryParams
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @queryParams
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @queryParams
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "response": {
    --     "result": {
    --       "bill_vendors": [
    --         {
    --           "account_number": "8394858982590",
    --           "city": null,
    --           "country": null,
    --           "created_at": "2020-10-06 14:20:20",
    --           "currency_code": "CAD",
    --           "is_1099": false,
    --           "language": "en",
    --           "outstanding_balance": [
    --             {
    --               "amount": {
    --                 "amount": "5.00",
    --                 "code": "CAD"
    --               }
    --             }
    --           ],
    --           "overdue_balance": [
    --           ],
    --           "phone": "8335970553",
    --           "postal_code": null,
    --           "primary_contact_email": "hdavid@freshbooks.com",
    --           "primary_contact_first_name": "Hannah",
    --           "primary_contact_last_name": "David",
    --           "province": null,
    --           "street": null,
    --           "street2": null,
    --           "tax_defaults": [
    --             {
    --               "amount": "13.56",
    --               "created_at": "2020-10-06 14:23:06",
    --               "enabled": true,
    --               "name": "GST",
    --               "system_taxid": 36859,
    --               "tax_authorityid": null,
    --               "taxid": 1,
    --               "updated_at": "2020-10-06 14:23:06",
    --               "vendorid": 1
    --             },
    --             {
    --               "amount": "13",
    --               "created_at": "2020-10-06 14:23:06",
    --               "enabled": true,
    --               "name": "HST",
    --               "system_taxid": 37696,
    --               "tax_authorityid": null,
    --               "taxid": 3,
    --               "updated_at": "2020-10-06 14:23:06",
    --               "vendorid": 1
    --             }
    --           ],
    --           "updated_at": "2020-10-06 14:22:49",
    --           "vendor_name": "Staples",
    --           "vendorid": 1,
    --           "vis_state": 0,
    --           "website": null
    --         }
    --       ],
    --       "page": 1,
    --       "pages": 1,
    --       "per_page": 15,
    --       "total": 1
    --     }
    --   }
    -- }

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

    DECLARE @account_number nvarchar(4000)

    DECLARE @city nvarchar(4000)

    DECLARE @country nvarchar(4000)

    DECLARE @created_at nvarchar(4000)

    DECLARE @currency_code nvarchar(4000)

    DECLARE @is_1099 int

    DECLARE @language nvarchar(4000)

    DECLARE @phone nvarchar(4000)

    DECLARE @postal_code nvarchar(4000)

    DECLARE @primary_contact_email nvarchar(4000)

    DECLARE @primary_contact_first_name nvarchar(4000)

    DECLARE @primary_contact_last_name nvarchar(4000)

    DECLARE @province nvarchar(4000)

    DECLARE @street nvarchar(4000)

    DECLARE @street2 nvarchar(4000)

    DECLARE @updated_at nvarchar(4000)

    DECLARE @vendor_name nvarchar(4000)

    DECLARE @vendorid int

    DECLARE @vis_state int

    DECLARE @website nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @Amount nvarchar(4000)

    DECLARE @Code nvarchar(4000)

    DECLARE @amount nvarchar(4000)

    DECLARE @enabled int

    DECLARE @name nvarchar(4000)

    DECLARE @system_taxid int

    DECLARE @tax_authorityid nvarchar(4000)

    DECLARE @taxid int

    DECLARE @Page int
    EXEC sp_OAMethod @jResp, 'IntOf', @Page OUT, 'response.result.page'
    DECLARE @Pages int
    EXEC sp_OAMethod @jResp, 'IntOf', @Pages OUT, 'response.result.pages'
    DECLARE @Per_page int
    EXEC sp_OAMethod @jResp, 'IntOf', @Per_page OUT, 'response.result.per_page'
    DECLARE @Total int
    EXEC sp_OAMethod @jResp, 'IntOf', @Total OUT, 'response.result.total'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.bill_vendors'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @account_number OUT, 'response.result.bill_vendors[i].account_number'
        EXEC sp_OAMethod @jResp, 'StringOf', @city OUT, 'response.result.bill_vendors[i].city'
        EXEC sp_OAMethod @jResp, 'StringOf', @country OUT, 'response.result.bill_vendors[i].country'
        EXEC sp_OAMethod @jResp, 'StringOf', @created_at OUT, 'response.result.bill_vendors[i].created_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @currency_code OUT, 'response.result.bill_vendors[i].currency_code'
        EXEC sp_OAMethod @jResp, 'BoolOf', @is_1099 OUT, 'response.result.bill_vendors[i].is_1099'
        EXEC sp_OAMethod @jResp, 'StringOf', @language OUT, 'response.result.bill_vendors[i].language'
        EXEC sp_OAMethod @jResp, 'StringOf', @phone OUT, 'response.result.bill_vendors[i].phone'
        EXEC sp_OAMethod @jResp, 'StringOf', @postal_code OUT, 'response.result.bill_vendors[i].postal_code'
        EXEC sp_OAMethod @jResp, 'StringOf', @primary_contact_email OUT, 'response.result.bill_vendors[i].primary_contact_email'
        EXEC sp_OAMethod @jResp, 'StringOf', @primary_contact_first_name OUT, 'response.result.bill_vendors[i].primary_contact_first_name'
        EXEC sp_OAMethod @jResp, 'StringOf', @primary_contact_last_name OUT, 'response.result.bill_vendors[i].primary_contact_last_name'
        EXEC sp_OAMethod @jResp, 'StringOf', @province OUT, 'response.result.bill_vendors[i].province'
        EXEC sp_OAMethod @jResp, 'StringOf', @street OUT, 'response.result.bill_vendors[i].street'
        EXEC sp_OAMethod @jResp, 'StringOf', @street2 OUT, 'response.result.bill_vendors[i].street2'
        EXEC sp_OAMethod @jResp, 'StringOf', @updated_at OUT, 'response.result.bill_vendors[i].updated_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @vendor_name OUT, 'response.result.bill_vendors[i].vendor_name'
        EXEC sp_OAMethod @jResp, 'IntOf', @vendorid OUT, 'response.result.bill_vendors[i].vendorid'
        EXEC sp_OAMethod @jResp, 'IntOf', @vis_state OUT, 'response.result.bill_vendors[i].vis_state'
        EXEC sp_OAMethod @jResp, 'StringOf', @website OUT, 'response.result.bill_vendors[i].website'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.bill_vendors[i].outstanding_balance'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @Amount OUT, 'response.result.bill_vendors[i].outstanding_balance[j].amount.amount'
            EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'response.result.bill_vendors[i].outstanding_balance[j].amount.code'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.bill_vendors[i].overdue_balance'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'response.result.bill_vendors[i].tax_defaults'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @amount OUT, 'response.result.bill_vendors[i].tax_defaults[j].amount'
            EXEC sp_OAMethod @jResp, 'StringOf', @created_at OUT, 'response.result.bill_vendors[i].tax_defaults[j].created_at'
            EXEC sp_OAMethod @jResp, 'BoolOf', @enabled OUT, 'response.result.bill_vendors[i].tax_defaults[j].enabled'
            EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'response.result.bill_vendors[i].tax_defaults[j].name'
            EXEC sp_OAMethod @jResp, 'IntOf', @system_taxid OUT, 'response.result.bill_vendors[i].tax_defaults[j].system_taxid'
            EXEC sp_OAMethod @jResp, 'StringOf', @tax_authorityid OUT, 'response.result.bill_vendors[i].tax_defaults[j].tax_authorityid'
            EXEC sp_OAMethod @jResp, 'IntOf', @taxid OUT, 'response.result.bill_vendors[i].tax_defaults[j].taxid'
            EXEC sp_OAMethod @jResp, 'StringOf', @updated_at OUT, 'response.result.bill_vendors[i].tax_defaults[j].updated_at'
            EXEC sp_OAMethod @jResp, 'IntOf', @vendorid OUT, 'response.result.bill_vendors[i].tax_defaults[j].vendorid'
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @queryParams
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -G -d "search[date_min]=2021-10-09"
	-d "search[date_max]=search%5Bdate_min%5D"
	-H "Authorization: Bearer <access_token>"
https://api.freshbooks.com/accounting/account/5pyBd/bills/bills

Postman Collection Item JSON

{
  "name": "Get Bills",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"billId\", jsonData.response.result.bills[0].id)"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "eyJraWQiOiJhMTlPSlR5aVlKOXhPM3FoWnhWeE1KZE5ZNXJ4cUhpQzBSTUY0TWRheGtjIiwiYWxnIjoiUlMyNTYifQ.eyJqdGkiOiJjZTI5MTQ1MWUxMTYyMThmNDdlZDhhMGIyYWJhZGVkODhhZDIyZWUxNDlhNmI0OTczMzVlNWU3MDMxNTdmZDJmIiwiaWF0IjoxNjM3MDc4ODUyLCJleHAiOjE2MzcxMjIwNTIsInN1YiI6IlNtdXhJZGVudGl0eTs2NzA1NDgiLCJjbGllbnRfaWQiOiI1MzFlMjI0NzMxYWUxYjcyNzkyMDUwYzIxYjdjYjg4YjQ4YjVjZmYyM2IyNjQ0OGYxMGM3YjZkZDE2YzM4M2NiIiwic2NvcGUiOiJhZG1pbjphbGwiLCJhdXRoLmZyZXNoYm9va3MuY29tL3N1Yl90eXBlIjoicmVzb3VyY2Vfb3duZXIifQ.XZYsOTZZJyxc4ny1NVrAShnNqbpS1GGc54jlRSO8gll4axcdadpluV_gflS_CyZfPSw6ivhu1lwxczj5xGNllZkMI85nVs17EPmpq4MqO9b08oaWoWIptxD329uO893jxW4cFQdGLbCAnJ2BNddWoVIaHS-1EiVPbEh8EKTFeJ-j5tYHRbWcuy32ftuKrqX53z5dY3sUMRsmhsGMfOEzTUfz0J1epHqJSDGHn-HQLljZ4fbmGwCf4LlWPnsgGJj5nDxiRWbUOPXW2J73_tAAH2-u49MRhO2nf_8meHKHhCVZZ0PM4lPzaAqv5OkofwCSEEgIX4Af3Ifh88O9uLtbGg",
          "type": "string"
        }
      ]
    },
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/5pyBd/bills/bills?search[date_min]=2021-10-09&search[date_max]=search[date_min]",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "5pyBd",
        "bills",
        "bills"
      ],
      "query": [
        {
          "key": "search[date_min]",
          "value": "2021-10-09"
        },
        {
          "key": "search[date_max]",
          "value": "search[date_min]"
        }
      ]
    }
  },
  "response": [
    {
      "name": "https://api.freshbooks.com/accounting/account/{{accountId}}/bill_vendors/bill_vendors",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/bill_vendors/bill_vendors",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "bill_vendors",
            "bill_vendors"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Date",
          "value": "Fri, 09 Oct 2020 15:19:07 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gGWFpfPRAGDFQNShECWl1eXxNcD1UOQRFGGgdNVk0LBQlRU1sOBwVTVwgABFBeVk4dUxRACVxRAFIGC1QHBAdaV1RWA0RPXlJcFwQ/"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Content-Security-Policy",
          "value": "default-src 'self'"
        },
        {
          "key": "X-Content-Security-Policy",
          "value": "default-src 'self'"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31556926; includeSubDomains; preload"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin-when-cross-origin"
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "600"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "599"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1602256808"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Alt-Svc",
          "value": "h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"bill_vendors\": [\n                {\n                    \"account_number\": \"8394858982590\",\n                    \"city\": null,\n                    \"country\": null,\n                    \"created_at\": \"2020-10-06 14:20:20\",\n                    \"currency_code\": \"CAD\",\n                    \"is_1099\": false,\n                    \"language\": \"en\",\n                    \"outstanding_balance\": [\n                        {\n                            \"amount\": {\n                                \"amount\": \"5.00\",\n                                \"code\": \"CAD\"\n                            }\n                        }\n                    ],\n                    \"overdue_balance\": [],\n                    \"phone\": \"8335970553\",\n                    \"postal_code\": null,\n                    \"primary_contact_email\": \"hdavid@freshbooks.com\",\n                    \"primary_contact_first_name\": \"Hannah\",\n                    \"primary_contact_last_name\": \"David\",\n                    \"province\": null,\n                    \"street\": null,\n                    \"street2\": null,\n                    \"tax_defaults\": [\n                        {\n                            \"amount\": \"13.56\",\n                            \"created_at\": \"2020-10-06 14:23:06\",\n                            \"enabled\": true,\n                            \"name\": \"GST\",\n                            \"system_taxid\": 36859,\n                            \"tax_authorityid\": null,\n                            \"taxid\": 1,\n                            \"updated_at\": \"2020-10-06 14:23:06\",\n                            \"vendorid\": 1\n                        },\n                        {\n                            \"amount\": \"13\",\n                            \"created_at\": \"2020-10-06 14:23:06\",\n                            \"enabled\": true,\n                            \"name\": \"HST\",\n                            \"system_taxid\": 37696,\n                            \"tax_authorityid\": null,\n                            \"taxid\": 3,\n                            \"updated_at\": \"2020-10-06 14:23:06\",\n                            \"vendorid\": 1\n                        }\n                    ],\n                    \"updated_at\": \"2020-10-06 14:22:49\",\n                    \"vendor_name\": \"Staples\",\n                    \"vendorid\": 1,\n                    \"vis_state\": 0,\n                    \"website\": null\n                }\n            ],\n            \"page\": 1,\n            \"pages\": 1,\n            \"per_page\": 15,\n            \"total\": 1\n        }\n    }\n}"
    }
  ]
}