pythonsolanacryptocurrency

Get Solana wallet balance with Python


Is there a way to check the balance of a Solana wallet using Python?

I tried using Solscan, but I don't understand how it works, and I can't find information about it on the Internet.


Solution

  • You could use Solana.py. If you haven't already, install it through pip install solana. Then you can run the following Python code to check the balance of your wallet, replacing YOUR_WALLET_ADDRESS with your actual wallet address.

    from solana.rpc.api import Client
    from solana.publickey import PublicKey
    client = Client("https://api.mainnet-beta.solana.com")
    public_key = PublicKey(wallet_address)
    response = client.get_balance(public_key)
    balance_lamports = response.value
    balance_sol = balance_lamports / 1e9
    print(f"Wallet balance: {balance_lamports} lamports ({balance_sol} SOL)")