Hedera
Search…
Create an account
The following sample will show you how to make a simple Hedera account. Hedera accounts are the entry point by which you can interact with the Hedera APIs. Accounts hold a balance of hbars used to pay for API calls for the various transaction and query types.

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 to your project

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

Step 2: Generate keys for the new account

You will need to generate public and private key pair to assign to the new account in the next step. Within the async function, generate the private and public key pair for the new account.
v2.0
v1.0
1
//const client = Client.forTestnet();
2
//client.setOperator(myAccountId, myPrivateKey);
3
//-----------------------<enter code below>--------------------------------------
4
5
6
//Create new keys
7
const newAccountPrivateKey = await PrivateKey.generate();
8
const newAccountPublicKey = newAccountPrivateKey.publicKey;
Copied!
1
//const client = Client.forTestnet();
2
//client.setOperator(myAccountId, myPrivateKey);
3
//-----------------------<enter code below>--------------------------------------
4
5
//Create new keys
6
const newAccountPrivateKey = await Ed25519PrivateKey.generate();
7
const newAccountPublicKey = newAccountPrivateKey.publicKey;
Copied!

Step 3: Create the new account

To create a new account you will submit an account create a transaction to the Hedera test network. The account minimally requires you to set the key of the account at creation. This means that the corresponding private key will be required to sign transactions for the new account. The account will additionally have a starting balance of 1,000 tinybars. This initial balance is funded from your account to the new account. You can optionally choose to create an account with a zero balance.
1
//Create a new account with 1,000 tinybar starting balance
2
const newAccountTransactionResponse = await new AccountCreateTransaction()
3
.setKey(newAccountPublicKey)
4
.setInitialBalance(Hbar.fromTinybars(1000))
5
.execute(client);
Copied!
Additional properties for accounts are explained here.

Step 4: Get the new account ID

The new account ID is stored in the receipt of the transaction. You must request the receipt of the transaction to obtain the new account ID. Requesting a receipt is free of charge today.
v2.0
v1.0
1
// Get the new account ID
2
const getReceipt = await newAccountTransactionResponse.getReceipt(client);
3
const newAccountId = getReceipt.accountId;
4
5
console.log("The new account ID is: " +newAccountId);
Copied!
1
// Get the new account ID
2
const getReceipt = await newAccountTransactionId.getReceipt(client);
3
const newAccountId = getReceipt.getAccountId();
4
5
console.log("The new account ID is: " +newAccountId);
Copied!

Step 5: Verify the new account balance

Next, you will submit a query to the Hedera test network to return the balance of the new account using the new account ID. The current account balance for the new account should be 1,000 tinybars.
v2.0
v1.0
1
//Verify the account balance
2
const accountBalance = await new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.execute(client);
5
6
console.log("The new account balance is: " +accountBalance.hbars.toTinybars() +" tinybar.");
Copied!
1
//Verify the account balance
2
const accountBalance = await new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.execute(client);
5
6
console.log("The new account balance is: " +accountBalance.asTinybar() +" tinybar.");
Copied!
⭐ Congratulations! You have successfully completed the following:
    Created new a Hedera account with an initial balance of 1,000 tinybars
    Obtained the new account ID by requesting the receipt of the transaction
    Verified the starting balance of the new account by submitting a query to the network
You are now ready to transfer some hbar to the new account 🤑!

Code Check ✅

Your index.js file should look like this:
v2.0
v1.0
1
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar} = 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
}
46
main();
Copied!
1
const { Client, Ed25519PrivateKey, AccountCreateTransaction, AccountBalanceQuery } = require("@hashgraph/sdk");
2
require("dotenv").config();
3
4
async function main() {
5
6
//Grab your Hedera testnet account ID and private key
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
console.log(myAccountId, myPrivateKey);
17
18
//Create our connection to the Hedera network
19
//The Hedera JS SDK makes this really easy!
20
const client = Client.forTestnet();
21
client.setOperator(myAccountId, myPrivateKey);
22
23
//Create new keys
24
const newAccountPrivateKey = await Ed25519PrivateKey.generate();
25
const newAccountPublicKey = newAccountPrivateKey.publicKey;
26
27
//Create a new account with 1,000 tinybar starting balance
28
const newAccountTransactionId = await new AccountCreateTransaction()
29
.setKey(newAccountPublicKey)
30
.setInitialBalance(1000)
31
.execute(client);
32
33
//Get the account ID
34
const getReceipt = await newAccountTransactionId.getReceipt(client);
35
const newAccountId = getReceipt.getAccountId();
36
37
console.log("The new account ID is: " +newAccountId);
38
39
//Verify the account balance
40
const accountBalance = await new AccountBalanceQuery()
41
.setAccountId(newAccountId)
42
.execute(client);
43
44
console.log("The new account balance is: " +accountBalance.asTinybar() +" tinybar.");
45
46
}
47
main();
Copied!
Last modified 12d ago