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 HBAR used to pay for API calls for the various transaction and query types.
Prerequisites
Step 1: Import modules
Import the following modules to your code file.
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.HederaPreCheckStatusException;
import com.hedera.hashgraph.sdk.HederaReceiptStatusException;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.TransactionResponse;
import com.hedera.hashgraph.sdk.PublicKey;
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
import com.hedera.hashgraph.sdk.AccountBalance;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.concurrent.TimeoutException;
Step 2: Generate keys for the new account
Generate a private and public key to associate with the account you will create.
//Create your Hedera Testnet client
//Client client = Client.forTestnet();
//client.setOperator(myAccountId, myPrivateKey)
//-----------------------<enter code below>--------------------------------------
// Generate a new key pair
PrivateKey newAccountPrivateKey = PrivateKey.generateED25519();
PublicKey newAccountPublicKey = newAccountPrivateKey.getPublicKey();
Step 3: Create a 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 with 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 HBAR or tokens from that account to another account. The account will have an initial balance of 1,000 tinybars funded from your testnet account created by the Hedera portal.
You can view transactions 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's valid start time e.g. 0.0.1234@1609348302
. The transaction's 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.
//Create new account and assign the public key
TransactionResponse newAccount = new AccountCreateTransaction()
.setKey(newAccountPublicKey)
.setInitialBalance( Hbar.fromTinybars(1000))
.execute(client);
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 scheduled 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
.
// Get the new account ID
AccountId newAccountId = newAccount.getReceipt(client).accountId;
//Log the account ID
System.out.println("The new account ID is: " +newAccountId);
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.
//Check the new account's balance
AccountBalance accountBalance = new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client);
System.out.println("The new account balance is: " +accountBalance.hbars);
⭐ Congratulations! You have successfully completed the following:
Created a new 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 ✅
Sample output:
New account ID: 0.0.13724748
New account balance: 1000 tinybars.
Last updated