Search code examples
node.jskucoin

KuCoin API - TypeError: request.charAt is not a function


I'm trying to make a request to the KuCoin API to query the balance. I'm using the NodeJS API found here but I keep getting the error whenever I execute the code.

And here's the code snippet

            data().then(api => {

            const apiKey = api.api_key;
            const apiSecretKey = api.api_secret;
            const contactId = api.contact_id;
            const exchange = api.exchange;
            const passphrase = 'Passphrase';

            /** Init Configure */
            const config =
            {
                key: apiKey, // KC-API-KEY
                secret: apiSecretKey, // API-Secret
                passphrase: passphrase, // KC-API-PASSPHRASE
                environment: "live"
            }

            API.init(require(config));

            if (apiKey && exchange === "KuCoin-Futures") {

                console.log("KuCoin Balance")


                async function getBalance() {

                    try {
                        let r = await API.getAccountOverview()
                        console.log(r.data)
                      } catch(err) {
                        console.log(err)
                      } 

                }

                return getBalance()

            }
        });

I the console log I get the following error

TypeError: request.charAt is not a function
at Function.Module._resolveLookupPaths (internal/modules/cjs/loader.js:617:15)

Does anyone know how I can fix this??


Solution

  • There are couple of things which look weird in the code snippet you provided, but the sample code from the kucoin-node-api library you linked should work perfectly fine. In case you are using that one, try this snippet which should show your account info:

    const api = require('kucoin-node-api');
    
    const config = {
      apiKey: 'YOUR_KUCOIN_API_KEY',
      secretKey: 'YOUR_KUCOIN_API_SECRET',
      passphrase: 'YOUR_KUCOIN_API_PASSPHRASE',
      environment: 'live'
    };
    
    api.init(config);
    
    api.getAccounts().then((r) => {
      console.log(r.data);
    }).catch((e) => {
      console.log(e);
    });
    

    In case you're using a different library, kucoin-node-sdk maybe (judging by your code snippet), then try to configure it correctly:

    config.js file:

    module.exports = {
      baseUrl: 'https://api.kucoin.com',
      apiAuth: {
        key: 'YOUR_KUCOIN_API_KEY',
        secret: 'YOUR_KUCOIN_API_SECRET',
        passphrase: 'YOUR_KUCOIN_API_PASSPHRASE'
      },
      authVersion: 2
    }
    

    and your main.js (or whatever the name is):

    const API = require('kucoin-node-sdk');
    
    API.init(require('./config'));
    
    const main = async () => {
      const getTimestampRl = await API.rest.Others.getTimestamp();
      console.log(getTimestampRl.data);
    };
    
    main();
    

    The code above will show you KuCoin server timestamp only, but should be enough to keep going.

    Good luck with trading!