Skip to content

Fetch Data via SDK

You can use the Aptos client to get on-chain data using a variety of helper functions. Specifically, many of the functions listed in the reference docs here that start with get... will retrieve data from on-chain.

Here’s an example showing how to fetch common data you may need in your application:

const aptosConfig = new AptosConfig({ network: Network.DEVNET });
const aptos = new Aptos(aptosConfig);
const fund = await aptos.getAccountInfo({ accountAddress: "0x123" });
const modules = await aptos.getAccountModules({ accountAddress: "0x123" });
const tokens = await aptos.getAccountOwnedTokens({ accountAddress: "0x123" });

The Aptos client can out of the box query both network data from fullnodes and the Indexer API which contains aggregated and enriched data. If you want to use a custom query for Indexer API data, you can use aptos.queryIndexer like so:

const ledgerInfo = await aptos.queryIndexer({
query: {
query: `
query MyQuery {
ledger_infos {
chain_id
}
}
`
}
})

Some queries are intentionally broad, but this can make inferring the proper return type difficult. To accommodate that, broad requests like getAccountResources allow you to specify what the expected response type should be when you use getAccountResource.

After the Fungible Asset migration, use getAccountAPTAmount for APT balances. New accounts may not publish the legacy 0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin> resource, so reading it with getAccountResource can return “resource not found”.

const aptAmount = await aptos.getAccountAPTAmount({
accountAddress: testAccount.accountAddress,
});

For other on-chain resources, use getAccountResource with a generic type parameter:

type OnChainAccount = { sequence_number: string; authentication_key: string };
const accountResource = await aptos.getAccountResource<OnChainAccount>({
accountAddress: testAccount.accountAddress,
resourceType: "0x1::account::Account",
});
const sequenceNumber = accountResource.sequence_number;

You can call view functions which return custom data from on-chain by using aptos.view.

For example, you can look up the network you are using with the chain_id view function:

const payload: InputViewFunctionData = {
function: "0x1::chain_id::get",
};
const chainId = (await aptos.view({ payload }))[0];

Behind the scenes, some requests use the Indexer API to access data which has been processed or aggregated. That extra parsing can take a bit of time, so the data may lag slightly behind the latest ledger.

If you want to ensure that the data is fresh, you can specify the minimumLedgerVersion in any request which uses the Indexer API.

// Get the latest ledger version number
const ledgerVersion = await aptos.getLedgerInfo().ledger_version;
const tokens = await aptos.getAccountOwnedTokens({
accountAddress: alice.accountAddress,
minimumLedgerVersion: BigInt(response.version),
});

You can also ensure that your request has the data from a transaction you submitted by getting the ledger version from the transaction validation itself.

// Wait for a transaction you just submitted
const response = await aptos.waitForTransaction({
transactionHash: pendingTransaction.hash,
});
// Then look up how that transaction affected alice's account
const tokens = await aptos.getAccountOwnedTokens({
accountAddress: alice.accountAddress,
minimumLedgerVersion: BigInt(response.version),
});