Last updated
Was this helpful?
Last updated
Was this helpful?
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.
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.
Before you begin, you should have completed the following tutorials:
transfer
example in the project directoryFrom the root directory of the project, cd
(change directories) into the transfer transaction example script.
If you completed a previous example in the series, you can go back to the root directory and cd
into this example.
If you want to get back to the root directory, you can CD out from any directory with this command
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.
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.
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.
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.
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.
Specify transferTxId
within the URL path
Specify 0
as the nonce
query parameter
The constructed transferTxVerifyMirrorNodeApiUrl
string should look like this:
In the terminal, cd
into the ./transfer
directory and run the transfer transaction script:
Sample output:
To view and verify the transaction details, copy, paste, and open the HashScan URL from the console output in your browser.
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.200
1 ℏ
{ 2 } Credit new account 0.0.201
2 ℏ
{ 3 } Node fee is paid to account 0.0.4
0.00007032 ℏ
{ 4 } Hedera fee collection account 0.0.98
0.00149404 ℏ
{ 5 } Staking reward account fee to 0.0.800
0.00016600 ℏ
0.00007032 + 0.00149404 + 0.00016600 + 10 = 3.00173036
Congratulations, you have completed the Transfer HBAR tutorial in the Getting Started series for the Web2 Developers learning path! 🎉🎉🎉
You learned how to:
Continue building on Hedera with another tutorial in the series to explore more Consensus Node services.
You can follow along through the code walkthrough or skip ahead to execute the program .
Open the transfer HBAR script (e.g., /transfer/script-transfer-hbar...
) in a code editor like , , or a 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.
Create and initialize a transfer transaction (TransaferTransaction
) by specifying the sender account, receiver account, and transfer amount. Refer to the transaction and query for the base transaction fee. In the code snippet below, you use the new testnet account you created in the guide to debit from your operator account (-3 HBAR) and credit accounts 0.0.200
(1 HBAR) and 0.0.201
(2 HBAR).
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.
You can explore the Mirror Node APIs interactively via its Swagger page: .
➡ You can learn more about the Mirror Nodes via its documentation: .
Note: Please see the transaction and query table for the base transaction fee.
Have questions? Join the and post them in the channel or ask on .
Learn how to create fungible tokens using the (HTS).
Learn how to create topics and publish messages using the (HCS).