Reputation: 98
I've tried using JavaScript code to check if an account address is associated with a Hedera token (see this thread).
Now, I want to use the mirror node to verify if an account is associated with a specific token. How do I do this?
Upvotes: 0
Views: 396
Reputation: 73
It looks like the thread you linked has been removed, so I'll do my best to answer it and hope it works.
You'll need to hit the Hedera API unless you're running your own mirror node, where it will make more sense to query the DB directly in your API.
I would not recommend using the public API, as you'll hit throttles pretty quickly for a moderately intense application. You may need to use a paid service like https://www.arkhia.io/, who do have a free tier for development.
But we will simply get the accounts tokens from the public mirror node from an arbitrary account:
axios.get('https://mainnet-public.mirrornode.hedera.com/api/v1/accounts/0.0.834926/tokens')
.then(function (response) {
let data = response.data;
console.log(data.tokens);
})
.catch(function (error) {
console.log(error);
});
This will return a response like this:
[{
automatic_association: false,
balance: 0,
created_timestamp: "1657580472.328129000",
freeze_status: "UNFROZEN",
kyc_status: "NOT_APPLICABLE",
token_id: "0.0.456858"
}, {
automatic_association: false,
balance: 1,
created_timestamp: "1654281728.041041000",
freeze_status: "NOT_APPLICABLE",
kyc_status: "NOT_APPLICABLE",
token_id: "0.0.609495"
}...]
Which you can then filter to see if the token you're after is present.
Documentation on the Hedera API (accounts specifically) can be found here: https://docs.hedera.com/hedera/sdks-and-apis/rest-api#api-v1-accounts - you're after the /api/v1/accounts/{idOrAliasOrEvmAddress}/tokens endpoint.
Upvotes: 2