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 not be compatible if you are using a different version than what is listed.

Step 1: Import the following modules

Continue building on the index.js from the previous example (Create an account) and add the following modules:
    TransferTransaction
v2.0
v1.0
1
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar, TransferTransaction} = require("@hashgraph/sdk");
2
require("dotenv").config();
Copied!
1
//Import the following modules
2
const { Client, Ed25519PrivateKey, AccountCreateTransaction, AccountBalanceQuery,Hbar, TransferTransaction } = require("@hashgraph/sdk");
3
require("dotenv").config();
Copied!

Step 2: 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.
v2.0
v1.0
1
//console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
2
//-----------------------<enter code below>--------------------------------------
3
4
//Create the transfer transaction
5
const transferTransactionResponse = await new TransferTransaction()
6
.addHbarTransfer(myAccountId, Hbar.fromTinybars(-1000)) //Sending account
7
.addHbarTransfer(newAccountId, Hbar.fromTinybars(1000)) //Receiving account
8
.execute(client);
Copied!
1
//console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
2
//-----------------------<enter code below>--------------------------------------
3
4
//Create the transfer transaction
5
const transferTransactionId = 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 3: 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.
v2.0
v1.0
1
//Verify the transaction reached consensus
2
const transactionReceipt = await transferTransactionResponse.getReceipt(client);
3
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
Copied!
1
//Verify the transaction reached consensus
2
const transactionReceipt = await transferTransactionId.getReceipt(client);
3
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status);
Copied!

Step 4: Get the updated account balance

4.1 Get the cost of requesting the query

You can request the cost of a query (or transaction) 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
const getBalanceCost = await new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.getCost(client);
5
6
console.log("The cost of query is: " +getBalanceCost);
Copied!
index.js
1
//Request the cost of the query
2
const getBalanceCost = await new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.getCost(client);
5
6
console.log("The cost of query is: " +getBalanceCost);
Copied!

4.2 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:
v2.0
v1.0
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.generate();
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 transferTransactionResponse = 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 transferTransactionResponse.getReceipt(client);
53
console.log("The transfer transaction from my account to the new account was: " + transactionReceipt.status.toString());
54
55
//Check the new account's balance
56
const getNewBalance = await new AccountBalanceQuery()
57
.setAccountId(newAccountId)
58
.execute(client);
59
60
console.log("The account balance after the transfer is: " +getNewBalance.hbars.toTinybars() +" tinybar.")
61
62
}
63
main();
Copied!
index.js
1
const { Client, Ed25519PrivateKey, 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 Ed25519PrivateKey.generate();
24
const newAccountPublicKey = newAccountPrivateKey.publicKey;
25
26
//Create a new account with 1,000 tinybar starting balance
27
const newAccountTransactionId = await new AccountCreateTransaction()
28
.setKey(newAccountPublicKey)
29
.setInitialBalance(Hbar.fromTinybar(1000))
30
.execute(client);
31
32
// Get the new account ID
33
const getReceipt = await newAccountTransactionId.getReceipt(client);
34
const newAccountId = getReceipt.getAccountId();
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
44
console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
45
46
//Create the transfer transaction
47
const transferTransactionId = await new TransferTransaction()
48
.addHbarTransfer(myAccountId, -1000)
49
.addHbarTransfer(newAccountId, 1000)
50
.execute(client);
51
52
//Verify the transaction reached consensus
53
const transactionReceipt = await transferTransactionId.getReceipt(client);
54
console.log("The transfer transaction from my account to the new account was: " +transactionReceipt.status);
55
56
//Request the cost of the query
57
const getBalanceCost = await new AccountBalanceQuery()
58
.setAccountId(newAccountId)
59
.getCost(client);
60
61
console.log("The cost of query is: " +getBalanceCost);
62
63
//Check the new account's balance
64
const getNewBalance = await new AccountBalanceQuery()
65
.setAccountId(newAccountId)
66
.execute(client);
67
68
console.log("The account balance after the transfer is: " +getNewBalance.hbars.toTinybars() +" tinybar.")
69
}
70
main();
Copied!
Last modified 3mo ago