Easily send cryptocurrency with JavaScript!
Tags: javascript, node, blockchain, hashgraph
Published: 2020-04-01 17:37:33

---
title: Easily send cryptocurrency with JavaScript!
published: true
description: A short tutorial, demonstrating how easy it is to send cryptocurrency with JavaScript on the Hedera Hashgraph network.
tags: JavaScript, Node, Blockchain, Hashgraph
---
## Background

[Hedera](https://hedera.com/) is a public network built on the lightning-fast [hasghraph consensus algorithm](https://www.hedera.com/learning/what-is-hashgraph-consensus). 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](https://cloud.google.com/blog/products/gcp/working-with-hedera-hashgraph-to-support-the-next-generation-of-distributed-ledger-technology) and [IBM](https://www.hedera.com/blog/ibm-and-tata-communications-join-hedera-governing-council).

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](https://ethereum.org/). In this post, I'll show you just how easy it is to get started building on the [Hedera](https://hedera.com/) network with Node.js and the [Hedera JavaScript SDK](https://github.com/hashgraph/hedera-sdk-js)!

## 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](https://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.

```javascript
{
  "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](https://code.visualstudio.com/) 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](https://nodejs.org/en/download/).

## 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](https://github.com/hashgraph/hedera-sdk-js)! 

Install it with your favorite package manager.

```javascript
// 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](https://hedera.com/hbar). If you don't yet have a Hedera Testnet Account, w/ Account ID + private key, [sign up](https://portal.hedera.com/).

```javascript
// 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](https://hedera.com/hbar)!

<hr/>

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