Read your first on-chain data
Read live data from Solana by fetching the balance of any address on the public devnet.
Read your first on-chain data
Arc theme: Reading the blockchain
Web2 bridge: Solana is a massive database where every table is public, and every query is free. Now that you have your own identity on-chain, you can read your own data.
The Scenario
Every web developer has fetched data from an API. You send a request, you get back JSON, you render it on a page. Solana works the same way, except the database behind the API is public. Anyone can read any ‘account’ without an API key, without authentication, without rate limits. And the data you’re reading is the same data everyone else sees. No separate staging environment, no private endpoints. One shared, global state.
Last week you created your own identity on Solana and funded it with devnet SOL. Today, you’re going to read data from that global permissionless database, starting with your own wallet.
The Challenge
Connect to Solana’s devnet (the test network developers use for experimentation) and read the SOL balance of a public address.
You’ll install Solana’s JavaScript SDK, create a connection to devnet, and query the balance of a known address. That’s it. No wallet needed, no transactions, no tokens. Just a read operation against a public database.
What You’ll Need
- Node.js
- A terminal
- A code editor
Steps
Create a new project folder and initialize it:
mkdir day-08-read-solana
cd day-08-read-solana
npm init -y
npm install @solana/kit
Create a file called read-balance.mjs:
import { createSolanaRpc, devnet, address } from "@solana/kit";
// Connect to devnet (Solana's test network)
const rpc = createSolanaRpc(devnet("https://api.devnet.solana.com"));
// Replace this with the wallet address you created on Day 1
const targetAddress = address(
"YOUR_WALLET_ADDRESS_HERE"
);
// Query the balance, just like calling a REST API
const { value: balanceInLamports } = await rpc
.getBalance(targetAddress)
.send();
// Lamports are Solana's smallest unit. 1 SOL = 1,000,000,000 lamports.
// You learned about this on Day 3.
const balanceInSol = Number(balanceInLamports) / 1_000_000_000;
console.log(`Address: ${targetAddress}`);
console.log(`Balance: ${balanceInSol} SOL`);
Run It
node read-balance.mjs
You should see an address and a balance printed to your terminal. You just read live data from a public blockchain.
What Just Happened
You made an RPC (Remote Procedure Call) to a Solana node. That node looked up the account associated with the address you provided and returned its balance. No API key, no auth header. The data is public by default.
A few things to notice:
The balance is returned in lamports, which you already learned about on Day 3. The SDK gives you the raw lamport value and you do the conversion to SOL.
You queried your own wallet address, the same one you created and funded in the first week. If you want to try querying other addresses, the Token-2022 program address (TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb) is a good target because it always has a non-zero balance. Tomorrow you’ll build on this by fetching transaction history, so keep this project folder around.
Resources
Submission
Share a screenshot or paste of your terminal output showing the address and balance.