8 min read
Overview
Whether you are querying a token account balance or constructing a token transfer instruction, knowing how to find a user's token account address is a fundamental skill for any Solana developer. In this guide, we ensure you know the basics of Solana Token Accounts, and walk through five easy ways to fetch the address of a Solana associated token account:
Using the command line...
Using JavaScript/TypeScript
- Using Solana Kit
- Using Solana's legacy web3.js library
Using Rust
What You Will Need
- Basic understanding of Solana Fundamentals
- A Solana wallet address to query
Understanding Token Accounts on Solana
Let's take a moment to recap what a token account is and how it differs from a Solana wallet address.
When you look at your token balances in a Solana wallet like Phantom or Solflare, you are typically looking at associated token accounts and their balances:
Let's take a look at where this data comes from. First, get your wallet address - you can copy it directly from Phantom or any other wallet:
Visit Solana Explorer and paste in your wallet address. Scroll down and click on the Tokens tab, you will see a list of token accounts and their balances. This shows not only fungible tokens (currency-like tokens, for example USDC or BONK), but also non-fungible tokens (NFTs). Click on the Detailed button to get a little more information about each token account:
You'll notice the token accounts and balances in Explorer match what you see in your Solana wallet app.
You can see that each token - USDC, PyUSD, BONK, etc. - has a seperate token account address. This allows multiple transactions that involve seperate token accounts to run in parallel - I can send USDC and recieve PyUSD at the same time, since they are stored in seperate token accounts. This is one of the reasons Solana is fast.
Mint Addresses
You can see that each token account has a token mint address.
A token mint address uniquely defines a token. The creator of a token will publish the mint address on their website or social media:
USDC is created by Circle, and the USDC mint address (EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
) is published on Circle's website.
PyUSD is created by PayPal, and the PyUSD mint address (2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo
) is published on PayPal's website.
BONK is created by the community, and the BONK mint address (DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263
) is published on the @bonk_inu X profile.
Someone could make a new token that looks like USDC or PyUSD, but if the mint address is different, it is a completely different token. Many apps like Solana Explorer display a warning when showing tokens that aren't well known for this reason:
Solana Explorer also lets your search for well known tokens like USDC, BONK, and PyUSD, and see the correct mint address.
The Associated Token Account program
The token accounts shown when looking at your wallet in Explorer are mostly associated token accounts. They're token accounts that are associated with a specific wallet address and token mint address.
While storing tokens in seperate accounts is fast, it means we also need to work out how to find each token account address, given a particular wallet address.
- If I want to send some USDC to
bob.sol
, I need to know the token account wherebob.sol
stores their USDC. - If I need to know if
alice.sol
has any BONK, I need to know the token account where Alice stores her BONK.
That's where associated token accounts come in.
The Associated Token Program is a way to deterministically find where a particular wallet account stores a type of token, based on the wallet address and the token mint. For example:
- To find the associated token account for
bob.sol
's USDC, we combine his wallet address and the USDC mint address. - To find the associated token account for
alice.sol
's BONK, we need the wallet address and the BONK mint address.
These accounts are owned by the Token Program (TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
) or the newer Token Extensions Program (TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb
) and controlled by a user's wallet address. This guide will show you how to fetch the address of an associated token account for a given wallet and mint.
Remember: associated token accounts are token accounts that use addresses made from a wallet address and a token mint.
Method 1 - Command line using SPL-Token CLI
Dependency | Version |
---|---|
spl-token-cli | 5.1.0 |
Our first method to check the balance of a wallet is the Solana SPL-Token CLI. If you do not already have it installed, follow the instructions for your operating environment at spl.solana.com/token. To make sure your installation was successful, open a new terminal and type:
spl-token --version
You should see something like this:
You're ready to go!
In your terminal, fetch your token account by entering:
spl-token address \
--owner WALLET_WALLET_ADDRESS \
--token TOKEN_MINT_ADDRESS \
--verbose \
--url mainnet-beta
We simply pass the --owner
flag with the wallet address we want to query, the --token
flag with the mint address of the token we want to query, and the --verbose
flag to get a more detailed response (required for this query). The -um
flag tells the CLI to use the Solana mainnet cluster (though our search is deterministic and should not require a specified cluster, the CLI tool verifies that our mint address is actually a valid mint address on that cluster).
You should see something like this:
Great job!
Method 2 - Command line using cURL
Dependency | Version |
---|---|
curl | 8.7.1 |
cURL is a command line tool and library for transferring data with URLs. Most *nix-based systems have cURL support out of the box. Check if you have it by running the following:
curl -V
If you don't have it installed, head to curl.se to set it up.
Once you are ready to go, make a file called get-associated-token-account.bash
with the following contents:
WALLET_ADDRESS="YOUR WALLET ADDRESS"
MINT_ADDRESS="YOUR MINT ADDRESS"
curl https://api.mainnet-beta.solana.com -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0",
"id": 1,
"method": "getTokenAccountsByOwner",
"params": [
"'$WALLET_ADDRESS'",
{
"mint": "'$MINT_ADDRESS'"
},
{
"encoding": "jsonParsed"
}
]
}
' | jq;
echo "✅ Completed successfully. Check the output above."
In your terminal, run:
bash get-associated-token-account.bash
You should see something like this:
Notice the same address is returned in the result.value[0].pubkey
field 🙌. Check out our Solana docs for more information on the getTokenAccountsByOwner
method.
Method 3 - TypeScript with Solana Kit
Dependency | Version |
---|---|
node.js | 22.14.0 |
@solana/kit | 2.1.1 |
@solana-program/token | 0.5.1 |
esrun | 3.2.26 |
Create a new project director in your terminal with the following:
mkdir token-address-solana-kit && cd token-address-solana-kit
Install Solana Kit dependencies:
npm init -y
npm install --save @solana/kit @solana-program/token esrun
Then make a new file called get-associated-token-account.ts
. with the following contents:
import { address } from "@solana/kit";
import { findAssociatedTokenPda } from "@solana-program/token";
const WALLET = address("YOUR_WALLET_ADDRESS"); // e.g., dDCQNnDmNbFVi8cQhKAgXhyhXeJ625tvwsunRyRc7c8
const MINT = address("YOUR_MINT_ACCOUNT"); // e.g., EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
const TOKEN_PROGRAM_ADDRESS = address(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
);
const [associatedTokenAddress] = await findAssociatedTokenPda({
mint: MINT,
owner: WALLET,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
console.log(
"✅ Got associated token account address using Solana Kit: ",
associatedTokenAddress
);
Run your code. In your terminal type,
npx esrun address.ts
You should see the associated token address returned in your terminal.
Nice job!
Alternatively, you can alternatively use the rpc
class to send a getTokenAccountsByOwner
request to the Solana cluster. This requires a network request so is slower, but it also finds token accounts created outside of the associated token program which could be useful in some (rare) cases!
Method 4 - TypeScript with Solana web3.js
Dependency | Version |
---|---|
node.js | 22.14.0 |
@solana/web3.js | 1.98.2 |
esrun | 3.2.26 |
Create a new project directory in your terminal with the following:
mkdir token-address-legacy-web3 && cd token-address-legacy-web3
Install Solana web3.js dependencies:
npm init -y
npm install --save @solana/web3.js@1
Create a new file called get-associated-token-account-legacy-web3.js
with the following contents:
Open address.js in a code editor of choice, and on line 1, require @solana/web3.js. We will import the PublicKey class from this package.
import { PublicKey } from '@solana/web3.js';
const WALLET = new PublicKey('YOUR_WALLET_ADDRESS'); // e.g., E645TckHQnDcavVv92Etc6xSWQaq8zzPtPRGBheviRAk
const MINT = new PublicKey('YOUR_MINT_ADDRESS'); // e.g., EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
const TOKEN_PROGRAM_ID = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
const ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
const [address] = PublicKey.findProgramAddressSync(
[WALLET.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), MINT.toBuffer()],
ASSOCIATED_TOKEN_PROGRAM_ID
);
console.log(
"✅ Got associated token account address using Solana web3.js: ",
address.toBase58()
);
The findProgramAddressSync
method deterministically finds a program address given seeds and a program ID. In this case, we pass the wallet address, token program ID, and mint address as seeds and the associated token program ID as the program ID (these seeds and their order are defined by the associated token program).
Run your code. In your terminal type,
npx esrun get-associated-token-account-legacy-web3.js
You should see something like this:
Nice job!
Note, you may want to use the getAssociatedTokenAddressSync
function from @solana/spl-token
- it's a little bit easier to use. Just install the @solana/spl-token
package:
npm install --save @solana/spl-token
And then use it like this:
import { PublicKey } from "@solana/web3.js";
import { getAssociatedTokenAddressSync } from "@solana/spl-token";
const WALLET = new PublicKey("YOUR_WALLET_ADDRESS"); // e.g., E645TckHQnDcavVv92Etc6xSWQaq8zzPtPRGBheviRAk
const MINT = new PublicKey("YOUR_MINT_ADDRESS"); // e.g., EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
const address = getAssociatedTokenAddressSync(WALLET, MINT);
console.log(
"✅ Got associated token account address using Solana web3.js: ",
address.toBase58()
);
Method 5 - Rust
Dependency | Version |
---|---|
rust | 1.86.0 |
solana-sdk | 2.2.22 |
spl-associated-token-account | 7.0.0 |
If you are a Rust developer, you can also use the Solana Rust SDK. Inside of your project folder, create a new project and change into it:
cargo new get-associated-token-address-rust; cd get-associated-token-address-rust
Add the necessary dependencies:
cargo add solana-sdk spl-associated-token-account
Open src/main.rs, and on line 1, import the necessary packages:
use solana_sdk::pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address;
use std::str::FromStr;
Then, modify your main
function that will fetch your address by passing the public keys of your owner and mint into the get_associated_token_address
method:
fn main() {
let wallet = Pubkey::from_str("YOUR_WALLET_ADDRESS").unwrap();
let mint = Pubkey::from_str("YOUR_TOKEN_MINT_ADDRESS").unwrap();
let associated_token_address = get_associated_token_address(&wallet, &mint);
println!(
"✅ Got associated token account address using Rust: {}",
associated_token_address
);
}
Compile and run your code. In your terminal type,
cargo run
And you should see your same token address returned:
Nice job!
Wrap Up
Nice work! You now have five handy tools for fetching the address of a Solana Associated Token Account. If you are just getting started with your Solana journey, here are some resources that may be helpful:
We would love to hear more about what you are building. Drop us a line in Discord, or give us a follow on X/Twitter to stay up to date on all the latest information!
We ❤️ Feedback!
Let us know if you have any feedback or requests for new topics. We'd love to hear from you.