Hedera
Search…
Environment Set-up

Summary

In this section you will complete the following:
  • Create your project directory
  • Create a .env file and store your Hedera testnet account ID and keys
  • Set-up your Hedera testnet client

Pre-requisites:

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 community here or on the GitHub repository.
Java
JavaScript
Go

Step 1: Create a new Gradle project in your favorite IDE

Open your favorite IDE and create a new gradle project. Add the following dependencies to your build.gradle file. You may choose to install the latest version of the SDK here.
​
build.gradle
1
dependencies {
2
​
3
implementation 'com.hedera.hashgraph:sdk:2.5.0'
4
implementation 'io.grpc:grpc-netty-shaded:1.40.0'
5
compile 'io.github.cdimascio:java-dotenv:5.2.1' // Module that stores your environment variables from a .env file
6
implementation 'org.slf4j:slf4j-nop:1.7.29'
7
implementation 'com.google.code.gson:gson:2.8.8'
8
9
}
Copied!
​

Step 2: Create a .env file in your project

Create a .env file in the root directory of your project. Grab the the Hedera testnet account ID and private key from your Hedera portal profile and enter them in the MY_ACCOUNT_ID and MY_PRIVATE_KEY fields.
​
1
MY_ACCOUNT_ID= ENTER TESTNET ACCOUNT ID
2
MY_PRIVATE_KEY= ENTER TESTNET PRIVATE KEY
Copied!

Step 3: Create a new class

Create a new java class and title it something like HederaExamples. Import the following classes to use in your example.
1
import com.hedera.hashgraph.sdk.AccountId;
2
import com.hedera.hashgraph.sdk.Client;
3
import com.hedera.hashgraph.sdk.PrivateKey;
4
import io.github.cdimascio.dotenv.Dotenv;
Copied!
​
Within the main method, grab your testnet account ID and private key from the .env file.
1
public class HederaExamples {
2
​
3
public static void main(String[] args) {
4
​
5
//Grab your Hedera testnet account ID and private key
6
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
7
PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
8
}
9
}
Copied!

Step 4: Create your Hedera testnet client

You have the option to create a client for the Hedera previewnet, testnet or mainnet. Since we are using a Hedera testnet account ID and private key, we will create a client for the Hedera testnet. You can view all the client configurations here.
After you create your Hedera testnet client, you will need to set the operator information. The operator is the 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.
1
//Create your Hedera testnet client
2
Client client = Client.forTestnet();
3
client.setOperator(myAccountId, myPrivateKey);
Copied!
The client has a default max transaction fee of 100,000,000 tinybars (1 hbar) and default max query payment of 100,000,000 tinybars (1 hbar). If you need to change these values, you can use.setMaxTransactionFee() for a transaction and .setMaxQueryPayment() for queries. You are only charged the actual cost of the transaction or query.
Your project environment is now set up to successfully submit transactions and queries to the Hedera test network!
Next, you will learn how to create an account. Click the link at the bottom to get started.

Code Check
βœ…

What your code should look like at this point:
1
HederaExamples.java
2
import com.hedera.hashgraph.sdk.AccountId;
3
import com.hedera.hashgraph.sdk.Client;
4
import com.hedera.hashgraph.sdk.PrivateKey;
5
import io.github.cdimascio.dotenv.Dotenv;
6
​
7
public class HederaExamples {
8
​
9
public static void main(String[] args) {
10
​
11
//Grab your Hedera testnet account ID and private key
12
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
13
PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
14
​
15
//Create your Hedera testnet client
16
Client client = Client.forTestnet();
17
client.setOperator(myAccountId, myPrivateKey);
18
​
19
}
20
}
Copied!

Step 1: Set up your node.js environment

Create a new directory for our sample & move into it

Open your terminal and create a directory called hello-hedera-js-sdk. After you create the project directory navigate into the directory.
1
mkdir hello-hedera-js-sdk && cd hello-hedera-js-sdk
Copied!

Initialize a node.js project in this new directory

1
npm init
Copied!
Note: you can just say β€œyes” to all of the defaults and/or plugin what makes sense. It’s an example!
1
{
2
"name": "hello-hedera-js-sdk",
3
"version": "",
4
"description": "",
5
"main": "index.js",
6
"scripts": {
7
"test": "echo \"Error: no test specified\" && exit 1"
8
},
9
"author": "",
10
"license": "ISC"
11
}
Copied!

Step 2: Install the Hedera JavaScript SDK

Now that you have your node environment setup, we can get started with Hedera’s JS SDK! You can open this project in your favorite text editor like Visual Studio Code.
  • Install it with your favorite package manager.
1
// install Hedera's JS SDK with NPM
2
npm install --save @hashgraph/sdk
3
​
4
// Install with Yarn
5
yarn add @hashgraph/sdk
Copied!
  • Install dotenv with your favorite package manager. This will allow our node environment to use your testnet account ID and the private key that we will store in a .env file next.
1
// install with NPM
2
npm install dotenv
3
​
4
// Install with Yarn
5
yarn add dotenv
Copied!

Step 3: Create a .env file in your project

The .env file will store your Hedera testnet account ID and private key. Create this file in the root directory of your project and save it as .env file.
Now you can add your testnet account ID and private key provided from your Hedera Portal Account.
1
MY_ACCOUNT_ID = ENTER YOUR ACCOUNT ID
2
MY_PRIVATE_KEY = ENTER YOUR PRIVATE KEY
Copied!

Step 4: Create an index.js file in the 'root' directory

This file will contain the code we will write in the following samples. Your project structure should look something like this after:
Grab your Hedera testnet account ID and private key from the .env file.
1
const { Client } = 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
main();
Copied!

Step 5: Create your Hedera testnet client

You have the option to create a client for the Hedera mainnet or testnet. Since we are using a Hedera testnet account ID and private key, we will create a client for the Hedera testnet. You can find all the client configurations here.
After you create your Hedera testnet client, you will need to set the operator information. The operator is the account that will pay for the transaction and query fees in hbar.
index.js
1
// Create our connection to the Hedera network
2
// The Hedera JS SDK makes this really easy!
3
const client = Client.forTestnet();
4
​
5
client.setOperator(myAccountId, myPrivateKey);
Copied!
The client has a default max transaction fee of 100,000,000 tinybars (1 hbar) and default max query payment of 100,000,000 tinybars (1 hbar). If you need to change these values, you can use.setMaxTransactionFee() for a transaction and .setMaxQueryPayment() for queries.
​
Your project environment is now set up to successfully submit transactions/queries to the Hedera test network!
Next, you will learn how to create an account. Click the link at the bottom to get started.

Code Check βœ…

What your index.js file should look like at this point:
index.js
1
const { Client } = 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
main();
Copied!

Step 1: Create your Go project

Open your terminal and create a project directory called something like hedera-go-examples to store your Go source code.
1
mkdir hedera-go-examples && cd hedera-go-examples
Copied!

Step 2: Create a .env file in your project

Open the project in your favorite IDE and create a .env file in the root directory of your project. Enter your Hedera testnet account ID and private key provided to you from your Hedera portal account.
1
MY_ACCOUNT_ID= ENTER TESTNET ACCOUNT ID
2
MY_PRIVATE_KEY= ENTER TESTNET PRIVATE KEY
Copied!

Step 3: Install the Hedera Go SDK

Create a hedera_examples.go file in hedera-go-examples directory. You will write all of your code in this file.
Import the following packages to your hedera_examples.go file:
1
package main
2
​
3
import (
4
"fmt"
5
"os"
6
​
7
"github.com/joho/godotenv"
8
"github.com/hashgraph/hedera-sdk-go/v2"
9
)
Copied!
Next, you will load your account ID and private key variables from the .env file created in the previous step.
1
package main
2
​
3
import (
4
"fmt"
5
"os"
6
​
7
"github.com/joho/godotenv"
8
"github.com/hashgraph/hedera-sdk-go/v2"
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
}
Copied!
In your terminal, enter the following command to create your go.mod file. This module is used for tracking dependencies and is required.
1
go mod init hedera_examples.go
Copied!
Run your code to see your testnet account ID and private key are printed to the console.
1
go run hedera_examples.go
Copied!

Step 4: Create your Hedera testnet client

You have the option to create a client for the Hedera previewnet, testnet, and mainnet. Since we are using a Hedera testnet account ID and private key, we will create a client for the Hedera testnet. This allows you to submit transactions and queries to the test network.
After you create your Hedera testnet client, you will need to set the operator account ID and private key. The operator is the default account that will pay for the transaction and query fees. The operator in this example will be your testnet account ID and private key.
1
//Create your testnet client
2
client := hedera.ClientForTestnet()
3
client.SetOperator(myAccountId, myPrivateKey)
Copied!
The client has a default max transaction fee of 100,000,000 tinybars (1 hbar) and default max query payment of 100,000,000 tinybars (1 hbar). If you need to change these values, you can use.setMaxTransactionFee() for transactions and .setMaxQueryPayment() for queries. You are only charged the actual cost of the transaction or query.
Your project environment is now set up to successfully submit transactions/queries to the Hedera test network!
Next, you will learn how to create a Hedera testnet account. Click the link at the bottom to get started.

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
}
Copied!
​
Copy link