Javascript Sample Code
This sample demonstrates how to connect to the API to GET resources using Javascript.
CryptoJS is required in order to encode the authentication :
- Encode Base 64: hmac-sha256.js
- HMAC SHA256: enc-base64-min.js
Note: For Security reason you must use HTTPS.
function getResponse() {
// Initialize variables and get values from the field
var xhr;
// build the url based on the different parameters
var urlParam = "ProductCode=TRED";
// Final url is built
var url = "https://api.unleashedsoftware.com/" + "Products?" + urlParam;
//CryptoJS is being used in javascript to generate the hash security keys
// We need to pass the url parameters as well as the key to return a SHA256
var hash = CryptoJS.HmacSHA256(urlParam, 'API-KEY-TO-USE-HERE');
// That hash generated has to be set into base64
var hash64 = CryptoJS.enc.Base64.stringify(hash);
// Simple ajax function with all the parameters
xhr =
$.ajax({
url: url,
dataType: json,
method: GET,
headers: {
'Accept': 'application/json',
'api-auth-id': 'API-ID-TO-USE-HERE',
'api-auth-signature': hash64,
'Content-Type': 'application/json'
}
}).done(function(data) {
// The request has been successful.
console.log(JSON.stringify(tmpHTML, false, 2));
}).fail(function(data, status, er) {
// The request has NOT been successful.
console.log("error: " + data + " status: " + status + " er:" + er);
console.log(JSON.stringify(data.responseJSON, false, 2));
});
}