Chilkat Online Tools

Node.js / New FreshBooks / List Staff

Back to Collection Items

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}


function chilkatExample() {

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

    var http = new chilkat.Http();
    var success;

    // Adds the "Authorization: Bearer <access_token>" header.
    http.AuthToken = "<access_token>";
    http.SetRequestHeader("Content-Type","application/json");

    var sbResponseBody = new chilkat.StringBuilder();
    success = http.QuickGetSb("https://api.freshbooks.com/auth/api/v1/users/business/{{businessId}}",sbResponseBody);
    if (success == false) {
        console.log(http.LastErrorText);
        return;
    }

    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = http.LastStatus;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(http.LastHeader);
        console.log("Failed.");
        return;
    }

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

    // {
    //   "response": {
    //     "id": 1966214,
    //     "name": "Postman Sandbox",
    //     "account_id": "wkMd2g",
    //     "business_group": {
    //       "id": 4942664,
    //       "category": "business",
    //       "members": [
    //         {
    //           "id": 6446238,
    //           "group_id": 4942664,
    //           "role": "business_partner",
    //           "identity_id": 37256,
    //           "first_name": "Marshall",
    //           "last_name": "Johnston",
    //           "email": "api.freshbooks@gmail.com",
    //           "company": "Postman Sandbox",
    //           "business_id": 1966214,
    //           "unacknowledged_change": false,
    //           "active": true
    //         },
    //         {
    //           "id": 6475824,
    //           "group_id": 4942664,
    //           "role": "business_accountant",
    //           "identity_id": 75537,
    //           "first_name": "Marshall",
    //           "last_name": "Johnston",
    //           "email": "api.freshbooks@gmail.com",
    //           "company": "Postman Sandbox",
    //           "business_id": 1966214,
    //           "unacknowledged_change": false,
    //           "active": true
    //         },
    //         {
    //           "id": 6446130,
    //           "group_id": 4942664,
    //           "role": "owner",
    //           "identity_id": 1882548,
    //           "first_name": "Ryan",
    //           "last_name": "Marr",
    //           "email": "api.freshbooks@gmail.com",
    //           "company": "Postman Sandbox",
    //           "business_id": 1966214,
    //           "unacknowledged_change": false,
    //           "active": true
    //         }
    //       ]
    //     },
    //     "date_format": "mm/dd/yyyy",
    //     "address": {
    //       "id": 2421054,
    //       "street": null,
    //       "city": null,
    //       "province": null,
    //       "country": "Canada",
    //       "postal_code": null
    //     },
    //     "phone_number": null,
    //     "business_clients": [
    //     ]
    //   }
    // }

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

    var Date_format = new chilkat.DtObj();
    var id;
    var group_id;
    var role;
    var identity_id;
    var first_name;
    var last_name;
    var email;
    var company;
    var business_id;
    var unacknowledged_change;
    var active;

    var Id = jResp.IntOf("response.id");
    var Name = jResp.StringOf("response.name");
    var Account_id = jResp.StringOf("response.account_id");
    var Business_groupId = jResp.IntOf("response.business_group.id");
    var Category = jResp.StringOf("response.business_group.category");
    jResp.DtOf("response.date_format",false,Date_format);
    var AddressId = jResp.IntOf("response.address.id");
    var Street = jResp.StringOf("response.address.street");
    var City = jResp.StringOf("response.address.city");
    var Province = jResp.StringOf("response.address.province");
    var Country = jResp.StringOf("response.address.country");
    var Postal_code = jResp.StringOf("response.address.postal_code");
    var Phone_number = jResp.StringOf("response.phone_number");
    var i = 0;
    var count_i = jResp.SizeOfArray("response.business_group.members");
    while (i < count_i) {
        jResp.I = i;
        id = jResp.IntOf("response.business_group.members[i].id");
        group_id = jResp.IntOf("response.business_group.members[i].group_id");
        role = jResp.StringOf("response.business_group.members[i].role");
        identity_id = jResp.IntOf("response.business_group.members[i].identity_id");
        first_name = jResp.StringOf("response.business_group.members[i].first_name");
        last_name = jResp.StringOf("response.business_group.members[i].last_name");
        email = jResp.StringOf("response.business_group.members[i].email");
        company = jResp.StringOf("response.business_group.members[i].company");
        business_id = jResp.IntOf("response.business_group.members[i].business_id");
        unacknowledged_change = jResp.BoolOf("response.business_group.members[i].unacknowledged_change");
        active = jResp.BoolOf("response.business_group.members[i].active");
        i = i+1;
    }

    i = 0;
    count_i = jResp.SizeOfArray("response.business_clients");
    while (i < count_i) {
        jResp.I = i;
        i = i+1;
    }


}

chilkatExample();

Curl Command

curl -X GET
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
https://api.freshbooks.com/auth/api/v1/users/business/{{businessId}}

Postman Collection Item JSON

{
  "name": "List Staff",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"identityId\", jsonData.response.business_group.members[0].identity_id)"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "url": {
      "raw": "https://api.freshbooks.com/auth/api/v1/users/business/{{businessId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "auth",
        "api",
        "v1",
        "users",
        "business",
        "{{businessId}}"
      ]
    }
  },
  "response": [
    {
      "name": "List Staff",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "url": {
          "raw": "https://api.freshbooks.com/auth/api/v1/users/business/{{businessId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "auth",
            "api",
            "v1",
            "users",
            "business",
            "{{businessId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        },
        {
          "key": "X-Permitted-Cross-Domain-Policies",
          "value": "none"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin-when-cross-origin"
        },
        {
          "key": "ETag",
          "value": "W/\"8cc492fe0ab5fdbd7afc687dfe9781bf\""
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "X-Request-Id",
          "value": "16b4dc9f-df36-4050-aa07-03208a1d1e74"
        },
        {
          "key": "X-Runtime",
          "value": "0.036737"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSBAUHUFQTGhEhCQ0WQg1UDl1KG39aAV0NVBZSEAEZZQAFCBdyUxAIDVl1XhUUUEJQClxZMF8XTAtWXw4IYQpMFVQyVhZLVVYNCkEUAx5UTVIZAQRRUwkPAlJQUFQJUglaDQ0YHwJJG1gGVwYHUgYPAgBVWlIEXABAag=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557346308"
        },
        {
          "key": "Retry-After",
          "value": "57"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:10:50 GMT"
        },
        {
          "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, 08 May 2019 20:10:50 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17361-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        \"id\": 1966214,\n        \"name\": \"Postman Sandbox\",\n        \"account_id\": \"wkMd2g\",\n        \"business_group\": {\n            \"id\": 4942664,\n            \"category\": \"business\",\n            \"members\": [\n                {\n                    \"id\": 6446238,\n                    \"group_id\": 4942664,\n                    \"role\": \"business_partner\",\n                    \"identity_id\": 37256,\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@gmail.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"business_id\": 1966214,\n                    \"unacknowledged_change\": false,\n                    \"active\": true\n                },\n                {\n                    \"id\": 6475824,\n                    \"group_id\": 4942664,\n                    \"role\": \"business_accountant\",\n                    \"identity_id\": 75537,\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@gmail.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"business_id\": 1966214,\n                    \"unacknowledged_change\": false,\n                    \"active\": true\n                },\n                {\n                    \"id\": 6446130,\n                    \"group_id\": 4942664,\n                    \"role\": \"owner\",\n                    \"identity_id\": 1882548,\n                    \"first_name\": \"Ryan\",\n                    \"last_name\": \"Marr\",\n                    \"email\": \"api.freshbooks@gmail.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"business_id\": 1966214,\n                    \"unacknowledged_change\": false,\n                    \"active\": true\n                }\n            ]\n        },\n        \"date_format\": \"mm/dd/yyyy\",\n        \"address\": {\n            \"id\": 2421054,\n            \"street\": null,\n            \"city\": null,\n            \"province\": null,\n            \"country\": \"Canada\",\n            \"postal_code\": null\n        },\n        \"phone_number\": null,\n        \"business_clients\": []\n    }\n}"
    }
  ]
}