Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

QuickBooks - Query Employees

See more QuickBooks Examples

Demonstrates how to query the QuickBooks employee data.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    // Get our previously obtained OAuth2 access token, which should contain JSON like this:
    // {
    //   "expires_in": 3600,
    //   "x_refresh_token_expires_in": 8726400,
    //   "refresh_token": "L011546037639r ... 3vR2DrbOmg0Sdagw",
    //   "access_token": "eyJlbmMiOiJBMTI4Q0 ... oETJEMbeggg",
    //   "token_type": "bearer"
    // }

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    try json_token.loadFile("qa_data/tokens/qb-access-token.json");

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect using TLS.
    // A single REST object, once connected, can be used for many Quickbooks REST API calls.
    // The auto-reconnect indicates that if the already-established HTTPS connection is closed,
    // then it will be automatically re-established as needed.
    const b_auto_reconnect = true;
    rest.connect("sandbox-quickbooks.api.intuit.com", 443, true, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const sb_auth = try chilkat.StringBuilder.init();
    defer sb_auth.deinit();
    sb_auth.append("Bearer ") catch {};
    sb_auth.append(try json_token.stringOf(alloc, "access_token")) catch {};
    rest.setAuthorization(try sb_auth.getAsString(alloc));

    rest.addHeader("Accept", "application/json") catch {};
    rest.setAllowHeaderFolding(false);

    const query = "select * from Employee";
    rest.addQueryParam("query", query) catch {};
    rest.addQueryParam("minorversion", "45") catch {};

    // The company ID is 123146096291789
    const response_body = rest.fullRequestNoBody(alloc, "GET", "/v3/company/123146096291789/query") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // We should expect a 200 response if successful.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("Request Header: \n", .{});
        std.debug.print("{s}\n", .{try rest.getLastRequestHeader(alloc)});
        std.debug.print("----\n", .{});
        std.debug.print("Response StatusCode = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("Response StatusLine: {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("{s}\n", .{response_body});
        return;
    }

    // Load the JSON response into a JSON object for parsing.
    // A sample JSON response is shown below.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(response_body) catch {};

    // Show the full JSON response.
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // These will be used for parsing date/time strings..
    const dtime = try chilkat.DateTime.init();
    defer dtime.deinit();
    const b_local_time = true;
    const dt = try chilkat.DtObj.init();
    defer dt.deinit();

    // Iterate over each account returned.
    const num_records = json.sizeOfArray("QueryResponse.Employee");
    var i: i32 = 0;
    while (i < num_records) {
        json.setI(i);
        std.debug.print("DisplayName: {s}\n", .{try json.stringOf(alloc, "QueryResponse.Employee[i].DisplayName")});
        std.debug.print("Id: {s}\n", .{try json.stringOf(alloc, "QueryResponse.Employee[i].Id")});
        std.debug.print("Active: {}\n", .{json.boolOf("QueryResponse.Employee[i].Active")});

        // Load the CreateTime into a CkDateTime...
        dtime.setFromTimestamp(try json.stringOf(alloc, "QueryResponse.Employee[i].MetaData.CreateTime")) catch {};
        dtime.toDtObj(b_local_time, dt);

        std.debug.print("{d}/{d}/{d}  {d}:{d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

        // The the birth year, month, and day, if it exists.
        // The BirthDate contains something like this: "1985-09-11"
        if (json.hasMember("QueryResponse.Employee[i].BirthDate")) {
            const s_tab = try chilkat.StringTable.init();
            defer s_tab.deinit();
            s_tab.splitAndAppend(try json.stringOf(alloc, "QueryResponse.Employee[i].BirthDate"), "-", true, true) catch {};
            const year = try s_tab.stringAt(alloc, 0);
            const month = try s_tab.stringAt(alloc, 1);
            const day = try s_tab.stringAt(alloc, 2);
            std.debug.print("BirthDate: {s}/{s}/{s}\n", .{ month, day, year });
        }

        std.debug.print("----\n", .{});
        i = i + 1;
    }

    std.debug.print("Success.\n", .{});

    // ------------------------------------------------------
    // The JSON response looks like this:

    // {
    //   "QueryResponse": {
    //     "Employee": [
    //       {
    //         "BillableTime": false,
    //         "domain": "QBO",
    //         "sparse": false,
    //         "Id": "55",
    //         "SyncToken": "0",
    //         "MetaData": {
    //           "CreateTime": "2016-09-15T11:21:48-07:00",
    //           "LastUpdatedTime": "2016-09-15T11:21:48-07:00"
    //         },
    //         "GivenName": "Emily",
    //         "FamilyName": "Platt",
    //         "DisplayName": "Emily Platt",
    //         "PrintOnCheckName": "Emily Platt",
    //         "Active": true
    //       },
    //       {
    //         "BillableTime": false,
    //         "BirthDate": "1985-09-11",
    //         "HiredDate": "2016-08-17",
    //         "domain": "QBO",
    //         "sparse": false,
    //         "Id": "54",
    //         "SyncToken": "1",
    //         "MetaData": {
    //           "CreateTime": "2016-09-15T11:21:28-07:00",
    //           "LastUpdatedTime": "2016-09-15T11:46:26-07:00"
    //         },
    //         "GivenName": "John",
    //         "FamilyName": "Johnson",
    //         "DisplayName": "John Johnson",
    //         "PrintOnCheckName": "John Johnson",
    //         "Active": true,
    //         "PrimaryPhone": {
    //           "FreeFormNumber": "(540) 555-9645"
    //         }
    //       },
    //       {
    //         "SSN": "XXX-XX-XXXX",
    //         "PrimaryAddr": {
    //           "Id": "98",
    //           "Line1": "45 N. Elm Street",
    //           "City": "Middlefield",
    //           "CountrySubDivisionCode": "CA",
    //           "PostalCode": "93242"
    //         },
    //         "BillableTime": false,
    //         "domain": "QBO",
    //         "sparse": false,
    //         "Id": "60",
    //         "SyncToken": "0",
    //         "MetaData": {
    //           "CreateTime": "2016-10-25T14:08:18-07:00",
    //           "LastUpdatedTime": "2016-10-25T14:08:18-07:00"
    //         },
    //         "GivenName": "John",
    //         "FamilyName": "Meuller",
    //         "DisplayName": "John Meuller",
    //         "PrintOnCheckName": "John Meuller",
    //         "Active": true,
    //         "PrimaryPhone": {
    //           "FreeFormNumber": "408-525-1234"
    //         }
    //       }
    //     ],
    //     "startPosition": 1,
    //     "maxResults": 3
    //   },
    //   "time": "2016-10-25T14:18:38.487-07:00"
    // }
}