npm init -y
npm i @hashgraph/sdk
Next, we'll set up our environment by defining which Hedera network and nodes we would like to send our transactions to, as well as the account that is going to be operating this connection.
To start, you'll likely want to use the public Hedera testnet, a free development environment that uses test hbars (or as I like to call them, testbars). If you don't have a testnet account, you can sign up for one at the Hedera Portal. In doing so, you'll receive a testnet Account ID and the associated public/private key pairing.
npm i dotenv
To access the testnet, we'll first create a .env file that will hold our account credentials. Within that .env file, add your information like below:
Now that we've setup our Node.js project, SDK, and environment, we want to use those credentials to sign transactions that we send to the testnet. Let's create an index.js file where we'll establish a client connection.
// allow us to grab our .env variables
// import the 'Client' module from the Hedera JS SDK
Let's test out this client to make sure it was successfully setup by sending our first hbar cryptocurrency transfer! For this, we'll want to import the `CryptoTransferTransaction` from the Hedera JS SDK, similar to other transactions, queries, or modules. You can add this as the next imported module after the Client, at the top of your file.
Then we'll create and use that `.CryptoTransferTransaction()` within the '.main()' function we previously created. A lot of the details for this transaction are handled by the SDK, like managing transaction fees, but we'll still have to specify a few details. In this case, we'll send 1 hbar from our account, to account 0.0.3, which is actually one of the testnet nodes. But this could be any account on the public testnet! Maybe you could even try sending some to mine (0.0.142293)?
/* imports, client config, etc. above ... */
const transactionId = await new CryptoTransferTransaction()
const receipt = transactionId.getReceipt(client);
} /* Close of the main() function */
main(); // make sure to call your main function
There’s a fair amount of other things going on in this transaction that the SDK doesn't manage or populate by default, so let me explain.
.setTransactionMemo() allows us to add messages into our transfers.
.execute() will generate and sign the new transaction, and additionally submit it to a node that is specified in your client connection. If you didn’t specify a specific node for this transaction to be submitted to, the SDKs will pick one from the address book at random.
.getReceipt() will ask the network if there’s a receipt for this specific transaction ID (which was generated after the transaction executed). Receipts and records, by default, only exist on the network for ~4 minutes. After this duration, they will be removed from the network, and persisted to any Mirror Nodes that are listening.
The full JS SDK reference documentation can be found here.
If you have successfully followed along, you should be able to run `node index.js` in your terminal. If your hbar transfer was successful, receive a receipt that looks similar to this:
With that you’ve successfully set up your Node.js environment for Hedera development, and can start moving onto using other Hedera network services. In our next part, we’ll cover creating your first Hedera Consensus Service Topic and sending HCS messages!