Cryptopia API for Google Apps Script

Gists

By the Google’s update at June 19, 2018, finally, Utilities.computeDigest(), Utilities.computeHmacSha256Signature() and Utilities.computeHmacSignature() got to be able to use the byte arrays. By this, using only native Google Apps Script, the result can be retrieved without using jsSHA. So Cryptopia API can be created using only Google Apps Script. If this is useful for you, I’m glad.

Sample script :

When you use this, at first, please input the requiring values. Now the sample values are used.

// Values inputted by users
var key = "#####";
var secret = "#####";
var params = {"Currency": "BTC"};
var command = "GetBalance";

// main
var baseUrl = 'https://www.cryptopia.co.nz/api/';
var url = baseUrl + command;
var nonce = Math.floor(new Date().getTime() / 1000);
var reqB64 = Utilities.base64Encode(
  Utilities.computeDigest(
    Utilities.DigestAlgorithm.MD5,
    params,
    Utilities.Charset.UTF_8
  )
);
var sig = key + "POST" + encodeURIComponent(url).toLowerCase() + nonce + reqB64;
var hmacSig = Utilities.base64Encode(
  Utilities.computeHmacSignature(
    Utilities.MacAlgorithm.HMAC_SHA_256,
    Utilities.base64Decode(Utilities.base64Encode(sig)),
    Utilities.base64Decode(secret)
  )
);
var options = {
  method: 'post',
  headers: {
    "Authorization": "amx " + key + ":" + hmacSig + ":" + nonce,
    "Content-Type": "application/json; charset=utf-8",
  },
};
var res = UrlFetchApp.fetch(url, options);

 Share!