API use in order to get wallet balance

Hi all, I’m new to the forum and I’m not too sure if I’m asking in the correct section of the Icon Community.

I’ve a silly question about how to use the API.

Is there an easier way to get the wallet balance through the API? Is there an endpoint that can provide such info making a simple request in Javascript?
Something like the do in blockchain.info does:
https://blockchain.info/rawaddr/$bitcoin_address

Many thanks in advance for your help and for making great such project.

Regards.

There’s JavaScript SDK: https://www.icondev.io/docs/javascript-sdk#section-check-the-icx-balance

You can use it with endpoints such as:
https://ctz.solidwallet.io/
https://ctz.blockmove.eu/

Hi Tomas, thank you so much for you answer. I’ve checked the documentation and I’ve tried to write the code I need but unsuccessfully. I’m super beginner in programming but unlike other api call, I’m having issues initializing the call.
I hope you can help me out. I need to get the balance of an address. Here my code:

Blockquote

    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Crypto wallet</title>
    <script src="https://cdn.jsdelivr.net/gh/icon-project/icon-sdk-js@latest/build/icon-sdk-js.web.min.js"></script>
    <script>
       function getBalanceICX(){
            const iconService = new IconService(new HttpProvider('https://bicon.net.solidwallet.io/api/v3'));
            const myAddress = 'hx902ecb51c109183ace539f247b4ea1347fbf23b5';
            const balance = await iconService.getBalance(myAddress).execute();
            console.log(balance);
        }
    </script>
</head>
<body>
    <script>
        getBalanceICX();
    </script>
</body>
</html>

Blockquote

If I add the async to the function I get errors 2. If I don’t I get errors 1 and 2.

  1. Uncaught SyntaxError: await is only valid in async function
    index.html:19
  2. Uncaught ReferenceError: getBalanceICX is not defined.

Can you please assist? I feel super dumb asking this kind of assistance but thank you very much.

Regards.

Fabio.

Try this

<html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Crypto wallet</title>
    <script src="https://cdn.jsdelivr.net/gh/icon-project/icon-sdk-js@latest/build/icon-sdk-js.web.min.js"></script>
    <script>
    const IconService = window['icon-sdk-js']
    const {HttpProvider} = IconService
    const provider = new HttpProvider('https://bicon.net.solidwallet.io/api/v3')
    const iconService = new IconService(provider)
    
       async function getBalanceICX(){
            const myAddress = 'hx902ecb51c109183ace539f247b4ea1347fbf23b5';
            const balance = await iconService.getBalance(myAddress).execute();
            console.log(balance);
        }
    </script>
</head>
<body>
    <script>
        getBalanceICX();
    </script>
</body>
</html>

Hi thanks a lot.

I’ve tried and at least I get a response but I don’t really understand it. I’m truly sorry to bother so much.

I get such response for 1 address:

  1. H {s: 1, e: 0, c: Array(1)}
  2. c: [0]
  3. e: 0
  4. s: 1

and the following for another:

  1. H {s: 1, e: 21, c: Array(2)}

  2. c: Array(2)

1. 0: 14820355
2. 1: 11352773803386
3. length: 2
4. __proto__: Array(0)
  1. e: 21
  2. s: 1

Can you help me to read the above responses? How do I convert the numbers. I think it touches the BigNumber thing but I’m not totally sure.

Thanks again for your help. Really appreciate it.

Fabio Borghini