Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created June 24, 2018 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/58f092a544eb1cfbb19bc0252f36e4cb to your computer and use it in GitHub Desktop.
Save tanaikech/58f092a544eb1cfbb19bc0252f36e4cb to your computer and use it in GitHub Desktop.
Cryptopia API for Google Apps Script

Cryptopia API for Google Apps Script

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment