- Kit
- Web3.js v3
- Rust
- Python
- Go
- Web3.js (legacy)
Kit
import { createSolanaRpc, type Address } from "@solana/kit";
const rpc = createSolanaRpc("http://localhost:8899");
const address = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address;
const { value } = await rpc.getBalance(address).send();
console.log(`Balance: ${Number(value) / 1_000_000_000} SOL`);
Output from a local Surfpool network
Output
Balance: 0.00114144 SOL
Web3.js v3
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const address = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
const balance = await connection.getBalance(address);
console.log(`Balance: ${Number(balance) / LAMPORTS_PER_SOL} SOL`);
Output from a local Surfpool network
Output
Balance: 0.00114144 SOL
Rust
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_commitment_config::CommitmentConfig;
use solana_sdk::{native_token::LAMPORTS_PER_SOL, pubkey};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = RpcClient::new_with_commitment(
String::from("http://localhost:8899"),
CommitmentConfig::confirmed(),
);
let address = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
let balance = client.get_balance(&address).await?;
println!("Balance: {} SOL", balance as f64 / LAMPORTS_PER_SOL as f64);
Ok(())
}
Output from a local Surfpool network
Output
Balance: 0.00114144 SOL
Python
import asyncio
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Confirmed
from solders.pubkey import Pubkey
async def main():
async with AsyncClient("http://localhost:8899", commitment=Confirmed) as client:
address = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
balance = await client.get_balance(address)
print(f"Balance: {balance.value / 1_000_000_000} SOL")
asyncio.run(main())
Output from a local Surfpool network
Output
Balance: 0.00114144 SOL
Go
package main
import (
"context"
"fmt"
"github.com/solana-foundation/solana-go/v2"
"github.com/solana-foundation/solana-go/v2/rpc"
)
func main() {
client := rpc.New("http://localhost:8899")
address := solana.MustPublicKeyFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
balance, err := client.GetBalance(context.Background(), address, rpc.CommitmentConfirmed)
if err != nil {
panic(err)
}
fmt.Printf("Balance: %v SOL\n", float64(balance.Value)/float64(solana.LAMPORTS_PER_SOL))
}
Output from a local Surfpool network
Output
Balance: 0.00114144 SOL
Web3.js (legacy)
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide to keep the class-based API.To run this code, install 1.x with npm install @solana/web3.js@1. A plain npm install @solana/web3.js installs v3, which this code doesn’t run on.import {
Connection,
LAMPORTS_PER_SOL,
PublicKey,
} from "@solana/web3.js";
const connection = new Connection("http://localhost:8899", "confirmed");
const address = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
const balance = await connection.getBalance(address);
console.log(`Balance: ${balance / LAMPORTS_PER_SOL} SOL`);
Output from a local Surfpool network
Output
Balance: 0.00114144 SOL