'Can't create KuCoin order with Google App Scripts

I can get account details so my authentication appears correct but in trying to modify that code to create an order it returns a code 401 "msg":"Invalid KC-API-SIGN". The modification involved adding in the method and payload and changing endpoint (/api/vi/accounts) to endpoint2 (/api/v1/orders)

function kucoinTest5()
{

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("xxxxx");
  var key = sheet.getRange("xx").getValue()
  var secret = sheet.getRange("xx").getValue();
  var passphrase = sheet.getRange("xx").getValue();
    
  var host = 'https://openapi-sandbox.kucoin.com';
  //var endpoint ='/api/v1/accounts';
  var endpoint2 ='/api/v1/orders';

  var timestamp = ''+ new Date().getTime();
  var strForSign = timestamp + 'GET' + endpoint2;

  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, strForSign, secret);
  var encodedPass = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secret);

  var url= host + endpoint2
  var requestOptions = {

  'method': "POST",
  
  'headers': {
      'KC-API-KEY': key,
      'KC-API-TIMESTAMP': timestamp,
      'KC-API-SIGN': Utilities.base64Encode(signature),
      'KC-API-KEY-VERSION': '2',
      'KC-API-PASSPHRASE': Utilities.base64Encode(encodedPass),   
    },

  'payload': {
    'clientOid': 'test1',
    'side': 'buy',
    'symbol': 'BTC-USDT',
    'type': 'market',
    'tradeType': 'TRADE',
    'funds': 100
  },    
  

  muteHTTPExceptions: true,

};

var httpRequest= UrlFetchApp.fetch(url, requestOptions);
//var getContext= httpRequest.getContentText();
Logger.log(httpRequest);
}


Solution 1:[1]

Solved above problem here is the code to post a buy order on KuCoin:

  function kuCoinTest5()
  {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("xxxx");
  var key = sheet.getRange("xx").getValue()
  var secret = sheet.getRange("xx").getValue();
  var passphrase = sheet.getRange("xx").getValue();

  var payload = {
    'clientOid':"UUID",
    'side':"buy",
    'symbol':"BTC-USDT",
    'type':"market",
    'tradeType':"TRADE",
    'funds':"100"
  };

  var data = JSON.stringify(payload);
 //Logger.log(data);

  var host = 'https://openapi-sandbox.kucoin.com';

  var timeStamp = ''+ new Date().getTime();
  //var nowStr = "" + nowDate;

  var endpoint ='/api/v1/accounts';
  var endpoint2 ='/api/v1/orders';

  var strForSign = timeStamp + "POST" + endpoint2 + data;
  //Logger.log(strForSign);


  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, strForSign, secret);
  var encodedPass = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secret);

  var url= host + endpoint2;
 //Logger.log(url);  


var options = {
"method":"POST",
  
'headers' : {
'KC-API-KEY': key,
'KC-API-TIMESTAMP': timeStamp,
'KC-API-SIGN': Utilities.base64Encode(signature),
'KC-API-KEY-VERSION': '2',
'KC-API-PASSPHRASE': Utilities.base64Encode(encodedPass)

},

"contentType":"application/json",
"payload":data,

//'payload' : {'clientOid':"45234524625",
    //'side':"buy",
    //'symbol':"BTC-USDT",
    //'type':"market",
    //'tradeType':"TRADE",
    //'funds':"100"},


"muteHttpExceptions":true,
}


 var result = UrlFetchApp.getRequest(url, options);
        Logger.log(result) // a better way to debug
        var result = UrlFetchApp.fetch(url, options); // works perfectly in my case
        Logger.log(result)



}

Solution 2:[2]

I had the same problem with a GET request, and finally solved thanks to the above code. Here is my code:

function KuCoinRequest(){

 var key ='xx'
 var secret = 'xx'
 var passphrase = 'xx'
 var url = "https://api-futures.kucoin.com/"; //endpoint
 var timestamp = '' + Number(new Date().getTime()).toFixed(0);

 var command = "GET";
 var endpoint = "api/v1/fills"
 var str_to_sign = timestamp + command +"/" + endpoint;
 
 var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, str_to_sign, secret)
 var encodedPass = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secret);
    
 var params = {
  'method': "GET",
  'headers' : {
  'KC-API-SIGN': Utilities.base64Encode(signature),
  'KC-API-KEY': key,
  'KC-API-TIMESTAMP': timestamp,         
  'KC-API-PASSPHRASE': Utilities.base64Encode(encodedPass),     
  'KC-API-KEY-VERSION': '2',     
  'muteHttpExceptions': true       
  }
 };         

 query = url + endpoint;      
 var data = UrlFetchApp.fetch(query, params);
 Logger.log(data.getContentText());   
 printJsonKucoin(data, endpoint); 
 return data;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user10490190
Solution 2