-1

I am attempting to run a function from Google Sheets to get the list of closed PNL orders from the Bybit Exchange.

I keep getting a signature error. Not sure how to resolve this.

function bybitpnl()
  {

  var timeStamp = ''+ new Date().getTime();

  var host = 'https://api.bybit.com';
  var endpoint ='/contract/v3/private/position/closed-pnl?symbol=ETHUSDT';

  var key = 'xxxxxxxxxxx';
  var secret = 'xxxxxxxxxxxx';

  var recv_widow = '5000';
  var url= host + endpoint;
  var strForSign = timeStamp.toString()+key+recv_widow.toString()+url.toString();

  Logger.log(strForSign);

  var signature = Utilities.computeHmacSha256Signature(strForSign,secret);

  var options = {
    "method":"GET",
      'headers' : {
      'X-BAPI-SIGN-TYPE': '2',
      'X-BAPI-API-KEY': key,
      'X-BAPI-TIMESTAMP': timeStamp,
      //'X-BAPI-SIGN': Utilities.base64Encode(signature),
      'X-BAPI-SIGN': signature,
      'X-BAPI-RECV-WINDOW': recv_widow

      },

    "contentType":"application/json",

  };

  var result = UrlFetchApp.getRequest(url, options);
  Logger.log(result)
  var result = UrlFetchApp.fetch(url, options);
  Logger.log(result)
};
player0
  • 124,011
  • 12
  • 67
  • 124

1 Answers1

1

I had to convert the signature to Hex, so I ran it through this function I found:

function DecToHex(value) {    
  var result = "";
  while( value != 0 ) {
    var temp = value % 16;
    Logger.log(temp);
    var hex = temp < 10 ? String.fromCharCode(temp+48) : String.fromCharCode(temp+55);
    result = hex.concat(result);
    value = Math.floor(value/16);
  }
 
  result = result;
  return result;
} 
  • Hi, @Dustin Parris, Welcome! Thanks to share your solution with the community. Please, could you check you question as resolved? Congrats! – Luciana Oliveira Aug 15 '22 at 12:38