This environment setup guide will provide you with the necessary steps to get your development environment ready for building applications on the Hedera Network. You will set up a new project directory, establish a .env environment variable file to store your Hedera Testnet account ID and private keys and configure your Hedera Testnet client.
Note: You can always check the "Code Check ✅" section at the bottom of each page to view the entire code if you run into issues. You can also post your issue to the respective SDK channel in our Discord communityhereor on the GitHub repositoryhere.
Step 1: Create your project directory
Open your IDE of choice and follow the below steps to create your new project directory.
Create a new Gradle project and name it HederaExamples. Add the following dependencies to your build.gradle file.
Open your terminal and create a directory called hello-hedera-js-sdk. After you create the project directory navigate to the directory by running the following command:
mkdirhello-hedera-js-sdk&&cdhello-hedera-js-sdk
Initialize a node.js project in this new directory by running the following command:
npminit-y
This is what your console should look like after running the command:
{"name":"hello-hedera-js-sdk","version":"1.0.0","description":"","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1" },"keywords": [],"author":"","license":"ISC"}
Open your terminal and create a project directory called something like hedera-go-examples to store your Go source code.
mkdirhedera-go-examples&&cdhedera-go-examples
Step 2: Install Dependencies and SDKs
Create a new Java class and name it something like HederaExamples. Import the following classes to use in your example:
Note: You may install the latest version of the Java SDKhere.
Install the JavaScript SDK with your favorite package manager npm or yarn by running the following command:
//InstallHedera's JS SDK with NPMnpm install --save @hashgraph/sdk// Install with Yarnyarn add @hashgraph/sdk
Install dotenv with your favorite package manager. This will allow our node environment to use your testnet account ID and the private key we will store in a .env file next.
Create a index.js file by running the following command:
touchindex.js
Your project structure should look something like this:
Create a hedera_examples.go file in hedera-go-examples root directory. You will write all of your code in this file.
touchhedera_examples.go
Create the Go "module" file by running the below command. The go.mod file defines the module's properties and dependencies and provides a way to manage versioning for Go projects.
Note: Testnet HBAR is required for this next step. Please follow the instructions to create a Hedera account on theportalbefore you move on to the next step.
Step 3: Create your .env File
Create the .env file in your project's root directory. The .env file stores your environment variables, such as your account ID and private key.
📣 Note: If you have not created an account, please do soherebefore this step.
If you created your testnet account through the developer portal, grab the Hedera Testnet account ID and DER-encoded private key from your Hedera portal profile (see screenshot below) and assign them to the MY_ACCOUNT_ID and MY_PRIVATE_KEY environment variables in your .env file:
Alternatively, if you used the faucet to create a testnet account, grab your faucet account ID and the private key (how to export a private key from MetaMask here) and assign them to the MY_ACCOUNT_ID and MY_PRIVATE_KEY environment variables in your .env file:
Next, you will load your account ID and private key variables from the .env file created in the previous step.
Within the main method, add your testnet account ID and private key from the environment file.
HederaExamples.java
publicclassHederaExamples {publicstaticvoidmain(String[] args) {//Grab your Hedera Testnet account ID and private keyAccountId myAccountId =AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));PrivateKey myPrivateKey =PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY")); }}
index.js
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar, TransferTransaction } = require("@hashgraph/sdk");
require('dotenv').config();asyncfunctionenvironmentSetup() {//Grab your Hedera testnet account ID and private key from your .env fileconstmyAccountId=process.env.MY_ACCOUNT_ID;constmyPrivateKey=process.env.MY_PRIVATE_KEY;// If we weren't able to grab it, we should throw a new errorif (!myAccountId ||!myPrivateKey) {thrownewError("Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present"); }}environmentSetup();
hedera_examples.go
funcmain() {//Loads the .env file and throws an error if it cannot load the variables from that file correctly err := godotenv.Load(".env")if err !=nil {panic(fmt.Errorf("Unable to load environment variables from .env file. Error:\n%v\n", err)) }//Grab your testnet account ID and private key from the .env file myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))if err !=nil {panic(err) } myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))if err !=nil {panic(err) }//Print your testnet account ID and private key to the console to make sure there was no error fmt.Printf("The account ID is = %v\n", myAccountId) fmt.Printf("The private key is = %v\n", myPrivateKey)}
In your terminal, enter the following command to create your go.mod file. This module is used for tracking dependencies and is required.
go mod init hedera_examples.go
Run your code to see your testnet account ID and private key printed to the console.
go run hedera_examples.go
Step 4: Create your Hedera Testnet client
Create a Hedera Testnet client and set the operator information using the testnet account ID and private key for transaction and query fee authorization. The operator is the default account that will pay for the transaction and query fees in HBAR. You will need to sign the transaction or query with the private key of that account to authorize the payment. In this case, the operator ID is your testnet account ID, and the operator private key is the corresponding testnet account private key.
To avoid encountering the INSUFFICIENT_TX_FEE error while conducting transactions, you can adjust the maximum transaction fee limit through the .setDefaultMaxTransactionFee() method. Similarly, the maximum query payment can be adjusted using the .setDefaultMaxQueryPayment() method.
🚨 How to resolve the INSUFFIENT_TX_FEE error
To resolve this error, you must adjust the max transaction fee to a higher value suitable for your needs.
Here is a simple example addition to your code:
constmaxTransactionFee=newHbar(XX); // replace XX with desired fee in Hbar
In this example, you can set maxTransactionFee to any value greater than 5 HBAR (or 500,000,000 tinybars) to avoid the "INSUFFICIENT_TX_FEE" error for transactions greater than 5 HBAR. Please replace XX with the desired value.
To implement this new max transaction fee, you use the setDefaultMaxTransactionFee() method as shown below:
//Create your Hedera Testnet clientClient client =Client.forTestnet();//Set your account as the client's operatorclient.setOperator(myAccountId, myPrivateKey);//Set the default maximum transaction fee (in Hbar)client.setDefaultMaxTransactionFee(newHbar(100));//Set the maximum payment for queries (in Hbar)client.setDefaultMaxQueryPayment(newHbar(50));
//Create your Hedera Testnet clientconstclient=Client.forTestnet();//Set your account as the client's operatorclient.setOperator(myAccountId, myPrivateKey);//Set the default maximum transaction fee (in Hbar)client.setDefaultMaxTransactionFee(newHbar(100));//Set the maximum payment for queries (in Hbar)client.setDefaultMaxQueryPayment(newHbar(50));
//Create your testnet clientclient := hedera.ClientForTestnet()client.SetOperator(myAccountId, myPrivateKey)// Set default max transaction feeclient.SetDefaultMaxTransactionFee(hedera.HbarFrom(100, hedera.HbarUnits.Hbar))// Set max query paymentclient.SetDefaultMaxQueryPayment(hedera.HbarFrom(50, hedera.HbarUnits.Hbar))
Your project environment is now set up to submit transactions and queries to the Hedera test network successfully!
importcom.hedera.hashgraph.sdk.Hbar;importcom.hedera.hashgraph.sdk.Client;importio.github.cdimascio.dotenv.Dotenv;importcom.hedera.hashgraph.sdk.AccountId;importcom.hedera.hashgraph.sdk.PublicKey;importcom.hedera.hashgraph.sdk.PrivateKey;importcom.hedera.hashgraph.sdk.AccountBalance;importcom.hedera.hashgraph.sdk.AccountBalanceQuery;importcom.hedera.hashgraph.sdk.TransferTransaction;importcom.hedera.hashgraph.sdk.TransactionResponse;importcom.hedera.hashgraph.sdk.ReceiptStatusException;importcom.hedera.hashgraph.sdk.PrecheckStatusException;importcom.hedera.hashgraph.sdk.AccountCreateTransaction;importjava.util.concurrent.TimeoutException;publicclassHederaExamples {publicstaticvoidmain(String[] args) {//Grab your Hedera Testnet account ID and private keyAccountId myAccountId =AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));+PrivateKey myPrivateKey =PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));//Create your Hedera Testnet clientClient client =Client.forTestnet();client.setOperator(myAccountId, myPrivateKey);// Set default max transaction fee & max query paymentclient.setDefaultMaxTransactionFee(newHbar(100)); client.setDefaultMaxQueryPayment(newHbar(50)); System.out.println("Client setup complete."); }}
JavaScript
index.js
const {Hbar,Client,} =require("@hashgraph/sdk");require("dotenv").config();asyncfunctionenvironmentSetup() {//Grab your Hedera testnet account ID and private key from your .env fileconstmyAccountId=process.env.MY_ACCOUNT_ID;constmyPrivateKey=process.env.MY_PRIVATE_KEY;// If we weren't able to grab it, we should throw a new errorif (!myAccountId ||!myPrivateKey) {thrownewError("Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present" ); }//Create your Hedera Testnet clientconstclient=Client.forTestnet();//Set your account as the client's operatorclient.setOperator(myAccountId, myPrivateKey);//Set the default maximum transaction fee (in Hbar)client.setDefaultMaxTransactionFee(newHbar(100));//Set the maximum payment for queries (in Hbar)client.setDefaultMaxQueryPayment(newHbar(50));console.log("Client setup complete.");}environmentSetup();
Go
hedera_examples.go
packagemainimport ("fmt""os""github.com/hashgraph/hedera-sdk-go/v2""github.com/joho/godotenv")funcmain() {//Loads the .env file and throws an error if it cannot load the variables from that file correctly err := godotenv.Load(".env")if err !=nil {panic(fmt.Errorf("Unable to load environment variables from .env file. Error:\n%v\n", err)) }//Grab your testnet account ID and private key from the .env file myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))if err !=nil {panic(err) } myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))if err !=nil {panic(err) }//Create your testnet client client := hedera.ClientForTestnet() client.SetOperator(myAccountId, myPrivateKey)// Set default max transaction fee & max query payment client.SetDefaultMaxTransactionFee(hedera.HbarFrom(100, hedera.HbarUnits.Hbar)) client.SetDefaultMaxQueryPayment(hedera.HbarFrom(50, hedera.HbarUnits.Hbar)) fmt.Println(“Client setup complete.”)}