DEV Community

Cooper Kunz
Cooper Kunz

Posted on

Easily send cryptocurrency with JavaScript!

Background

Hedera is a public network built on the lightning-fast hasghraph consensus algorithm. You can build apps and services on Hedera like you would a blockchain; send cryptocurrency, run smart contracts, even store files! Uniquely, Hedera is run by a few of the leading companies in the world, including Google and IBM.

Hedera's use of hashgraph allows it to be a high throughput alternative to blockchain, currently throttled to allow over 10,000 cryptocurrency transfers per second, compared to 10-20 for other networks like Ethereum. In this post, I'll show you just how easy it is to get started building on the Hedera network with Node.js and the Hedera JavaScript SDK!

Step 1: Create an Account

In order to use the Hedera Public Testnet, you'll need an Account. You can easily sign up on portal.hedera.com.

Step 2: Set up node.js environment

In this simple example, we'll create the bare minimum node.js environment we're going to need.

2.1. Create a new directory for our example & move into it.

mkdir hello-hedera-js-sdk && cd hello-hedera-js-sdk

2.2. Initialize a node.js project in this new directory.

npm init

Note: you can just say "yes" to all of the defaults and/or plugin what makes sense. It's an example!

Here's mine for reference.

{
  "name": "hello-hedera-js-sdk",
  "version": "1.0.0",
  "description": "A hello world project for the Hedera JavaScript SDK",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Cooper Kunz",
  "license": "Apache-2.0"
}

2.3. Switch environments, and open your directory.

You can do this in just about any text editor. I personally really like VS Code if you haven't checked it out recently!But you could also do this in vim, or anywhere you prefer.

2.4. Create an index.js file in the 'root' of your directory.

You can just add this one line to the file, so we can make sure you have node configured properly. console.log("hello node.js!");

2.5. Test out your node.js installation.

Switch environments back over to your terminal. You should be able to run node -v to get your current version. Presuming you're all setup with node, running node index.js should output hello node.js!. If you don't get an appropriate response, you may need to install node.

Step 3: Install the Hedera Hashgraph JS SDK

Now that you have your node environment setup, we can get started with the official Hedera JavaScript SDK!

Install it with your favorite package manager.

// install Hedera's JS SDK with NPM
npm install --save @hashgraph/sdk

// Install with Yarn
yarn add @hashgraph/sdk

Step 4: Finally, the fun part

Update your index.js with the following example for sending Hedera's native cryptocurrency, hbar. If you don't yet have a Hedera Testnet Account, w/ Account ID + private key, sign up.

// Allow access to our .env
require("dotenv").config();

// Import the modules we need from the Hedera Hashgraph JS SDK
const { Client, CryptoTransferTransaction } = require("@hashgraph/sdk");

// Create our connection to the Hedera public test network
// The Hedera JS SDK makes this reallyyy easy!
const client = Client.forTestnet()
client.setOperator("YOUR_ACCOUNT_ID", "YOUR_PRIVATE_KEY");

(async function() {

    const transactionId = await new CryptoTransferTransaction()
    .addSender("YOUR_ACCOUNT_ID", 1) // sends 1 "tinybar"
    .addRecipient("0.0.3", 1) // to another testnet account!
    .execute(client); // signed and paid for by our operator

    const receipt = await transactionId.getReceipt(client);
    console.log("Transaction receipt:", receipt);

}());

Save and run your updated index.js file by running node index.js - if successful, you should see your transaction receipt!

Congratulations on finishing the tutorial! You have now:

  • Created a Hedera Testnet Account
  • Setup the Hedera JS SDK in a node environment
  • Sent your first cryptocurrency transfer with hbar!

Have issues or questions? Let me know in the comments 👇

Top comments (2)

Collapse
 
mudlabs profile image
Sam

CryptoTransferTransaction is undefined.

Collapse
 
mudlabs profile image
Sam

Perhaps this is based on an old SDK. For anyone looking the current (10-11-21) startup tutorial is here: docs.hedera.com/guides/getting-sta...

Also if you follow the tutorial and get the INVALID_TRANSACTION_START error, check you local clock is on the current UTC time. Mine needed to be reset and then it worked.