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
Open your terminal and create a directory called hello-hedera-js-sdk. After you create the project directory navigate into the directory.
mkdir hello-hedera-js-sdk && cd hello-hedera-js-sdk
npm init
Note: you can just say “yes” to all of the defaults and/or plugin what makes sense. It’s an example!
{"name": "hello-hedera-js-sdk","version": "","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "","license": "ISC"}
Note: Please follow the example in the version of the SDK you are using. Code specific to v2.0 of the SDK will be marked in code block tab title, otherwise it works for both v2.0 and v1.0. The examples may may be not be compatible if you are using a different version than what is listed.
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.
// install Hedera's JS SDK with NPM// This example uses Hedera JavaScript SDK v2.0.13npm install --save @hashgraph/[email protected]// Install with Yarnyarn add @hashgraph/[email protected]
// install Hedera's JS SDK with NPM// This example uses Hedera JavaScript SDK v1.4.4npm install --save @hashgraph/[email protected]// Install with Yarnyarn add @hashgraph/[email protected]
Install dotenv
with your favorite package manager. This will allow our node environment to use your testnet account ID and private key that we will store in a .env file next.
// install with NPMnpm install dotenv// Install with Yarnyarn add dotenv
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.
MY_ACCOUNT_ID= TESTNET ACCOUNT ID (0.0.x)MY_PRIVATE_KEY= TESTNET PRIVATE KEY (302e...)
This file will contain the code we will right 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.
const { Client } = require("@hashgraph/sdk");require("dotenv").config();async function main() {//Grab your Hedera testnet account ID and private key from your .env fileconst myAccountId = process.env.MY_ACCOUNT_ID;const myPrivateKey = process.env.MY_PRIVATE_KEY;// If we weren't able to grab it, we should throw a new errorif (myAccountId == null ||myPrivateKey == null ) {throw new Error("Environment variables myAccountId and myPrivateKey must be present");}}main();
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.
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. You will need to sign the transaction or query with the private key of the account to authorize the payment.
index.js// Create our connection to the Hedera network// The Hedera JS SDK makes this reallyyy easy!const client = Client.forTestnet();client.setOperator(myAccountId, myPrivateKey);
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 a Hedera testnet account.
What your index.js file should look like at this point:
index.jsconst { Client } = require("@hashgraph/sdk");require("dotenv").config();async function main() {//Grab your Hedera testnet account ID and private key from your .env fileconst myAccountId = process.env.MY_ACCOUNT_ID;const myPrivateKey = process.env.MY_PRIVATE_KEY;// If we weren't able to grab it, we should throw a new errorif (myAccountId == null ||myPrivateKey == null ) {throw new Error("Environment variables myAccountId and myPrivateKey must be present");}// Create our connection to the Hedera network// The Hedera JS SDK makes this reallyyy easy!const client = Client.forTestnet();client.setOperator(myAccountId, myPrivateKey);}main();