1. Adding required modules

- Source: https://www.npmjs.com/package/request


var request = require("request");


2. Setup the keys

- Replace with your own


var pnAPIKeys = {
    "subKey": "sub-c-09257e22-7c8a-11e7-9c85-0619f8945a4f",
    "pubKey": "pub-c-366e31ee-247d-4e04-aab1-1ae7a5b6a207",
    "secKey": "sec-c-OTYxNTQ2NjctNzAzMi00YzQxLWExMGItMGQ0YTgxM2VlMzc3"
};


3. Messages deletion


 - Using current configuration this config as it stands, it will delete all messages in the channel.
 - To delete from an offset or range, add start and/or end parameters with PN timetoken integers as values.


// History Delete URI
var historyDeleteUrl = "/v3/history/sub-key/" + pnAPIKeys.subKey + "/channel/myChannel"

// URI Params
var urlParams = {
    "uuid": "pn-86eec3a6-2aff-4faf-8722-71374c737f2a",
    "pnsdk": "DocusignExampleBasedOnPubNub-JS-Nodejs/4.14.0",
    "auth": "myAuthToken",
    "uuid": "ec4b2732-4739-494a-b83f-094ad22be275",
    "timestamp": Math.floor(new Date().getTime() / 1000)
};


4. Request Signing & Params


// Sign the request
var signature = signRequest(url, pnAPIKeys);

// Add the signature to the URL Params obj
urlParams.signature = signature;

// Create the params string from the obj
var finalParams = (objectToParams(urlParams));


5. The Request


 - *DELETE history operation must use DELETE method


request.del("http://ps.pndsn.com" + url + "?" + finalParams, function(error, response, body){
    if(error){
        console.log(error);
    }else{
        console.log(JSON.stringify(response));
    }
});


6. Utilities


function signRequest(url, outgoingParams) {

    var hmacsha256 = require("crypto-js/hmac-sha256");
    var CryptoJS = require("crypto-js");
    var signInput = pnAPIKeys.subKey + '\n' + pnAPIKeys.pubKey + '\n' + url + '\n';

    // Add a new line containing the query parameters in urlencoded alpha order
    signInput += signPamFromParams(urlParams);

    // Create the hash
    var signature = hmacsha256(signInput, outgoingParams.secKey);
    base64Signature = signature.toString(CryptoJS.enc.Base64);

    // In the signature, Replace the plusses with -, Replace the slashes with underscores
    base64Signature = base64Signature.replace(/\+/g, '-');
    base64Signature = base64Signature.replace(/\//g, '_');

    // Done!
    //console.log(base64Signature);
    return base64Signature;
}


function objectToList(o) {
    var l = [];
    Object.keys(o).forEach(function (key) {
        return l.push(key);
    });
    return l;
}

function encodeString(input) {
    return encodeURIComponent(input).replace(/[!~*'()]/g, function (x) {
        return '%' + x.charCodeAt(0).toString(16).toUpperCase();
    });
}

function objectToListSorted(o) {
    return objectToList(o).sort();
}

function objectToParams(obj) {
    var str = "";
    for (var key in obj) {
        if (str != "") {
            str += "&";
        }
        str += key + "=" + encodeURIComponent(obj[key]);
    }

    return str;
}

function signPamFromParams(params) {
    var l = objectToListSorted(params);
    return l.map(function (paramKey) {
        return paramKey + '=' + encodeString(params[paramKey]);
    }).join('&');
}

function endsWith(searchString, suffix) {
    return searchString.indexOf(suffix, this.length - suffix.length) !== -1;
}