How to get the Solana balance of an address with JavaScript

In this tutorial, we are going to learn how to get the Solana balance of any address and the balance of the connected wallet using JavaScript with the @solana/web3.js library/

In this tutorial, we are going to learn how to get the Solana balance of any address and the balance of the connected wallet using JavaScript with the @solana/web3.js library.

Here is the code to fetch the Solana balance of an address:

import { LAMPORTS_PER_SOL, PublicKey, Connection, clusterApiUrl } from '@solana/web3.js';

const fetchBalances = async () => {
  // Create a connection to the blockchain
  const connection = new Connection(clusterApiUrl('mainnet-beta'))
  
  // The address you want to get the balance of
  const address = 'GX6nkQgcXy4xDyuSH9MKjG9nq5KN5ntE3ZUUHSqUrcC8'
  const publicKey = new PublicKey(address)
  
  // Get the Solana balance
  const balance = await connection.getBalance(publicKey);

  // Return the balance in the right units
  return balance / LAMPORTS_PER_SOL;
};

Here is how that code works:

  1. Get a connection to the Solana network of your choice (the mainnet, the testnet or the devnet)
  2. Get a PublicKey object from the address you want to get the balance of
  3. Call the getBalance method and pass the PublicKey you want to get the balance of in the parameters
  4. The unit returned will be Lamports so to convert it to Solana, you need to divide the value returned by LAMPORTS_PER_SOL

The differences in React are that to get the connection to the blockchain, you can use the useConnection hook instead of creating a new object:

import { useConnection } from '@solana/wallet-adapter-react';

// ... In your component

const { connection } = useConnection()

And the other difference is that if you want to get the Solana balance of the connected wallet, you can get the PublicKey of the connected wallet using the useWallet hook:

import { useWallet } from '@solana/wallet-adapter-react';

// ... In your component

const { publicKey } = useWallet()

Once you have a connection to the blockchain and the PublicKey of the address you want to get the balance of, you can call the getBalance method which will return the Solana balance of the PublicKey you passed in parameters.

That balance returned will be in Lamports which is the smallest sub-unit of Solana that is used to represent Solana amounts on the blockchain.

That sub-unit has 9 decimals of precision so 1 SOL = 1,000,000,000 Lamports.

We use that unit so we always manipulate integers on the blockchain and never have to deal with floaters which can introduce losses of precision.

So, if you want to convert that value into the unit we're used to (Solana instead of Lamports), you have to divide it by 10 to the power of 9 or use the LAMPORTS_PER_SOL constant that the @solana/web3.js library exports:

const balanceInSol = balance / LAMPORTS_PER_SOL

Now, that function will only return the balance of the address for Solana tokens. If you want to get the balance of another token or get all the balances of all the tokens that an address holds, you have to use another function.

If you want to learn how to get the token balances of an address, check out this tutorial that we've made on this topic:

How to get the token balances of a Solana address with JavaScript
In this tutorial, we are going to learn how to get the balance of all the tokens that a Solana address holds using JavaScript and the @solana/web3.js library.

Also, feel free to check out all our articles about the Solana blockchain here:

Solana - 0xDev
Learn web3 development and blockchain development for free with our tutorials and courses. You will find all you need to learn to become a web3 developer

And that's it 🎉

Thank you for reading this article