You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用KuCoin API和Google Apps Script获取子账户期货账户余额

解决KuCoin子账户期货USDT余额获取问题

核心问题:错误的API端点

你使用的/api/v1/account-overview并不是子账户期货账户的有效端点,这是404错误的直接原因。KuCoin针对子账户期货账户查询有专门的API接口,需根据使用场景选择:

场景1:主账户查询子账户的期货账户余额

需使用子账户管理API中的查询子账户期货账户接口,端点为:
/api/v1/sub-accounts/{subUserId}/futures/accounts
其中{subUserId}是目标子账户的用户ID,需提前通过/api/v1/sub-accounts接口获取所有子账户列表得到。

场景2:子账户自身查询期货账户余额

直接使用期货账户API的查询期货账户概览接口,端点为:
/api/v1/futures/account-overview
若需指定资产,可添加查询参数:/api/v1/futures/account-overview?currency=USDT

关键调整步骤

  1. 替换正确的API端点
    根据你的场景选择对应端点,比如主账户查询子账户期货余额时,先获取子账户ID,再构造完整请求路径。

  2. 确保API密钥权限

    • 主账户的API密钥需开启「子账户管理」和「期货」相关权限;
    • 子账户的API密钥需开启「期货」权限。
  3. 修正签名逻辑(含查询参数时)
    当请求带查询参数时,签名字符串strForSign必须包含完整的路径(含参数部分)。例如使用/api/v1/futures/account-overview?currency=USDT时,签名字符串应为:
    timestamp + method + "/api/v1/futures/account-overview?currency=USDT"

示例脚本(子账户自身查询期货USDT余额)

function getKuCoinFuturesBalance(apiKey, secretKey, passphrase, asset) {
    var endpoint = "/api/v1/futures/account-overview?currency=" + asset;
    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'
        },
        'muteHttpExceptions': true // 开启以便查看完整错误信息
    };

    var url = baseUrl + endpoint;
    var response = UrlFetchApp.fetch(url, options);
    Logger.log(response.getContentText());
    var dataAll = JSON.parse(response.getContentText());
    
    if (dataAll.success) {
        // 返回对应资产的可用余额,可根据接口返回结构调整为权益等字段
        return dataAll.data.availableBalance;
    } else {
        throw new Error("API请求失败:" + dataAll.msg);
    }
}

额外注意事项

  • 调用子账户API时,主账户的API密钥必须拥有子账户管理权限;
  • 若仍有错误,开启muteHttpExceptions: true后查看完整响应,根据错误码(如403、401)排查权限或签名问题;
  • 你的脚本中已正确设置KC-API-KEY-VERSION: '2',这部分无需修改。

内容的提问来源于stack exchange,提问作者EagleEye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:43:23