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

Pre-requisites:

Note: Please follow the example in the version of the SDK you are using. The examples may may be not be compatible if you are using a different version than what is listed.

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 now transfer 1,000 tinybars from your account to the new account. The account sending hbars is required to sign the transaction to authorize the transfer out of that account.
v2.0
v1.0
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);
9
Copied!
1
//System.out.println("The new account balance is: " +accountBalance);
2
//-----------------------<enter code below>--------------------------------------
3
4
TransactionId sendHbar = new TransferTransaction()
5
.addHbarTransfer((myAccountId, Hbar.fromTinybar(-1000)) //Sending account
6
.addHbarTransfer(newAccountId, Hbar.fromTinybar(1000)) //Receiving account
7
.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 by getting the status.
v2.0
v1.0
1
System.out.println("The transfer transaction was: " +sendHbar.getReceipt(client).status);
Copied!
1
System.out.println("The transfer transaction was: " +sendHbar.getReceipt(client).status);
Copied!

Step 3. Get the account balance

3.1 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.
v2.0
v1.0
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!
1
//Request the cost of the query
2
long queryCost = new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.getCost(client);
5
6
System.out.println("The cost of this query is: " +queryCost);
Copied!

3.2 Get the account balance

You will verify the account balance was updated for the new account after the transfer 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.
v2.0
v1.0
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!
1
//Check the new account's balance
2
Hbar accountBalanceNew = new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.execute(client);
5
6
System.out.println("The new account balance is: " +accountBalanceNew);
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:
v2.0
v1.0
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.generate();
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!
HederaExamples.java
1
import com.hedera.hashgraph.sdk.Client;
2
import com.hedera.hashgraph.sdk.Hbar;
3
import com.hedera.hashgraph.sdk.HederaStatusException;
4
import com.hedera.hashgraph.sdk.TransactionId;
5
import com.hedera.hashgraph.sdk.account.AccountBalanceQuery;
6
import com.hedera.hashgraph.sdk.account.AccountCreateTransaction;
7
import com.hedera.hashgraph.sdk.account.AccountId;
8
import com.hedera.hashgraph.sdk.account.TransferTransaction;
9
import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PublicKey;
10
import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey;
11
import io.github.cdimascio.dotenv.Dotenv;
12
13
public class HederaExamples {
14
15
public static void main(String[] args) throws InterruptedException, HederaStatusException {
16
17
//Grab your account ID and private key from the .env file
18
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
19
Ed25519PrivateKey myPrivateKey = Ed25519PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
20
21
//Create a Hedera testnet client
22
Client client = Client.forTestnet();
23
client.setOperator(myAccountId, myPrivateKey);
24
25
//Generate a new pair of keys
26
Ed25519PrivateKey newAccountPrivateKey = Ed25519PrivateKey.generate();
27
Ed25519PublicKey newAccountPublicKey = newAccountPrivateKey.publicKey;
28
29
//Create new account and assign the public key
30
TransactionId newAccount = new AccountCreateTransaction()
31
.setKey(newAccountPublicKey)
32
.setInitialBalance(1000)
33
.execute(client);
34
35
//Get the new account ID
36
AccountId newAccountId = newAccount.getReceipt(client).getAccountId();
37
38
System.out.println("The new account ID is: " +newAccountId);
39
40
//Check the new account's balance
41
Hbar accountBalance = new AccountBalanceQuery()
42
.setAccountId(newAccountId)
43
.execute(client);
44
45
System.out.println("The new account balance is: " +accountBalance);
46
47
//Transfer hbar
48
TransactionId sendHbar = new CryptoTransferTransaction()
49
.addSender(myAccountId, 1000)
50
.addRecipient(newAccountId, 1000)
51
.execute(client);
52
53
System.out.println("The transfer transaction was: " +sendHbar.getReceipt(client).status);
54
55
//Request the cost of the query
56
long queryCost = new AccountBalanceQuery()
57
.setAccountId(newAccountId)
58
.getCost(client);
59
60
System.out.println("The cost of this query is: " +queryCost);
61
62
//Check the new account's balance
63
Hbar accountBalanceNew = new AccountBalanceQuery()
64
.setAccountId(newAccountId)
65
.execute(client);
66
67
System.out.println("The new account balance is: " +accountBalanceNew);
68
}
69
}
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!
Last modified 12d ago