Hedera
Search…
Create an account

Summary

In this section, you will learn 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:

Step 1: Import modules

Import the following modules to your code file.
Java
JavaScript
Go
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 io.github.cdimascio.dotenv.Dotenv;
13
​
14
import java.util.concurrent.TimeoutException;
Copied!
1
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar } = require("@hashgraph/sdk");
2
require("dotenv").config();
Copied!
1
import (
2
"fmt"
3
"os"
4
​
5
"github.com/hashgraph/hedera-sdk-go/v2"
6
"github.com/joho/godotenv"
7
)
Copied!

Step 2: Generate keys for the new account

Generate a private and public key to associate to the account you will create.
Java
JavaScript
Go
1
//Create your Hedera testnet client
2
//Client client = Client.forTestnet();
3
//client.setOperator(myAccountId, myPrivateKey)
4
//-----------------------<enter code below>--------------------------------------
5
​
6
// Generate a new key pair
7
PrivateKey newAccountPrivateKey = PrivateKey.generateED25519()
8
PublicKey newAccountPublicKey = newAccountPrivateKey.getPublicKey();
Copied!
1
//const client = Client.forTestnet();
2
//client.setOperator(myAccountId, myPrivateKey);
3
//-----------------------<enter code below>--------------------------------------
4
​
5
//Create new keys
6
const newAccountPrivateKey = await PrivateKey.generateED25519();
7
const newAccountPublicKey = newAccountPrivateKey.publicKey;
Copied!
1
//client := hedera.ClientForTestnet()
2
//client.SetOperator(myAccountId, myPrivateKey)
3
//-----------------------<enter code below>--------------------------------------
4
​
5
//Generate new keys for the account you will create
6
newAccountPrivateKey, err := hedera.PrivateKeyGenerateEd25519()
7
​
8
if err != nil {
9
panic(err)
10
}
11
​
12
newAccountPublicKey := newAccountPrivateKey.PublicKey()
Copied!

Step 3: Create new account

Create a new account using AccountCreateTransaction(). Use the public key created in the previous step to enter in the setKey() field. This will associate the key pair generated in the previous step to the new account. The public key of the account is visible to the public and can be viewed in a mirror node explorer. The private key is used to authorize account related transactions like transferring hbars or tokens from that account to another account. The account will have an initial balance of 1,000 tinybar that is funded from your testnet account created by the Hedera portal.
You can view transactions that were successfully submitted to the network by getting the transaction ID and searching for it in a mirror node explorer. The transaction ID is composed of the account ID that paid for the transaction and the transaction valid start time e.g. [email protected] <ADD IN EXAMPLE>. The transaction valid start time is the time the transaction begins to be valid on the network. The SDK automatically generates a transaction ID for each transaction behind the scenes.
Java
JavaScript
Go
1
//Create new account and assign the public key
2
TransactionResponse newAccount = new AccountCreateTransaction()
3
.setKey(newAccountPublicKey)
4
.setInitialBalance( Hbar.fromTinybars(1000))
5
.execute(client);
Copied!
1
//Create a new account with 1,000 tinybar starting balance
2
const newAccount = await new AccountCreateTransaction()
3
.setKey(newAccountPublicKey)
4
.setInitialBalance(Hbar.fromTinybars(1000))
5
.execute(client);
Copied!
1
//Create new account and assign the public key
2
newAccount, err := hedera.NewAccountCreateTransaction().
3
SetKey(newAccountPublicKey).
4
SetInitialBalance(hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)).
5
Execute(client)
Copied!

Step 4: Get the new account ID

The account ID for the new account is returned in the receipt of the transaction that created the account. The receipt provides information about the transaction like whether it was successful or not and any new entity IDs that were created. Entities include accounts, smart contracts, tokens, files, topics, and schedule transactions. The account ID is in x.y.z format where z is the account number. The preceding values (x and y) default to zero today and represent the shard and realm number respectively. Your new account ID should result in something like 0.0.1234.
Java
JavaScript
Go
1
// Get the new account ID
2
AccountId newAccountId = newAccount.getReceipt(client).accountId;
3
​
4
//Log the account ID
5
System.out.println("The new account ID is: " +newAccountId);
Copied!
1
// Get the new account ID
2
const getReceipt = await newAccount.getReceipt(client);
3
const newAccountId = getReceipt.accountId;
4
​
5
//Log the account ID
6
console.log("The new account ID is: " +newAccountId);
Copied!
1
//Request the receipt of the transaction
2
receipt, err := newAccount.GetReceipt(client)
3
if err != nil {
4
panic(err)
5
}
6
​
7
//Get the new account ID from the receipt
8
newAccountId := *receipt.AccountID
9
​
10
//Log the account ID
11
fmt.Printf("The new account ID is %v\n", 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. Getting the balance of an account is free today.
Java
JavaScript
Go
1
//Check the new account's balance
2
AccountBalance accountBalance = new AccountBalanceQuery()
3
.setAccountId(newAccountId)
4
.execute(client);
5
​
6
System.out.println("The new account balance is: " +accountBalance.hbars);
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.hbars.toTinybars() +" tinybar.");
Copied!
1
//Create the account balance query
2
query := hedera.NewAccountBalanceQuery().
3
SetAccountID(newAccountId)
4
​
5
//Sign with client operator private key and submit the query to a Hedera network
6
accountBalance, err := query.Execute(client)
7
if err != nil {
8
panic(err)
9
}
10
​
11
//Print the balance of tinybars
12
fmt.Println("The account balance for the new account is ", accountBalance.Hbars.AsTinybar())
Copied!
​
​
⭐
Congratulations! You have successfully completed the following:
  • Created new a Hedera account with an initial balance of 1,000 tinybar
  • 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
βœ…
​

Java
JavaScript
Go
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 io.github.cdimascio.dotenv.Dotenv;
13
​
14
import java.util.concurrent.TimeoutException;
15
​
16
public class HederaExamples {
17
​
18
public static void main(String[] args) throws TimeoutException, HederaPreCheckStatusException, HederaReceiptStatusException {
19
​
20
//Grab your Hedera testnet account ID and private key
21
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
22
PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
23
​
24
//Create your Hedera testnet client
25
Client client = Client.forTestnet();
26
client.setOperator(myAccountId, myPrivateKey);
27
​
28
// Generate a new key pair
29
PrivateKey newAccountPrivateKey = PrivateKey.generateED25519();
30
PublicKey newAccountPublicKey = newAccountPrivateKey.getPublicKey();
31
​
32
//Create new account and assign the public key
33
TransactionResponse newAccount = new AccountCreateTransaction()
34
.setKey(newAccountPublicKey)
35
.setInitialBalance( Hbar.fromTinybars(1000))
36
.execute(client);
37
​
38
// Get the new account ID
39
AccountId newAccountId = newAccount.getReceipt(client).accountId;
40
​
41
System.out.println("The new account ID is: " +newAccountId);
42
​
43
//Check the new account's balance
44
AccountBalance accountBalance = new AccountBalanceQuery()
45
.setAccountId(newAccountId)
46
.execute(client);
47
​
48
System.out.println("The new account balance is: " +accountBalance.hbars);
49
​
50
}
51
}
Copied!
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.generateED25519();
24
const newAccountPublicKey = newAccountPrivateKey.publicKey;
25
​
26
//Create a new account with 1,000 tinybar starting balance
27
const newAccount = 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 newAccount.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
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
}
Copied!
​
​
​
​
​
​
​
Last modified 4d ago