Create and fund account

Hello World sequence: Create a new account on Hedera Testnet, and fund it. Do this before any of the other Hello World sequences.

Why you need to create and fund an account

Hedera is a distributed ledger technology (DLT). To interact with it, you will need to send transactions to the network, which will then process them and add them to the ledger if they are deemed to be valid. On most web services (web2), you need to authenticate using usernames and passwords to operate your account. On DLTs such as Hedera, it is similar, except that you will need to use cryptographic keys instead of passwords to operate your account. One key difference is that unlike web2, each interaction needs to be paid for using the native currency of the DLT, which is similar to microtransactions. On Hedera, this currency is HBAR.

What you will accomplish


Prerequisites

Before you begin, you should be familiar with the following:

Also, you should have the following set up on your computer
Check your prerequisites set up

Open your terminal, and enter the following commands.

bash --version
zsh --version
git --version
code --version
node --version
npm --version

Each of these commands should output some text that includes a version number, for example:

bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin22)
Copyright (C) 2007 Free Software Foundation, Inc.

zsh --version
zsh 5.9 (x86_64-apple-darwin22.0)

git --version
git version 2.39.2 (Apple Git-143)

code --version
1.81.1
6c3e3dba23e8fadc360aed75ce363ba185c49794
arm64

node --version
v20.6.1

npm --version
9.8.1

If the output contains text similar to command not found, please install that item.

If the version number that is output is lower than the required versions, please re-install or update that item.

If the version number that is output is same or higher than the required versions, you have met the prequisites! 🎉


Get started: Set up project

To follow along, start with the main branch, which is the default branch of this repo. This gives you the initial state from which you can follow along with the steps as described in the tutorial.

git clone https://github.com/hedera-dev/hello-future-world.git
Alternative with `git` and SSH

If you have configured SSH to work with git, you may wish use this command instead:

git clone [email protected]:hedera-dev/hello-future-world.git

In the terminal, from the hello-future-world directory, enter the subdirectory for this sequence.

cd 00-create-fund-account/

Install the dependencies using npm.

npm install

Create your .env file

Make a .env file by copying the provided .env.sample file. Then open the .env file in a code editor, such as VS Code.

cp .env.sample .env

Create account

If you already have an account on the Hedera Portal, you may skip the following steps.

Set up a Hedera Portal account ⬇
  1. Visit the Hedera Portal and create a Testnet account.

  1. Check your email for a confirmation code. Copy and paste this confirmation code to verify your Hedera portal account.

  1. Fill out this form.

  1. In the top-left, select Hedera Testnet from the drop-down.

Once your portal testnet account is created, copy the Hedera Testnet Account ID and HEX-encoded private key from your Hedera portal dashboard (see screenshot below) and assign them to the MY_ACCOUNT_ID and MY_PRIVATE_KEY variables in your .env file:

For example, if your Account ID is 0.0.123, and your HEX-encoded private key is 0xabcd1234, your .env file should look like this:

.env
ACCOUNT_ID=0.0.123
ACCOUNT_PRIVATE_KEY=0xabcd1234

🎉 Now you are ready to start using your Hedera Testnet account from the portal within script files on your computer! Be sure to save your files before moving on to the next step.


Write the script

An almost complete script has already been prepared for you, and you will only need to make a few modifications (outlined below) for it to run successfully.

Then open the script file, script-create-fund-account.js, in a code editor.

Step 1: Initialize the account using Client

Initialize the client instance by invoking the setOperator method, and passing in accountId and accountKey as parameter.

    const client = Client.forTestnet().setOperator(accountId, accountKey);

Now the client instance represents and operates your account.

Look for a comment in the code to locate the specific lines of code that you will need to edit. For example, for this step, look for this:

    // Step (1) in the accompanying tutorial

You will need to delete the inline comment that looks like this: /* ... */. Replace it with the correct code. For example, in this step, insert this:

accountId, accountKey

Step 2: Obtain the balance of the account

Use the AccountBalanceQuery method to obtain the Testnet HBAR balance of your account.

    const accountBalance = await new AccountBalanceQuery()

Be sure to check that the line of code matches the above exactly. It is easy to miss the await when copy-pasting!

Note: The return value is an object and needs to be parsed.

Step 3: Convert the balance result object to HBARs

Parse that return value to extract its Testnet HBAR balance so that you may convert it into a string for display purposes.

    const accountBalanceHbars = accountBalance.hbars.toBigNumber();

Run the script

In the terminal, run the script using the following command:

node script-create-fund-account.js

You should see output similar to the following:

accountId: 0.0.1201
accountBalanceTinybars: 10,000.00000000
accountExplorerUrl: https://hashscan.io/testnet/account/0.0.1201

Open accountExplorerUrl in your browser and check that:

  • The account exists, and its "account ID" should match accountId.

  • The "balances" should match accountBalanceTinybars.

Drawing
Account in Hashscan, with annotated items to check.

Complete

Congratulations, you have completed the create and fund account Hello World sequence! 🎉🎉🎉

You have learned how to:


Next Steps

Now that you have an account on Hedera Testnet and it is funded, you can interact with the Hedera network. Continue by following along with the other Hello World sequences.


Cheat sheet

Skip to final state

The repo, github.com/hedera-dev/hello-future-world, is intended to be used alongside this tutorial.

To skip ahead to the final state, use the completed branch. This gives you the final state with which you can compare your implementation to the completed steps of the tutorial.

git fetch origin completed:completed
git checkout completed

To see the full set of differences between the initial and final states of the repo, you can use diff.

cd 00-create-fund-account/
git diff main..completed -- ./

Alternatively, you may view the diff rendered on Github: hedera-dev/hello-future-world/compare/main..completed (This will show the diff for all sequences.)

Note that the branch names are delimited by .., and not by ..., as the latter finds the diff with the latest common ancestor commit, which is not what we want in this case.


Writer: Brendan Editors: Abi, Michiel, Ryan, Krystal

Last updated

Was this helpful?

#2416: #441 #524 #553 update docs with portal faucet + API key features

Change request updated