Skip to content

Fetch Data via the Kotlin SDK

Kaptos exposes data-reading APIs across fullnode REST endpoints, indexer-backed endpoints, and Move view functions.

Network calls return Result<Ok, Err>, so you can choose explicit branching or fail-fast unwrapping.

val ledgerInfoResult = aptos.getLedgerInfo()
when (ledgerInfoResult) {
is Result.Ok -> println("Ledger version: ${ledgerInfoResult.value.ledgerVersion}")
is Result.Err -> println("Failed to read ledger info: ${ledgerInfoResult.error.message}")
}

If you want fail-fast semantics:

val ledgerInfo = aptos.getLedgerInfo().expect("Failed to fetch ledger info")

For more patterns, see Handle SDK Data Fetch Responses.

val chainId = aptos.getChainId().expect("Failed to fetch chain id")
val latestBlock = aptos.getBlockByHeight(ledgerHeight = 1, withTransactions = true).expect("Failed to fetch block info")
val address = AccountAddress.fromString("0x1")
val accountInfo = aptos.getAccountInfo(accountAddress = address).expect("Failed to fetch account info")
val modules = aptos.getAccountModules(accountAddress = address) { limit = 20 }.expect("Failed to fetch modules")
val resources = aptos.getAccountResources(accountAddress = address) { limit = 50 }.expect("Failed to fetch resources")

Indexer-backed APT balance:

val aptBalance = aptos.getAccountAPTAmount(accountAddress = address).expect("Failed to fetch APT balance")

Smart-contract view path for a specific coin type:

val aptBalanceFromView =
aptos
.getAccountCoinAmountFromSmartContract(
accountAddress = address,
coinType = MoveValue.MoveStructId("0x1::aptos_coin::AptosCoin"),
)
.expect("Failed to fetch coin balance from smart contract")

Use view<T>() for custom read-only Move calls.

val payload =
InputViewFunctionData(
function = "0x1::chain_id::get",
typeArguments = emptyList(),
functionArguments = emptyList(),
)
val viewResult =
aptos
.view<List<MoveValue.MoveUint8Type>>(payload)
.expect("Failed to execute view")
val chainIdFromView = viewResult.first().value