Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bittrex API for Google Apps Script

Bittrex API for Google Apps Script

This sample script is for using Bittrex API by Google Apps Script.

The following PHP script is a sample at bittrex.com.

$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

When this is converted to GAS, the script is as follows.

function main() {
  var apikey = '#####'; // Please input your key.
  var apisecret = '#####'; // Please input your secret.

  var nonce = Number(new Date().getTime() / 1000).toFixed(0);
  var uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + apikey + '&nonce=' + nonce;
  var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
  sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
  var params = {
    method: "get",
    headers: {Apisign: sign}
  }
  var res = UrlFetchApp.fetch(uri, params);
  Logger.log(res.getContentText())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment