Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (22 sloc) 1.08 KB

Generating signed HMAC request with Javascript

Note: You should use this code as a reference but never in Production! You should never expose your API Secret on the client!

postman.clearEnvironmentVariable("x-prd-time");
postman.clearEnvironmentVariable("x-prd-key");
postman.clearEnvironmentVariable("x-prd-signature");
postman.clearEnvironmentVariable("x-prd-nonce");

api_key = 'API_KEY';
api_secret = 'API_SECRET';
api_method = request.method;
nonce = Math.random().toString(36).substr(2, 16);
current_time = Math.floor(Date.now() / 1000);


payload = '{"method":\"' + api_method + '\","x-prd-key":\"' + api_key + '\","x-prd-nonce":\"' + nonce + '\","x-prd-time":\"' + current_time + '\"}';
console.log("Using payload as " + payload);

var sha256digest = CryptoJS.HmacSHA256(payload, api_secret);
var base64sha1 = CryptoJS.enc.Base64.stringify(sha256digest);

postman.setEnvironmentVariable('x-prd-signature', base64sha1);
postman.setEnvironmentVariable('x-prd-key', api_key);
postman.setEnvironmentVariable("x-prd-time", current_time);
postman.setEnvironmentVariable("x-prd-nonce", nonce);