Logo

dev-resources.site

for different kinds of informations.

Easily send cryptocurrency with JavaScript!

Published at
4/1/2020
Categories
javascript
node
blockchain
hashgraph
Author
cooper_kunz
Author
11 person written this
cooper_kunz
open
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 👇

hashgraph Article's
28 articles in total
Favicon
Join Us at HederaCon 2025!
Favicon
Hedera Ecosystem Leadership Changes
Favicon
EQTY Lab’s Verifiable Compute Brings Trust to AI with Hedera
Favicon
Welcoming Shyam Nagarajan as Hedera's Chief Operating Officer
Favicon
Hedera Adopts the Chainlink Data Standard To Accelerate DeFi and Tokenized RWA Adoption
Favicon
Hedera Enhances Network Management With the Dynamic Address Book
Favicon
Are Ed25519 Keys Quantum-Resistant? Exploring the Future of Cryptography
Favicon
Mainnet v0.54 Release
Favicon
The Nairobi Securities Exchange (NSE) Joins the Hedera Council to Expedite Tokenization of Securities in Kenya’s Capital Markets
Favicon
Prove AI Launches on the Hedera Network: Bringing a New Standard in AI Governance
Favicon
Introducing HIP-904: Frictionless Airdrops
Favicon
These are the Winners of the 2024 #HelloFuture Hackathon
Favicon
Introducing Hedera Asset Tokenization Studio
Favicon
Introducing HIP-850: Enhancing Supply Key Functionality for NFT Updates in Treasury Account
Favicon
Consensus 2024: BAF University Summit Recap
Favicon
TOKEN2049 Recap
Favicon
Changes to the Hedera-Operated Mirror Node
Favicon
Introducing Hiero: The Foundation of the Future
Favicon
Getting Started with Hedera Hashgraph with OpenZepplin
Favicon
What is a Hashgraph, & how is it different from a blockchain?
Favicon
How does Hederas Hashgraph consensus improve things & what are its use cases?
Favicon
Why is the voting weight tipped when 1/3 of Hbar is controlled by one entity?
Favicon
What do the Hedera Hashgraph council do? And who are all the current members in 2021?
Favicon
Hashgraph Vs Blockchain- Top 7 Differences That You Must Know
Favicon
Vue.js demo app for trading emojis
Favicon
Easily send cryptocurrency with JavaScript!
Favicon
Educational resources for "Web 3.0"
Favicon
Fungible vs. non-fungible blockchain tokens

Featured ones: