Integration of Bitfinex API with Google Sheets using Google Apps Script

In this Post, we Integrate google sheets to Bitfinex API through the ‘authenticated’ section of the API so I can access my account information. Here is the API link.
This Script is to Get account wallet balances from Bitfinex.

function bitfinex() {
  var api_key = '<Your API key here>';
  var api_secret = '<Your API secret here>';
  var apiPath = "v2/auth/r/wallets";
  var nonce = Date.now().toString();
  var body = {};
  var rawBody = JSON.stringify(body);
  var signature = "/api/" + apiPath + nonce + rawBody;
  signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384, signature, api_secret)
    .map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
    }).join("");
  var url = "https://api.bitfinex.com/" + apiPath;
  var options = {
    method: 'POST',
    contentType: "application/json",
    headers: {
      'bfx-nonce': nonce,
      'bfx-apikey': api_key,
      'bfx-signature': signature
    },
    payload: rawBody
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
  var result = JSON.parse(response.getContentText());
  Logger.log(result)
  //var wallet_btc = result.BALANCE
  var balance = 0;
  for (var i in result) {
    if (result[i][0] == "exchange" && result[i][1] == "BTC") {
      balance = result[i][2];
      break;
    }
  }
  Logger.log(balance)
}

Leave a Reply