Hedera
Search…
Transfer hbar

Summary

In this section, you will learn how to transfer hbars from your account to another account on the Hedera test network.

Pre-requisites:

You can always check the "Code Check βœ… " section at the bottom of each page to view the entire code if you run into issues. You can also post your issue to the respective SDK channel in our Discord community here or on the GitHub repository.
Java
JavaScript
Go

Step 1. Create a transfer transaction

You should already have a new account from the account you created in the "Create an account" section. You will transfer 1,000 tinybars from your account to the new account. The account transferring hbars is required to sign the transaction with the account's corresponding private key to authorize the transfer. Since you are transferring from the account associated with the client, you do not need to explicitly sign the transaction as the operator account signs all transactions to authorize the payment of the transaction fee.
1
//System.out.println("The new account balance is: " +accountBalance.hbars);
2
//-----------------------<enter code below>--------------------------------------
3
​
4
//Transfer hbar
5
TransactionResponse sendHbar = new TransferTransaction()
6
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000)) //Sending account
7
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000)) //Receiving account
8
.execute(client);
Copied!
The net value of the transfer must equal zero (total number of hbars sent by the sender must equal the total number of hbars received by the recipient).

Step 2. Verify the transfer transaction reached consensus

To verify the transfer transaction reached consensus by the network, you will submit a request to obtain the receipt of the transfer transaction. The receipt will let you know if the transaction was successful or not.
1
System.out.println("The transfer transaction was: " +sendHbar.getReceipt(client).status);
Copied!

Step 3. Get the account balance

Get the cost of requesting the query

You can request the cost of a query prior to submitting the query to the Hedera network. Checking an account balance is free of charge today. You can verify that by the method below.
1
//Request the cost of the query
2
Hbar queryCost = new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.getCost(client);
5
​
6
System.out.println("The cost of this query is: " +queryCost);
Copied!

Get the account balance

You will verify the account balance was updated for the new account by requesting a get account balance query. The current account balance should be the sum of the initial balance (1,000 tinybar) plus the transfer amount (1,000 tinybar) and equal to 2,000 tinybars.
1
//Check the new account's balance
2
AccountBalance accountBalanceNew = new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.execute(client);
5
​
6
System.out.println("The new account balance is: " +accountBalanceNew.hbars);
Copied!
​
⭐
Congratulations! You have successfully transferred hbars to another account on the Hedera testnet! If you have followed the tutorial from the beginning, you have completed the following thus far:
  • Set up your Hedera environment to submit transactions and queries
  • Created an account
  • Transferred hbars to another account
Do you want to keep learning? Visit our "Resources" and "Documentation" sections to take your learning experience to the next level. You can also find additional Java SDK examples here.
​

Code Check
βœ…

Your complete code file should look something like this:
HederaExamples.java
1
import com.hedera.hashgraph.sdk.AccountId;
2
import com.hedera.hashgraph.sdk.HederaPreCheckStatusException;
3
import com.hedera.hashgraph.sdk.HederaReceiptStatusException;
4
import com.hedera.hashgraph.sdk.PrivateKey;
5
import com.hedera.hashgraph.sdk.Client;
6
import com.hedera.hashgraph.sdk.TransactionResponse;
7
import com.hedera.hashgraph.sdk.PublicKey;
8
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
9
import com.hedera.hashgraph.sdk.Hbar;
10
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
11
import com.hedera.hashgraph.sdk.AccountBalance;
12
import com.hedera.hashgraph.sdk.TransferTransaction;
13
import io.github.cdimascio.dotenv.Dotenv;
14
​
15
import java.util.concurrent.TimeoutException;
16
​
17
public class HederaExamples {
18
​
19
public static void main(String[] args) throws TimeoutException, HederaPreCheckStatusException, HederaReceiptStatusException {
20
​
21
//Grab your Hedera testnet account ID and private key
22
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
23
PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
24
​
25
//Create your Hedera testnet client
26
Client client = Client.forTestnet();
27
client.setOperator(myAccountId, myPrivateKey);
28
​
29
// Generate a new key pair
30
PrivateKey newAccountPrivateKey = PrivateKey.generateED25519());
31
PublicKey newAccountPublicKey = newAccountPrivateKey.getPublicKey();
32
​
33
//Create new account and assign the public key
34
TransactionResponse newAccount = new AccountCreateTransaction()
35
.setKey(newAccountPublicKey)
36
.setInitialBalance( Hbar.fromTinybars(1000))
37
.execute(client);
38
​
39
// Get the new account ID
40
AccountId newAccountId = newAccount.getReceipt(client).accountId;
41
​
42
System.out.println("The new account ID is: " +newAccountId);
43
​
44
//Check the new account's balance
45
AccountBalance accountBalance = new AccountBalanceQuery()
46
.setAccountId(newAccountId)
47
.execute(client);
48
​
49
System.out.println("The new account balance is: " +accountBalance.hbars);
50
​
51
//Transfer hbar
52
TransactionResponse sendHbar = new TransferTransaction()
53
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000))
54
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000))
55
.execute(client);
56
​
57
System.out.println("The transfer transaction was: " +sendHbar.getReceipt(client).status);
58
​
59
//Request the cost of the query
60
Hbar queryCost = new AccountBalanceQuery()
61
.setAccountId(newAccountId)
62
.getCost(client);
63
​
64
System.out.println("The cost of this query is: " +queryCost);
65
​
66
//Check the new account's balance
67
AccountBalance accountBalanceNew = new AccountBalanceQuery()
68
.setAccountId(newAccountId)
69
.execute(client);
70
​
71
System.out.println("The new account balance is: " +accountBalanceNew.hbars);
72
​
73
}
74
}
75
​
Copied!

Sample Output:

1
The new account ID is: 0.0.215975
2
The new account balance is: 1000 tℏ
3
The transfer transaction was: SUCCESS The cost of this query is: 0
4
The new account balance is: 2000 tℏ
Copied!

Step 1: Create a transfer transaction

You should already have a new account ID from the account you created from the "Create an account" section. You will transfer 1,000 tinybars from your account to the new account. The account sending hbars is the signature that is required for this transaction to be processed.
1
//console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
2
//-----------------------<enter code below>--------------------------------------
3
​
4
//Create the transfer transaction
5
const sendHbar = await new TransferTransaction()
6
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000)) //Sending account
7
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000)) //Receiving account
8
.execute(client);
Copied!
The net value of the transfer must equal zero (total number of hbars sent by the sender must equal the total number of hbars received by the recipient).

Step 2: Verify the transfer transaction reached consensus

To verify the transfer transaction reached consensus by the network, you will submit a request to obtain the receipt of the transaction. The receipt status will let you know if the transaction was successful (reached consensus) or not.
1
//Verify the transaction reached consensus
2
const transactionReceipt = await sendHbar.getReceipt(client);
3
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
Copied!

Step 3: Get the updated account balance

Get the cost of requesting the query

You can request the cost of a query prior to submitting the query to the Hedera network. Checking an account balance is free of charge today. You can verify that by the method below.
1
//Request the cost of the query
2
const queryCost = await new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.getCost(client);
5
​
6
console.log("The cost of query is: " +queryCost);
Copied!

Get the account balance

You will verify the account balance was updated for the new account by requesting a get account balance query. The current account balance should be the sum of the initial balance (1,000 tinybars) plus the transfer amount (1,000 tinybasr) and equal to 2,000 tinybars.
1
//Check the new account's balance
2
const getNewBalance = await new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.execute(client);
5
​
6
console.log("The account balance after the transfer is: " +getNewBalance.hbars.toTinybars() +" tinybar.")
Copied!
⭐ Congratulations! You have successfully transferred hbars to another account on the Hedera testnet! If you have followed the tutorial from the beginning, you have completed the following thus far:
  • Set up your Hedera environment to submit transactions and queries
  • Created an account
  • Transferred hbars to another account
Do you want to keep learning? Visit our "Resources" and "Documentation" sections to take your learning experience to the next level. You can also find additional JavaScript SDK examples here.

Code Check βœ…

Your complete index.js file should look something like this:
index.js
1
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar, TransferTransaction} = require("@hashgraph/sdk");
2
require("dotenv").config();
3
​
4
async function main() {
5
​
6
//Grab your Hedera testnet account ID and private key from your .env file
7
const myAccountId = process.env.MY_ACCOUNT_ID;
8
const myPrivateKey = process.env.MY_PRIVATE_KEY;
9
​
10
// If we weren't able to grab it, we should throw a new error
11
if (myAccountId == null ||
12
myPrivateKey == null ) {
13
throw new Error("Environment variables myAccountId and myPrivateKey must be present");
14
}
15
​
16
// Create our connection to the Hedera network
17
// The Hedera JS SDK makes this really easy!
18
const client = Client.forTestnet();
19
​
20
client.setOperator(myAccountId, myPrivateKey);
21
​
22
//Create new keys
23
const newAccountPrivateKey = await PrivateKey.generateED25519();
24
const newAccountPublicKey = newAccountPrivateKey.publicKey;
25
​
26
//Create a new account with 1,000 tinybar starting balance
27
const newAccountTransactionResponse = await new AccountCreateTransaction()
28
.setKey(newAccountPublicKey)
29
.setInitialBalance(Hbar.fromTinybars(1000))
30
.execute(client);
31
​
32
// Get the new account ID
33
const getReceipt = await newAccountTransactionResponse.getReceipt(client);
34
const newAccountId = getReceipt.accountId;
35
​
36
console.log("The new account ID is: " +newAccountId);
37
​
38
//Verify the account balance
39
const accountBalance = await new AccountBalanceQuery()
40
.setAccountId(newAccountId)
41
.execute(client);
42
​
43
console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
44
​
45
//Create the transfer transaction
46
const sendHbar = await new TransferTransaction()
47
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000))
48
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000))
49
.execute(client);
50
​
51
//Verify the transaction reached consensus
52
const transactionReceipt = await sendHbar.getReceipt(client);
53
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
54
55
//Request the cost of the query
56
const queryCost = await new AccountBalanceQuery()
57
.setAccountId(newAccountId)
58
.getCost(client);
59
​
60
console.log("The cost of query is: " +queryCost);
61
​
62
//Check the new account's balance
63
const getNewBalance = await new AccountBalanceQuery()
64
.setAccountId(newAccountId)
65
.execute(client);
66
​
67
console.log("The account balance after the transfer is: " +getNewBalance.hbars.toTinybars() +" tinybar.")
68
​
69
}
70
main();
Copied!

Step 1. Create a transfer transaction

You should already have a new account ID from the account you created in the "Create an account" section. You will transfer 1,000 tinybars from your testnet account to the new account. The sender account's private key is required to sign the transaction. The sender account is your testnet account so the client is already set-up to sign with your testnet account's private key to authorize the transfer.
1
//Print the balance of tinybars
2
//fmt.Println("The account balance for the new account is ", accountBalance.Hbars.AsTinybar())
3
//-----------------------<enter code below>--------------------------------------
4
​
5
//Transfer hbar from your testnet account to the new account
6
transaction := hedera.NewTransferTransaction().
7
AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).
8
AddHbarTransfer(newAccountId,hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar))
9
​
10
//Submit the transaction to a Hedera network
11
txResponse, err := transaction.Execute(client)
12
​
13
if err != nil {
14
panic(err)
15
}
Copied!
The net value of the transfer must equal zero (total number of hbars sent by the sender must equal the total number of hbars received by the recipient).

Step 2. Verify the transfer transaction reached consensus

To verify the transfer transaction reached consensus by the network, you will submit a request to obtain the receipt of the transfer transaction. The receipt will let you know if the transaction was successful or not.
1
//Request the receipt of the transaction
2
transferReceipt, err := txResponse.GetReceipt(client)
3
​
4
if err != nil {
5
panic(err)
6
}
7
​
8
//Get the transaction consensus status
9
transactionStatus := transferReceipt.Status
10
​
11
fmt.Printf("The transaction consensus status is %v\n", transactionStatus)
Copied!

Step 3. Get the account balance

Get the cost of requesting the query

You can request the cost of a query prior to submitting the query to the Hedera network. Checking an account balance is free of charge today. You can verify that by the method below.
1
//Create the query that you want to submit
2
balanceQuery := hedera.NewAccountBalanceQuery().
3
SetAccountID(newAccountId)
4
​
5
//Get the cost of the query
6
cost, err := balanceQuery.GetCost(client)
7
​
8
if err != nil {
9
panic(err)
10
}
11
​
12
println("The account balance query cost is:", cost.String())
Copied!
​

Get the account balance

You will verify the account balance was updated for the new account by submitting a get account balance query. The current account balance should be the sum of the initial balance (1,000 tinybar) plus the transfer amount (1,000 tinybar) and equal to 2,000 tinybars.
1
//Check the new account's balance
2
newAccountBalancequery := hedera.NewAccountBalanceQuery().
3
SetAccountID(newAccountId)
4
​
5
//Sign with client operator private key and submit the query to a Hedera network
6
newAccountBalance, err := newAccountBalancequery.Execute(client)
7
if err != nil {
8
panic(err)
9
}
10
​
11
//Print the balance of tinybars
12
fmt.Println("The hbar account balance for this account is", newAccountBalance.Hbars.AsTinybar())
Copied!
​
⭐ Congratulations! You have successfully transferred hbars to another account on the Hedera testnet! If you have followed the tutorial from the beginning, you have completed the following thus far:
  • Set up your Hedera environment to submit transactions and queries
  • Created an account
  • Transferred hbars to another account
Do you want to keep learning? Visit our "Resources" and "Documentation" sections to take your learning experience to the next level. You can also find additional Hedera Go SDK examples here.

Code Check βœ…

Your complete code file should look something like this:
1
package main
2
​
3
import (
4
"fmt"
5
"os"
6
​
7
"github.com/hashgraph/hedera-sdk-go/v2"
8
"github.com/joho/godotenv"
9
)
10
​
11
func main() {
12
​
13
//Loads the .env file and throws an error if it cannot load the variables from that file correctly
14
err := godotenv.Load(".env")
15
if err != nil {
16
panic(fmt.Errorf("Unable to load environment variables from .env file. Error:\n%v\n", err))
17
}
18
​
19
//Grab your testnet account ID and private key from the .env file
20
myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))
21
if err != nil {
22
panic(err)
23
}
24
​
25
myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))
26
if err != nil {
27
panic(err)
28
}
29
​
30
//Print your testnet account ID and private key to the console to make sure there was no error
31
fmt.Printf("The account ID is = %v\n", myAccountId)
32
fmt.Printf("The private key is = %v\n", myPrivateKey)
33
​
34
//Create your testnet client
35
client := hedera.ClientForTestnet()
36
client.SetOperator(myAccountId, myPrivateKey)
37
​
38
//Generate new keys for the account you will create
39
newAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
40
if err != nil {
41
panic(err)
42
}
43
​
44
newAccountPublicKey := newAccountPrivateKey.PublicKey()
45
​
46
//Create new account and assign the public key
47
newAccount, err := hedera.NewAccountCreateTransaction().
48
SetKey(newAccountPublicKey).
49
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
50
Execute(client)
51
​
52
//Request the receipt of the transaction
53
receipt, err := newAccount.GetReceipt(client)
54
if err != nil {
55
panic(err)
56
}
57
​
58
//Get the new account ID from the receipt
59
newAccountId := *receipt.AccountID
60
​
61
//Print the new account ID to the console
62
fmt.Printf("The new account ID is %v\n", newAccountId)
63
​
64
//Create the account balance query
65
query := hedera.NewAccountBalanceQuery().
66
SetAccountID(newAccountId)
67
​
68
//Sign with client operator private key and submit the query to a Hedera network
69
accountBalance, err := query.Execute(client)
70
if err != nil {
71
panic(err)
72
}
73
​
74
//Print the balance of tinybars
75
fmt.Println("The account balance for the new account is", accountBalance.Hbars.AsTinybar())
76
​
77
//Transfer hbar from your testnet account to the new account
78
transaction := hedera.NewTransferTransaction().
79
AddHbarTransfer(myAccountId, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).
80
AddHbarTransfer(newAccountId, hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar))
81
​
82
//Submit the transaction to a Hedera network
83
txResponse, err := transaction.Execute(client)
84
​
85
if err != nil {
86
panic(err)
87
}
88
​
89
//Request the receipt of the transaction
90
transferReceipt, err := txResponse.GetReceipt(client)
91
​
92
if err != nil {
93
panic(err)
94
}
95
​
96
//Get the transaction consensus status
97
transactionStatus := transferReceipt.Status
98
​
99
fmt.Printf("The transaction consensus status is %v\n", transactionStatus)
100
​
101
//Create the query that you want to submit
102
balanceQuery := hedera.NewAccountBalanceQuery().
103
SetAccountID(newAccountId)
104
​
105
//Get the cost of the query
106
cost, err := balanceQuery.GetCost(client)
107
​
108
if err != nil {
109
panic(err)
110
}
111
​
112
fmt.Println("The account balance query cost is:", cost.String())
113
​
114
//Check the new account's balance
115
newAccountBalancequery := hedera.NewAccountBalanceQuery().
116
SetAccountID(newAccountId)
117
​
118
//Sign with client operator private key and submit the query to a Hedera network
119
newAccountBalance, err := newAccountBalancequery.Execute(client)
120
if err != nil {
121
panic(err)
122
}
123
​
124
//Print the balance of tinybars
125
fmt.Println("The hbar account balance for this account is", newAccountBalance.Hbars.AsTinybar())
126
}
Copied!
​
​
Last modified 4d ago
Copy link