This tutorial will walk you through a TransferTransaction and show you how to transfer HBAR between accounts, initialize a Hedera client, securely sign and submit a transfer transaction, and verify the transaction was successful using the Hedera Mirror Node.
What you will accomplish
By the end of this tutorial, you will be able to:
Create and send a transfer transaction.
Send an account balance query.
Query the transaction via Mirror Node API.
View your transaction on a Mirror Node Explorer.
Prerequisites
Before you begin, you should have completed the following tutorials:
Step 1: Navigate to the transfer example in the project directory
From the root directory of the project, cd (change directories) into the transfer transaction example script.
cdtransfer
If you completed a previous example in the series, you can go back to the root directory and cd into this example.
cd../transfer
If you want to get back to the root directory, you can CD out from any directory with this command
cd../
You can follow along through the code walkthrough or skip ahead to execute the program here.
Step 2: Guided Code Walkthrough
Open the transfer HBAR script (e.g., /transfer/script-transfer-hbar...) in a code editor like VS Code, IntelliJ, or a Gitpod instance. The imports at the top include modules for interacting with the Hedera network via the SDK. The @hashgraph/sdk enables account management and transactions like creating a token while the dotenv package loads environment variables from the .env file, such as the operator account ID, private key, and name variables.
import { Client, PrivateKey, AccountId, TransferTransaction, Hbar, HbarUnit, AccountBalanceQuery,} from'@hashgraph/sdk';import dotenv from'dotenv';import { convertTransactionIdForMirrorNodeApi, createLogger,} from'../util/util.js';constlogger=awaitcreateLogger({ scriptId:'transferHbar', scriptCategory:'task',});let client;asyncfunctionscriptTransferHbar() {logger.logStart('Hello Future World - Transfer Hbar - start');// Read in environment variables from `.env` file in parent directorydotenv.config({ path:'../.env' });logger.log('Read .env file');// Initialise the operator accountconstoperatorIdStr=process.env.OPERATOR_ACCOUNT_ID;constoperatorKeyStr=process.env.OPERATOR_ACCOUNT_PRIVATE_KEY;if (!operatorIdStr ||!operatorKeyStr) {thrownewError('Must set YOUR_NAME, OPERATOR_ACCOUNT_ID, OPERATOR_ACCOUNT_PRIVATE_KEY'); }constoperatorId=AccountId.fromString(operatorIdStr);constoperatorKey=PrivateKey.fromStringECDSA(operatorKeyStr);logger.log('Using account:', operatorIdStr);}
script-transfer-hbar.go
packagemainimport ("encoding/json""fmt""log""math""os""sort""strings""time""github.com/hashgraph/hedera-sdk-go/v2""github.com/imroc/req/v3""github.com/joho/godotenv")typeTransferMNAPIResponsestruct { Account string`json:"account"` Amount int64`json:"amount"`}typeTransferTransactionMNAPIResponsestruct { Transactions []struct { Transfers []TransferMNAPIResponse`json:"transfers"` } `json:"transactions"`}funcmain() { fmt.Println("🏁 Hello Future World - Transfer Hbar - start")// Load environment variables from .env file err := godotenv.Load("../.env")if err !=nil { log.Fatal("Error loading .env file") }// Initialize the operator account operatorIdStr := os.Getenv("OPERATOR_ACCOUNT_ID") operatorKeyStr := os.Getenv("OPERATOR_ACCOUNT_PRIVATE_KEY")if operatorIdStr ==""|| operatorKeyStr =="" { log.Fatal("Must set OPERATOR_ACCOUNT_ID, OPERATOR_ACCOUNT_PRIVATE_KEY") } operatorId, _ := hedera.AccountIDFromString(operatorIdStr)// Necessary because Go SDK v2.37.0 does not handle the `0x` prefix automatically// Ref: https://github.com/hashgraph/hedera-sdk-go/issues/1057 operatorKeyStr = strings.TrimPrefix(operatorKeyStr, "0x") operatorKey, _ := hedera.PrivateKeyFromStringECDSA(operatorKeyStr) fmt.Printf("Using account: %s\n", operatorId) fmt.Printf("Using operatorKey: %s\n", operatorKeyStr)
Create a Hedera Testnet Client
To set up your Hedera Testnet client, create the client and configure the operator using your testnet account ID and private key. The operator account covers transaction and query fees in HBAR, with all transactions requiring a signature from the operator's private key for authorization.
ScriptTransferHbar.java
//Create your Hedera Testnet clientClient client =Client.forTestnet();//Set your account as the client's operatorclient.setOperator(operatorId, operatorKey);//Set the default maximum transaction fee (in Hbar)client.setDefaultMaxTransactionFee(newHbar(100));//Set the maximum payment for queries (in Hbar)client.setDefaultMaxQueryPayment(newHbar(50));
script-transfer-hbar.js
// The client operator ID and key is the account that will be automatically set to pay for the transaction fees for each transactionclient =Client.forTestnet().setOperator(operatorId, operatorKey);//Set the default maximum transaction fee (in Hbar)client.setDefaultMaxTransactionFee(newHbar(100));//Set the maximum payment for queries (in Hbar)client.setDefaultMaxQueryPayment(newHbar(50));
//Create your testnet clientclient := hedera.ClientForTestnet()client.SetOperator(myAccountId, myPrivateKey)// Set default max transaction feeclient.SetDefaultMaxTransactionFee(hedera.HbarFrom(100, hedera.HbarUnits.Hbar))// Set max query paymentclient.SetDefaultMaxQueryPayment(hedera.HbarFrom(50, hedera.HbarUnits.Hbar))
To avoid encountering the INSUFFICIENT_TX_FEE error while executing transactions, you can also specify the maximum transaction fee limit through the .setDefaultMaxTransactionFee() method and the maximum query payment through the .setDefaultMaxQueryPayment() method to control costs, ensuring your client operates within your desired financial limits on the Hedera Testnet.
🚨 How to resolve the INSUFFICIENT_TX_FEE error
To resolve this error, you must adjust the max transaction fee to a higher value suitable for your needs.
Here is a simple example addition to your code:
Copy
constmaxTransactionFee=newHbar(XX); // replace XX with desired fee in Hbar
In this example, you can set maxTransactionFee to any value greater than 5 HBAR (or 500,000,000 tinybars) to avoid the "INSUFFICIENT_TX_FEE" error for transactions greater than 5 HBAR. Please replace XX with the desired value.
To implement this new max transaction fee, you use the setDefaultMaxTransactionFee() method as shown below:
Create and initialize a transfer transaction (TransaferTransaction) by specifying the sender account, receiver account, and transfer amount. Refer to the transaction and query fees table for the base transaction fee. In the code snippet below, you use the new testnet account you created in the Get Your Testnet Account guide to debit from your operator account (-3 HBAR) and credit accounts 0.0.200(1 HBAR) and 0.0.201 (2 HBAR).
ScriptTransferHbar.java
AccountId recipientAccount1 =AccountId.fromString("0.0.200");AccountId recipientAccount2 =AccountId.fromString("0.0.201");TransferTransaction transferTx =newTransferTransaction().setTransactionMemo("Hello Future World transfer - xyz")// Debit 3 HBAR from the operator account (sender).addHbarTransfer(operatorId,Hbar.from(-3,HbarUnit.HBAR))// Credit 1 HBAR to account 0.0.200 (1st recipient).addHbarTransfer(recipientAccount1,Hbar.from(1,HbarUnit.HBAR))// Credit 2 HBAR to account 0.0.201 (2nd recipient).addHbarTransfer(recipientAccount2,Hbar.from(2,HbarUnit.HBAR))// Freeze the transaction to prepare for signing.freezeWith(client);// Get the transaction ID for the transfer transactionTransactionId transferTxId =transferTx.getTransactionId();System.out.println("The transfer transaction ID: "+transferTxId.toString());
script-transfer-hbar.js
// Create the transfer transactionconsttransferTx=awaitnewTransferTransaction().setTransactionMemo(`Hello Future World transfer - ${logger.version}`)// Debit 3 HBAR from the operator account (sender).addHbarTransfer(operatorId,newHbar(-3,HbarUnit.Hbar))// Credit 1 HBAR to account 0.0.200 (1st recipient).addHbarTransfer('0.0.200',newHbar(1,HbarUnit.Hbar))// Credit 2 HBAR to account 0.0.201 (2nd recipient).addHbarTransfer('0.0.201',newHbar(2,HbarUnit.Hbar))// Freeze the transaction to prepare for signing.freezeWith(client);// Get the transaction ID for the transfer transactionconsttransferTxId=transferTx.transactionId;logger.log('The transfer transaction ID:',transferTxId.toString());
script-transfer-hbar.go
recipientAccount1, _ := hedera.AccountIDFromString("0.0.200")recipientAccount2, _ := hedera.AccountIDFromString("0.0.201")transferTx, _ := hedera.NewTransferTransaction().SetTransactionMemo(fmt.Sprintf("Hello Future World transfer - xyz")).// Debit 3 HBAR from the operator account (sender)AddHbarTransfer(operatorId, hedera.HbarFrom(-3, hedera.HbarUnits.Hbar)).// Credit 1 HBAR to account 0.0.200 (1st recipient)AddHbarTransfer(recipientAccount1, hedera.HbarFrom(1, hedera.HbarUnits.Hbar)).// Credit 2 HBAR to account 0.0.201 (2nd recipient)AddHbarTransfer(recipientAccount2, hedera.HbarFrom(2, hedera.HbarUnits.Hbar)).// Freeze the transaction to prepare for signingFreezeWith(client)// Get the transaction ID for the transfer transactiontransferTxId := transferTx.GetTransactionID()fmt.Printf("The transfer transaction ID: %s\n", transferTxId.String())
Sign the Transfer Transaction
The transaction must be signed using the private key of the sender's (operator) account. This ensures that the sender authorizes the transfer. Since you are transferring from the account associated with the client, you do not need to sign the transaction explicitly, as the operator account (the account transferring the HBAR) signs all transactions to authorize the transaction fee payment.
ScriptTransferHbar.java
// Sign with the operator keyTransferTransaction transferTxSigned =transferTx.sign(operatorKey);// Submit the transaction to the Hedera TestnetTransactionResponse transferTxSubmitted =transferTxSigned.execute(client);
script-transfer-hbar.js
// Sign with the operator keyconsttransferTxSigned=awaittransferTx.sign(operatorKey);// Submit the transfer transaction to the Hedera networkconsttransferTxSubmitted=awaittransferTxSigned.execute(client);
script-transfer-hbar.go
// Sign with the operator keytransferTxSigned := transferTx.Sign(operatorKey)// Submit the transaction to the Hedera TestnettransferTxSubmitted, err := transferTxSigned.Execute(client)if err !=nil {log.Fatalf("Error executing TransferTransaction: %v\n", err)}
To verify that the transaction has reached consensus on the network, submit a request for the transaction receipt. The request returns the transaction's status to your console. If the console returns a SUCCESS status, the transaction was successfully processed into the consensus state.
script-transfer-hbar.js
// Get the transfer transaction receipt consttransferTxReceipt=awaittransferTxSubmitted.getReceipt(client);// Get the transaction consensus statusconsttransactionStatus=transferTxReceipt.status;// Log the transaction statuslogger.log('The transfer transaction status is:',transactionStatus.toString(),);
ScriptTransferHbar.java
// Get the transfer transaction receiptTransactionReceipt transferTxReceipt =transferTxSubmitted.getReceipt(client);// Get the transaction consensus statusStatus transactionStatus =transferTxReceipt.status;// Print the transaction statusSystem.out.println("The transfer transaction status is: "+transactionStatus.toString());
script-transfer-hbar.go
// Get the transfer transaction receipttransferTxReceipt, err := transferTxSubmitted.GetReceipt(client)if err !=nil { log.Fatalf("Error getting receipt for TransferTransaction: %v\n", err)}// Get the transaction consensus statustransactionStatus := transferTxReceipt.Status// Print the transaction statusfmt.Printf("The transfer transaction status is: %s\n", transactionStatus.String())
Query the Account Balance
Verify the account balance was updated for the account (0.0.201, 0.0.200) you transferred HBAR to by sending an account balance query. This query will check the current balance of the specified account. The current account balance should be the sum of the initial balance plus the transfer amount. For example, if the initial account balance is 100 HBAR, the balance after transferring 2 HBAR will be 102 HBAR.
ScriptTransferHbar.java
// Query HBAR balance using AccountBalanceQueryAccountBalance newAccountBalance =newAccountBalanceQuery().setAccountId(operatorId).execute(client);// Get the new HBAR balanceHbar newHbarBalance =newAccountBalance.hbars;// Print the new account balance after the transferSystem.out.println("The new account balance after the transfer: "+newHbarBalance.toString());
script-transfer-hbar.js
// Query HBAR balance using AccountBalanceQueryconstnewAccountBalance=newAccountBalanceQuery().setAccountId('0.0.201').execute(client);// Wait for the query result and get the HBAR balanceconstnewHbarBalance= (await newAccountBalance).hbars;// Log the new account balance after the transferlogger.log('The new account balance after the transfer:',newHbarBalance.toString());
script-transfer-hbar.go
// Query HBAR balance usingnewAccountBalance, _ := hedera.NewAccountBalanceQuery().SetAccountID(operatorId).Execute(client)// Get the new HBAR balancenewHbarBalance := newAccountBalance.Hbars// Print the new account balance after the transferfmt.Printf("The new account balance after the transfer: %s\n", newHbarBalance.String())
Query the Transfer Transaction via Mirror Node API
Mirror nodes store the history of transactions that took place on the network. To query the transaction, use the Mirror Node API with the path /api/v1/transactions/${transferTxIdMirrorNodeFormat}. This API endpoint allows you to retrieve the details of a specified transfer transaction ID.
Specify transferTxId within the URL path
Specify 0 as the nonce query parameter
The constructed transferTxVerifyMirrorNodeApiUrl string should look like this:
You can perform the same Mirror Node API query as transferTxVerifyMirrorNodeApiUrl above. This is what the relevant part of the Swagger page would look like when doing so:
➡ You can learn more about the Mirror Nodes via its documentation: REST API.
Step 3: Run the Transfer Transaction Script
In the terminal, cd into the ./transfer directory and run the transfer transaction script:
The Hedera network Transaction fees are split between two accounts. Most of the fee goes to the fee collection account { 2 } to cover network expenses like processing, bandwidth, and storage costs. The remaining portion is paid to account 0.0.7{ 1 }, the consensus node fee collection account, which plays a critical role in the Hedera network's consensus by validating and processing transactions. This fee structure reflects the actual costs of transactions, protecting against abuse such as Denial of Service (DoS) attacks and ensuring scalable network usage.
{ 0 }Debit (add 1 - 5 for exact amount) from operator account 0.0.464xxx-3.00173036 ℏ
{ 1 } Credit new account 0.0.2001 ℏ
{ 2 } Credit new account 0.0.2012 ℏ
{ 3 } Node fee is paid to account 0.0.40.00007032 ℏ