Fetch Data via SDK
You can use the Aptos client to retrieve on-chain data using a variety of helper methods. The examples below assume you have already created an Aptos client instance:
use aptos_sdk::{Aptos, AptosConfig};
let aptos = Aptos::new(AptosConfig::testnet())?;Account Balances
Section titled “Account Balances”Fetch the APT balance for any account address. The balance is returned in octas (1 APT = 100,000,000 octas).
let address = "0x1".parse()?;let balance = aptos.get_balance(address).await?;println!("Balance: {} octas", balance);Account Information
Section titled “Account Information”Check whether an account exists on-chain and retrieve its current sequence number. The sequence number is needed when building transactions manually.
let address = "0x1".parse()?;
// Check if an account existslet exists = aptos.account_exists(address).await?;println!("Account exists: {}", exists);
// Get the current sequence numberlet sequence_number = aptos.get_sequence_number(address).await?;println!("Sequence number: {}", sequence_number);Ledger Info
Section titled “Ledger Info”Retrieve the current state of the blockchain, including the latest ledger version, block height, and epoch.
let info = aptos.ledger_info().await?;println!("Ledger version: {}", info.ledger_version);println!("Block height: {}", info.block_height);println!("Epoch: {}", info.epoch);View Functions (JSON)
Section titled “View Functions (JSON)”View functions let you call on-chain Move functions that are marked as #[view] without submitting a transaction. The JSON variant returns results as serde_json::Value, which is useful for quick lookups and dynamic data.
No type arguments
Section titled “No type arguments”Call a simple view function with no type arguments or parameters:
let result = aptos .view("0x1::timestamp::now_seconds", vec![], vec![]) .await?;println!("Current timestamp: {:?}", result);With type arguments
Section titled “With type arguments”To query a specific coin balance, pass the coin type as a type argument and the account address as a function argument:
let result = aptos .view( "0x1::coin::balance", vec!["0x1::aptos_coin::AptosCoin"], vec![serde_json::json!("0x1")], ) .await?;println!("Coin balance: {:?}", result);View Functions (BCS)
Section titled “View Functions (BCS)”The view_bcs method returns results as BCS-encoded bytes, which you can deserialize into strongly typed Rust structs. This is more efficient and type-safe than the JSON variant.
let timestamp: Vec<u64> = aptos .view_bcs("0x1::timestamp::now_seconds", vec![], vec![]) .await?;println!("Current timestamp: {} seconds", timestamp[0]);Gas Estimation
Section titled “Gas Estimation”Estimate the gas cost of a transaction before submitting it. This is helpful for displaying fee estimates to users or validating that a transaction will not exceed a gas budget.
use aptos_sdk::types::InputEntryFunctionData;
let payload = InputEntryFunctionData { function: "0x1::aptos_account::transfer".to_string(), type_arguments: vec![], arguments: vec![ serde_json::json!("0xrecipient_address"), serde_json::json!("1000"), ],};
let gas_estimate = aptos.estimate_gas(payload).await?;println!("Estimated gas units: {}", gas_estimate);Fullnode REST Client
Section titled “Fullnode REST Client”For advanced queries not covered by the high-level Aptos methods, you can access the fullnode REST client directly. This gives you access to the complete set of fullnode API endpoints.
let address = "0x1".parse()?;
// Retrieve all modules published under an accountlet modules = aptos.fullnode().get_account_modules(address).await?;for module in &modules { println!("Module: {}", module.abi.as_ref().unwrap().name);}Indexer Queries
Section titled “Indexer Queries”The SDK includes an indexer client for querying aggregated and enriched on-chain data. The indexer provides access to data types that are not available through the fullnode API alone, such as token ownership history, event aggregations, and account transaction summaries.
let indexer = aptos.indexer();The indexer client provides access to:
- Token data — ownership, metadata, and transfer history for digital assets.
- Coin balances — current and historical balances across all coin types.
- Account transactions — enriched transaction data with decoded payloads.
- Event data — aggregated and filterable event streams.
- Collection data — NFT collection metadata and statistics.