Skip to content

Instantly share code, notes, and snippets.

@phillijw
Created February 25, 2021 14:39
Show Gist options
  • Save phillijw/cf1de972df08ae37fc9ae980c00fd730 to your computer and use it in GitHub Desktop.
Save phillijw/cf1de972df08ae37fc9ae980c00fd730 to your computer and use it in GitHub Desktop.
Coinigy v2 API Authentication for Postman
// Pre-request script for private Coinigy endpoints (requiring authentication and authorization)
// This function is used for variable replacement in URLs. It finds the curly braces
// and replaces with the corresponding variable
function interpolate (str) {
return str.replace(/{{([^}]+)}}/g, function (match, $1) {
return pm.variables.get($1);
});
}
// It is expected that you have an environment variable called apiKey and apiSecret
var apiKey = pm.environment.get("apiKey");
var apiSecret = pm.environment.get("apiSecret");
var timestamp = (((new Date()).getTime()) + "").substring(0, 10);
var method = pm.request.method.toUpperCase();
var endpoint = "/api/v2" + interpolate(request.url.split("/api/v2")[1]);
var body = (pm.request.body === undefined || pm.request.body.raw === undefined) ? "" : pm.request.body.raw;
var toSign = apiKey + timestamp + method + endpoint + body;
var signature = CryptoJS.HmacSHA256(toSign, apiSecret); //CryptoJS is included with Postman
console.log("apiKey: " + apiKey);
console.log("apiSecret: " + apiSecret);
console.log("timestamp: " + timestamp);
console.log("method: " + method);
console.log("endpoint: " + endpoint);
console.log("body: " + body);
console.log("signature: " + signature.toString());
pm.request.headers.add({key: 'X-API-KEY', value: `${apiKey}` });
pm.request.headers.add({key: 'X-API-TIMESTAMP', value: `${timestamp}` });
pm.request.headers.add({key: 'X-API-SIGN', value: `${signature.toString()}` });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment