Skip to content

KX Connect (JS)

Node JS

This section assumes that you have node JS installed in your system. If not, install before proceeding further.

Install dependencies

There are core dependencies that must be installed to run the code provided below. There are dependencies to perform additional functions such as exporting to a CSV file. Take the time now to install what you need.

Dependencies

Code

Here is the brief outline of authenticating with KX Connect.

Snippets of code are shown as examples but use the code in the appendix for a fully working example.

Sample Code

KX Connect Document

Details of KX Connect can be found in your deployed system at http://host:port/connect/index.html

This should include dynamically generated API documentation of your analytics.

Login

All users accessing Connect API must login before sending further requests.

  • The login request is made using a POST call to https://<host>:<port>/connect/api/auth/login.

  • A client must send a JSON message of type "LoginReq" and UUID with their credentials.

Example:

{ "msg" : [{ "username" : "kxSupport", "password" : "12345678" }],
"type" : "LoginReq", "id" : "e133598e-7b9e-429a-b3e5-bda881c47024",
"date" : "Fri, 26 Jul 2019 15:59:00 GMT" }

If successfully logged in, the response will include a sessionId which will be used to make further requests.

Creating request

Below are the steps to create an authorization HTTP header string.

Build stringToSign

The elements necessary to construct the signed input string are:

JSON_Data
{
    "type" : <METHOD_REQUEST>,
    "msg" : [
        <PARAMS>
    ],
    "id" : "e133598e-7b9e-429a-b3e5-bda881c47024",
    "date" : "Fri, 26 Jul 2019 16:59:00 GMT"
}

- HTTP-Verb ("POST")
- authPath ("/connect/api/< METHOD_GROUP>/<METHOD_NAME>)
- username (same as login username)
- JSON_Data hashed using MD5
- Content type ("Application-json")
- dateStr (date in RFC1123 format "EEE, dd, MMM yyyy HH:mm:ss zzz")
- sessionId (of valid login)

Create the message signature

  • Encrypt stringToSign with HMAC-SHA1 using sessionId as the secret key. Then, encode the above with Base64.
var hmacEncStr = CryptoJS.HmacSHA1(stringToSign, sessionId);
var signature = hmacEncStr.toString(CryptoJS.enc.Base64);

Create the authorization header

  • The authorization value is the username + the last 5 characters of the session id + colon(:) + signature.

  • Set the headers and make an API call.

Additional code for CSV export

  • If exporting the API response to a CSV file, add the file in the same directory as connectSample.js.

  • Uncomment a few lines in connectSample.js.

// Include this is you wish to use the CSV export function
var ex = require("./export");

// Call below you wish to export to CSV
ex.CSV(data, METHOD_NAME);

Additional Code

Running code

From where your JS script is located.

  • To run with default parameters:

node connectSample.js

  • To run with override parameters:

node connectSample.js <host:port> <username> <password> <method>

If you've coded to print the result out to console, it should look like this:

Screenshot