<p><code>getBalance</code> function returns cbor encoded hex string, so you should actually decode it.</p>
<p>Easiest way to do it is by using <a href="https://www.npmjs.com/package/@emurgo/cardano-serialization-lib-browser" rel="noreferrer">Cardano Serialization Lib</a>.</p>
<p>After you've downloaded the library you can decoded it by doing this:</p>
<pre><code>import * as wasm from '@emurgo/cardano-serialization-lib-browser';

window.cardano.getBalance().then(res =&gt; {
   const balance = wasm.Value.from_bytes(Buffer.from(res, 'hex'));
   const lovelaces = balance.coin().to_str();

   console.log(lovelaces);
})
</code></pre>
<p><strong>EDIT:</strong></p>
<p>Thanks to <a href="https://cardano.stackexchange.com/users/4593/hernan-rajchert">Hernan Rajchert</a> for explaining on how to get lovelaces from Values object.</p>

CC BY-SA 4.0
2021-10-13T15:43:15.360Z
thanks! but this code returns this object "{ptr: 1180552}", what to do with that?
Daniel Diaz
CC BY-SA 4.0
2021-10-14T09:50:11.967Z
Yeah I got the same response so I'm not too sure about it. I'd recommend you rather building your transactions using TransactionBuilder, because it offers you nice way to specify change address and etc.
pyropy
CC BY-SA 4.0
2021-11-03T20:30:06.607Z
@DanielDiaz, that is a pointer to the WASM object, if you need to get the lovelace value as string you can do `balance.coin().to_str()`
Hernan Rajchert
CC BY-SA 4.0
2021-11-06T13:40:58.147Z
Yes as DanielDias suggested thats the way to go I got my balance in lovelaces
Alicia Basilio
CC BY-SA 4.0
2021-11-06T17:58:45.903Z
I'll update my answer and tag @HernanRajchert
pyropy
CC BY-SA 4.0
2021-12-10T16:50:21.647Z
Why do I get "Uncaught ReferenceError: Buffer is not defined"?
Daniel Cukier
CC BY-SA 4.0
2021-12-10T16:55:24.640Z
//another way to convert hex string to bytes without Buffer
function hexToBytes(hex) {
  for (var bytes = [], c = 0; c < hex.length; c += 2)
      bytes.push(parseInt(hex.substr(c, 2), 16));
  return bytes;
}
Daniel Cukier
CC BY-SA 4.0
2022-03-31T20:23:54.697Z
@HernanRajchert or anybody else know how I can view the contents of these WASM objects that you're referring to?
B Snow
CC BY-SA 4.0
2021-10-13T14:35:46.497Z
2021-11-06T18:01:43.307Z
2021-11-06T18:01:43.307Z
pyropy
<p>You have to use the <code>cbor</code> npm package to extract the balance value:</p>
<pre><code>import cbor from 'cbor';

const getAdaBalance = (string) =&gt; {
  const decoded = cbor.decode(string)
  return ((typeof decoded === 'number') ? decoded : decoded[0]) / 1_000_000;
}

let myBalance = getAdaBalance(await window.cardano.getBalance());
</code></pre>

CC BY-SA 4.0
2021-12-10T16:48:48.290Z
2021-12-10T16:48:48.290Z
Daniel Cukier
<p>Hi i just recently started working with Nami Wallet to be implementes in a web app, but when im trying to get my wallet balance:</p>
<pre><code>window.cardano.getBalance().then(res =&gt; console.log(res))
</code></pre>
<p>its returning an string.</p>
<blockquote>
<p>&quot;1a3b983883&quot;</p>
</blockquote>
<p>would love to know what that is and if i can convert that to an actual number.</p>
<p>Picture:</p>
<p><a href="https://i.sstatic.net/KQlBe.png" rel="noreferrer"><img src="https://i.sstatic.net/KQlBe.png" alt="getBalance() response" /></a></p>

<wallet><ada><address><api>
Nami wallet showing getBalance returning a NaN
# Nami wallet showing getBalance returning a NaN

(Asked by: username_0 on 2021-10-13)

Hi i just recently started working with Nami Wallet to be implementes in a web app, but when im trying to get my wallet balance:
window.cardano.getBalance().then(res => console.log(res))

its returning an string.

"1a3b983883"

would love to know what that is and if i can convert that to an actual number.
Picture:

**Tags:** wallet, ada, address, api

---

## 2 answers

### Answer by username_1 on 2021-10-13. Score: 7

getBalance function returns cbor encoded hex string, so you should actually decode it.
Easiest way to do it is by using Cardano Serialization Lib.
After you've downloaded the library you can decoded it by doing this:
import * as wasm from '@emurgo/cardano-serialization-lib-browser';

window.cardano.getBalance().then(res => {
   const balance = wasm.Value.from_bytes(Buffer.from(res, 'hex'));
   const lovelaces = balance.coin().to_str();

   console.log(lovelaces);
})

EDIT:
Thanks to Hernan Rajchert for explaining on how to get lovelaces from Values object.

> **Comments:**
> **username_0**: thanks! but this code returns this object "{ptr: 1180552}", what to do with that?  
> **username_1**: Yeah I got the same response so I'm not too sure about it. I'd recommend you rather building your transactions using TransactionBuilder, because it offers you nice way to specify change address and etc.  
> **username_2**: @DanielDiaz, that is a pointer to the WASM object, if you need to get the lovelace value as string you can do `balance.coin().to_str()`  
> **username_3**: Yes as DanielDias suggested thats the way to go I got my balance in lovelaces  
> **username_1**: I'll update my answer and tag @HernanRajchert  
> **username_4**: Why do I get "Uncaught ReferenceError: Buffer is not defined"?  
> **username_4**: //another way to convert hex string to bytes without Buffer function hexToBytes(hex) {   for (var bytes = [], c = 0; c < hex.length; c += 2)       bytes.push(parseInt(hex.substr(c, 2), 16));   return bytes; }  
> **username_5**: @HernanRajchert or anybody else know how I can view the contents of these WASM objects that you're referring to?  

---

### Answer by username_4 on 2021-12-10. Score: -1

You have to use the cbor npm package to extract the balance value:
import cbor from 'cbor';

const getAdaBalance = (string) => {
  const decoded = cbor.decode(string)
  return ((typeof decoded === 'number') ? decoded : decoded[0]) / 1_000_000;
}

let myBalance = getAdaBalance(await window.cardano.getBalance());


---

