How to get coin balances by wallet addresses
Prerequisites​
Before getting started, make sure you have the following ready:
- Node v.14+ or Python
- NPM/Yarn or Pip
Step 1: Setup Moralis​
First register your Moralis account and get your Moralis API Key.
Once you have your Moralis API Key, install the Moralis SDK in your project.
- npm
- yarn
- pnpm
- pip
npm install moralis
yarn add moralis
pnpm add moralis
pip install moralis
Step 2: Get Coin Balances by Wallet Addresses​
In order to get coin balances by wallet addresses, Moralis provides you a getCoinBalancesByWallets endpoint to do so.
Here you'll need two parameters: limit
and ownerAddresses
.
Once you have obtained both the limit
and ownerAddresses
, you can copy the following code:
- index.js (JavaScript)
- index.ts (TypeScript)
- index.py (Python)
const Moralis = require("moralis").default;
const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const limit = 10;
const ownerAddresses = [
"0x34d54bca84f3a0e34b351d173d5934e93a6f1bb36892832de314239c96506d75",
];
const response = await Moralis.AptosApi.wallets.getCoinBalancesByWallets({
limit: 10,
ownerAddresses,
});
console.log(response.result);
};
runApp();
import Moralis from "moralis";
const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const limit = 10;
const ownerAddresses = [
"0x34d54bca84f3a0e34b351d173d5934e93a6f1bb36892832de314239c96506d75",
];
const response = await Moralis.AptosApi.wallets.getCoinBalancesByWallets({
limit: 10,
ownerAddresses,
});
console.log(response.result);
};
runApp();
from moralis import aptos_api
api_key = "YOUR_API_KEY"
params = {
"network": "mainnet",
"limit": 10,
"owner_addresses": ["0x34d54bca84f3a0e34b351d173d5934e93a6f1bb36892832de314239c96506d75"]
}
result = aptos_api.wallets.get_coin_balances_by_wallets(
api_key=api_key,
params=params,
)
print(result)
Step 3: Run the script​
To run the script, enter the following command:
- Shell (JavaScript)
- Shell (TypeScript)
- Shell (Python)
node index.js
ts-node index.ts
python index.py
In your terminal, you should see the following JSON response:
{
"cursor": null,
"hasNextPage": false,
"result": [
{
"amount": "821827502",
"coin_type_hash": "91ceb1308a98389691e05158b07ed5f079ab78461a6bb8d5a4054b1bb5cb8bb6",
"owner_address": "0x34d54bca84f3a0e34b351d173d5934e93a6f1bb36892832de314239c96506d75",
"coin_type": "0x1::aptos_coin::AptosCoin",
"last_transaction_timestamp": "2023-01-21T05:43:47.000Z",
"last_transaction_version": "73068385"
}
]
}
Congratulations 🥳 You just got coin balances by wallet addresses with just a few lines of code using the Moralis Wallet API!
Youtube Video​
API Reference​
If you want to know more details on the endpoint and optional parameters, check out:
Support​
If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.