Hedera
Search…
Environment Set-up
In this section you will complete the following:
    Create your project directory
    Create a .env file and store your Hedera testnet account ID and private key
    Set-up your Hedera testnet client
Note: Please follow the example in the version of the SDK you are using. The examples may not be compatible if you are using a different version than what is listed.

Pre-requisites:

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:
build.gradle <v2.0.3>
build.gradle <v1.3.3>
1
dependencies {
2
3
implementation 'com.hedera.hashgraph:sdk:2.0.3'
4
implementation 'io.grpc:grpc-netty-shaded:1.35.0'
5
compile 'io.github.cdimascio:java-dotenv:5.2.1' // Module that allows you to access environment variables from a .env file
6
}
Copied!
1
dependencies {
2
3
implementation 'com.hedera.hashgraph:sdk:1.3.3'
4
implementation 'io.grpc:grpc-netty-shaded:1.24.0'
5
compile 'io.github.cdimascio:java-dotenv:5.2.1' // Module that stores your environment variables from a .env file
6
}
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.
.env
1
MY_ACCOUNT_ID= TESTNET ACCOUNT ID (0.0.x)
2
MY_PRIVATE_KEY= TESTNET PRIVATE KEY (302e...)
Copied!

Step 3: Create a new class

Create a new java class and title it something like HederaExamples. Within the main method, grab your testnet account ID and private key from the .env file.
v2.0
v1.0
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
}
10
}
Copied!
1
public class HederaExamples {
2
3
public static void main(String[] args) throws InterruptedException, HederaStatusException {
4
5
//Grab your Hedera testnet account ID and private key
6
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
7
Ed25519PrivateKey myPrivateKey = Ed25519PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
8
}
9
}
Copied!

Step 4: Create your Hedera testnet client

You will need to create a testnet account so that you can submit transaction and query requests. 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 this case the operator ID is your testnet account ID and the operator private key is the corresponding testnet account private key.
v2.0
v1.0
1
//Create your Hedera testnet client
2
Client client = Client.forTestnet();
3
client.setOperator(myAccountId, myPrivateKey);
Copied!
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.

Code Check

What your code should look like at this point:
v2.0
v1.0
HederaExamples.java
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;
5
6
public class HederaExamples {
7
8
public static void main(String[] args) {
9
10
//Grab your Hedera testnet account ID and private key
11
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
12
PrivateKey myPrivateKey = PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
13
14
//Create your Hedera testnet client
15
Client client = Client.forTestnet();
16
client.setOperator(myAccountId, myPrivateKey);
17
}
18
}
Copied!
HederaExamples.java
1
import com.hedera.hashgraph.sdk.Client;
2
import com.hedera.hashgraph.sdk.HederaStatusException;
3
import com.hedera.hashgraph.sdk.account.AccountId;
4
import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PublicKey;
5
import com.hedera.hashgraph.sdk.crypto.ed25519.Ed25519PrivateKey;
6
import io.github.cdimascio.dotenv.Dotenv;
7
8
public class HederaExamples {
9
10
public static void main(String[] args) throws InterruptedException, HederaStatusException {
11
12
//Grab your Hedera testnet account ID and private key
13
AccountId myAccountId = AccountId.fromString(Dotenv.load().get("MY_ACCOUNT_ID"));
14
Ed25519PrivateKey myPrivateKey = Ed25519PrivateKey.fromString(Dotenv.load().get("MY_PRIVATE_KEY"));
15
16
//Create your Hedera testnet client
17
Client client = Client.forTestnet();
18
client.setOperator(myAccountId, myPrivateKey)
19
}
20
}
Copied!
Last modified 12d ago