google-apps-scriptgoogle-sheetshttp-status-code-404kucoin

Get sub-account's Futures account balance using KuCoin API and Google Apps Script


I am trying to get a value (USDT) from KuCoin sub-account's Futures account but having an issue. Here is the script:

function getKuCoinBalance(apiKey, secretKey, passphrase, asset) {
    var endpoint = "/api/v1/accounts";
    var baseUrl = "https://api.kucoin.com";
    var timestamp = Date.now().toString();
    var method = "GET";

    var strForSign = timestamp + method + endpoint;
    var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, strForSign, secretKey);
    var encodedSignature = Utilities.base64Encode(signature);

    var passphraseHash = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secretKey);
    var encodedPassphrase = Utilities.base64Encode(passphraseHash);
 
    var options = {
        'method' : method,
        'headers' : {
            'KC-API-SIGN' : encodedSignature,
            'KC-API-TIMESTAMP' : timestamp,
            'KC-API-KEY' : apiKey,
            'KC-API-PASSPHRASE' : encodedPassphrase,
            'KC-API-KEY-VERSION' : '2'
        }
    };

    var url = baseUrl + endpoint;
    var response = UrlFetchApp.fetch(url, options);
    Logger.log(response)
    var dataAll = JSON.parse(response.getContentText());
    var data = dataAll.data;

    for (var i = 0; i < data.length; i++) {
        if (data[i].currency == asset) {
            return data[i].balance;
        }
    }
}

I am able to get data of different types of accounts (trade, isolated, main), you can find the API information Here, but I am looking to get sub-account's Futures account details, as shown in the following image:

USDT Value

I believe I am making a mistake in using the correct URL, I reckon the correct url can be /api/v1/account-overview?currency=USDT but it throws the following error:

Exception: Request failed for https://api.kucoin.com returned code 404. Truncated server response: {"code":"404","msg":"Not Found","retry":false,"success":false} (use muteHttpExceptions option to examine full response)

Can you please guide me in the right direction? Any help would be much appreciated.


Solution

  • On looking the API Information that you provided in your question, I found this endpoint that can be used to get the balance of any sub-account. So here is the workflow:

    1. First, get the user id of your sub-account by using this request: /api/v1/sub-accounts

    2. Then use this user id in your code. So the modified script would be:

    Script Code:

     function getKuCoinSubAccountFuturesBalance(apiKey, secretKey, passphrase, currency) {
      var endpoint = "/api/v1/account-overview";
      var baseUrl = "https://api-futures.kucoin.com";
      var timestamp = Date.now().toString();
      var method = "GET";
    
      var strForSign = timestamp + method + endpoint +"?currency="+currency;
      var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, strForSign, secretKey);
      var encodedSignature = Utilities.base64Encode(signature);
    
      var passphraseHash = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, passphrase, secretKey);
      var encodedPassphrase = Utilities.base64Encode(passphraseHash);
    
      var options = {
        method: method,
        headers: {
          'KC-API-SIGN': encodedSignature,
          'KC-API-TIMESTAMP': timestamp,
          'KC-API-KEY': apiKey,
          'KC-API-PASSPHRASE': encodedPassphrase,
          'KC-API-KEY-VERSION': '2'
        },
        muteHttpExceptions: true
      };
    
      var url = baseUrl + endpoint +"?currency="+currency;
      var response = UrlFetchApp.fetch(url, options);
      var dataAll = JSON.parse(response.getContentText());
      return dataAll.data.accountEquity;
    }
    

    Note: Use Sub Account credentials (API and Secret key)

    Let me know if it helps.