Hedera
Search…
Associate tokens to an account
Associates the provided Hedera account with the provided Hedera token(s). Hedera accounts must be associated with a fungible or non-fungible token first before you can transfer tokens to that account. In the case of NON_FUNGIBLE Type, once an account is associated, it can hold any number of NFTs (serial numbers) of that token type. The Hedera account that is being associated with a token is required to sign the transaction.
    If the provided account is not found, the transaction will resolve to INVALID_ACCOUNT_ID.
    If the provided account has been deleted, the transaction will resolve to ACCOUNT_DELETED.
    If any of the provided tokens is not found, the transaction will resolve to INVALID_TOKEN_REF.
    If any of the provided tokens has been deleted, the transaction will resolve to TOKEN_WAS_DELETED.
    If an association between the provided account and any of the tokens already exists, the transaction will resolve to TOKEN_ALREADY_ASSOCIATED_TO_ACCOUNT.
    If the provided account's associations count exceeds the constraint of maximum token associations per account, the transaction will resolve to TOKENS_PER_ACCOUNT_LIMIT_EXCEEDED.
    On success, associations between the provided account and tokens are made and the account is ready to interact with the tokens.
The maximum number of Token IDs that can be associated to an account is 1,000.
Transaction Signing Requirements:
    The key of the account the token is being associated to
    Transaction fee payer account key
Constructor
Description
new TokenAssociateTransaction()
Initializes a TokenAssociateTransaction object
1
new TokenAssociateTransaction()
Copied!

Methods

V2
V1
Method
Type
Description
Requirement
setAccountId(<accountId>)
AccountId
The account to be associated with the provided tokens
Required
setTokenIds(<tokens>)
List <TokenId>
The tokens to be associated with the provided account
Required
Java
1
//Associate a token to an account
2
TokenAssociateTransaction transaction = new TokenAssociateTransaction()
3
.setAccountId(accountId)
4
.setTokenIds(Collections.singletonList(tokenId));
5
6
//Freeze the unsigned transaction, sign with the private key of the account that is being associated to a token, submit the transaction to a Hedera network
7
TransactionResponse txResponse = transaction.freezeWith(client).sign(accountKey).execute(client);
8
9
//Request the receipt of the transaction
10
TransactionReceipt receipt = txResponse.getReceipt(client);
11
12
//Get the transaction consensus status
13
Status transactionStatus = receipt.status;
14
15
System.out.println("The transaction consensus status " +transactionStatus);
16
//v2.0.4
Copied!
JavaScript
1
//Associate a token to an account and freeze the unsigned transaction for signing
2
const transaction = await new TokenAssociateTransaction()
3
.setAccountId(accountId)
4
.setTokenIds([tokenId])
5
.freezeWith(client);
6
7
//Sign with the private key of the account that is being associated to a token
8
const signTx = await transaction.sign(accountKey);
9
10
//Submit the transaction to a Hedera network
11
const txResponse = await signTx.execute(client);
12
13
//Request the receipt of the transaction
14
const receipt = await txResponse.getReceipt(client);
15
16
//Get the transaction consensus status
17
const transactionStatus = receipt.status;
18
19
console.log("The transaction consensus status " +transactionStatus.toString());
20
21
//v2.0.7
Copied!
Go
1
//Associate the token to an account and freeze the unsigned transaction for signing
2
transaction, err := hedera.NewTokenAssociateTransaction().
3
SetAccountID(accountId).
4
SetTokenIDs(tokenId).
5
FreezeWith(client)
6
7
if err != nil {
8
panic(err)
9
}
10
11
//Sign with the private key of the account that is being associated to a token, submit the transaction to a Hedera network
12
txResponse, err = transaction.Sign(accountKey).Execute(client)
13
14
if err != nil {
15
panic(err)
16
}
17
18
//Request the receipt of the transaction
19
receipt, err = txResponse.GetReceipt(client)
20
21
if err != nil {
22
panic(err)
23
}
24
25
//Get the transaction consensus status
26
status := receipt.Status
27
28
fmt.Printf("The transaction consensus status is %v\n", status)
29
30
//v2.1.0
Copied!
Method
Type
Description
Requirement
setAccountId(<accountId>)
AccountId
The account to be associated with the provided tokens
Required
addTokenId(<tokenId>)
TokenId
The tokens to be associated with the provided account
Required
Java
1
//Associate a token to an account
2
TokenAssociateTransaction transaction = new TokenAssociateTransaction()
3
.setAccountId(accountId)
4
.addTokenId(tokenId);
5
6
//Build the unsigned transaction, sign with the private key of the account that is being associated to a token, submit the transaction to a Hedera network
7
TransactionId transactionId = transaction.build(client).sign(accountKey).execute(client);
8
9
//Request the receipt of the transaction
10
TransactionReceipt getReceipt = transactionId.getReceipt(client);
11
12
//Obtain the transaction consensus status
13
Status transactionStatus = getReceipt.status;
14
15
System.out.println("The transaction consensus status " +transactionStatus);
16
//Version: 1.2.2
Copied!
JavaScript
1
//Associate a token to an account
2
const transaction = new TokenAssociateTransaction()
3
.setAccountId(accountId)
4
.addTokenId(tokenId); //Will change to addTokenId()
5
6
//Build the unsigned transaction, sign with the private key of the account that is being associated to a token, submit the transaction to a Hedera network
7
const transactionId = await transaction.build(client).sign(accountKey).execute(client);
8
9
//Request the receipt of the transaction
10
const getReceipt = await transactionId.getReceipt(client);
11
12
//Obtain the transaction consensus status
13
const transactionStatus = getReceipt.status;
14
15
console.log("The transaction consensus status " +transactionStatus);
16
//Version 1.4.2
Copied!
Last modified 6d ago
Copy link
Contents
Methods