Connecting Binance Exchange using Google Apps Script

Connecting Binance Exchange using Google Apps Script

Hi fellas!

Today I am going to show you how you can manage your crypto portfolio or trades although this article is just showing you the connection to the Binance Exchange using Google Apps Script.

So, my background..

I'm a #NoCode developer and Advance Google Apps Script Developer.

I love Crypto. I'm a big believer in Blockchain and DeFi (Decentralized Finance) Projects.

In this article, i am simply showing you how to establish a connection with Binance Exchange using Google Apps Script.

Here you can see how i am managing my trades in Google Sheet.

image.png

Let me know if anyone finds it helpful. I can able to share the sheet template.

So now my objective is to show you all how i did that using this small scripting.

Let's dive into it.

First, you will need to have an account on Binance because you will going to need an API key and Secret in the code.

How to get the API Key and Secret From Binance? binance.com/en/support/articles/360002502072

Let's go to the Google Apps Script code editor, you can either open the editor via google sheet (Going to Tools > Script Editor) or directly visiting script.google.com and creating a new standalone script.

Here how we are going to divide the files into

  1. config.gs : For storing all the configuration stuff like API keys, signature forming functions
  2. main.gs : For business logic

Lets do some coding now..

config.gs


var config = {

  "baseUrl" : "https://api.binance.com",
  "key"     : "6uGxxxxxxxxxxxxxxxxxxxxxxxx0yOPiPYS",
  "secret"  : "xKgxxxxxxxxxxxxxxxxxxxxxxxxxx2rkxHI" 

}

main.gs

//common method to return server response to the calling method
function get(query){

  var params = {
    'method' : 'get',
    'headers': { 'X-MBX-APIKEY': config.key },
    'muteHttpExceptions': true
  }
  var url  = config.baseUrl + query;  
  return UrlFetchApp.fetch(url, params).getContentText();
}

//Getting last 10 trades of BTC USDT pair
function trades(){

  var symbol        = "BTCUSDT";
  var endpoint     = "/api/v3/trades";
  //timestamp in milliseconds
  var timestamp   = Number(new Date().getTime()).toFixed(0); 
 //adding parameters to form a querystring
  var queryString = "symbol="+symbol+"&limit=10"; 
  var signature     = getSignature(queryString);
  var query           = endpoint + "?" + queryString ;  
  var res               = get(query);
  var trades         = JSON.parse(res);                          //array of trades object

 //Following logic i have used to store the trades in sheet
 //You can ignore the rest as you are good to go with above method as well

  var final_array = [];
  for(var i=0; i< trades.length; i++){
    var obj =  trades[i];
    var date_time =  Utilities.formatDate(new Date(obj.time), "IST", "dd-MMM-yyyy hh:ss a")
    final_array.push([symbol,date_time,obj.price,obj.qty,obj.isBuyerMaker,obj.isBestMatch,obj.quoteQty]);
  }
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("trades");
  sheet.getRange(2,1,final_array.length,final_array[0].length).setValues(final_array)

}

I am still working on this project on weekends, if anyone wants to join please let me know in the comment or message me on telegram t.me/sidmish

Thanks for reading and making till last.