Hedera
Search…
Create an account
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:

Note: This example uses Hedera Go SDK v2.0. The latest Hedera Go SDK version can be found here, however may not be compatible with this tutorial.

Step 1. Generate keys for the new account

Create keys for the new account. The public key will be associated with the new account and the corresponding private key is used to sign transactions.
Continue building on the hedera_examples file from the previous section.
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.GeneratePrivateKey()
7
if err != nil {
8
panic(err)
9
}
10
11
newAccountPublicKey := newAccountPrivateKey.PublicKey()
Copied!

Step 2. Create a new account

To create a new account, you will submit an account create transaction to the Hedera test network. The account minimally requires you to assign a public key to the account at creation. This means that the corresponding private key will be required to sign transactions for the new account. This account will have a starting balance of 1,000 tinybars. This initial balance is funded from your testnet account to the new account. You can optionally choose to create an account with a zero balance.
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!
Additional properties can be set for accounts here.

Step 3. Get the new account ID

When you issue a transaction that creates a new entity (account, token, topic, etc) the entity ID is recovered from 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.
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
//Print the new account ID to the console
11
fmt.Printf("The new account ID is %v\n", newAccountId)
Copied!

Step 4. 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.
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!
You can also verify the account balance by visiting a Hedera explorer like Kabuto or DragonGlass and entering the account ID.
⭐ 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 hbars to the new account 🤑!

Code Check ✅

What your code should look like at this point:
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.GeneratePrivateKey()
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 12d ago