First Transaction

This basic guide will show you how to setup your environment and send your first 0 value transaction on the IOTA Public Devnet. Setting up a transaction with value is essentially the same but requires a seed with tokens in it.

Requirements:

  • Node.js 8+
  • Node Package Manager (NPM)
  • Code editor
  • Terminal Access
  • An Internet Connection

To start we need to create a working directory:
mkdir iota-example && cd iota-example
Once in our directory we can use Node Package Manager (NPM) to fetch and install the required Node.js libraries for IOTA to the folder:
npm install @iota/core @iota/converter --save
This will result in a package.json file and a node_modules folder being created in your directory. After this we will create a new file called index.js in the current folder with the following contents:
After saving the file you can run this code using this command:
node index.js
This should result in the following output:

Output of our getNodeInfo call

Now that we've verified that the code is working and returning general information about the node, we can create a transaction on the network.
At the bottom of the index.js file we will add a transaction to the Devnet with the following message: Hello World!
To achieve this the following code needs to be appended:
const address =
  'HELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDD'
const seed =
  'PUEOTSEITFEVEWCWBTSIZM9NKRGJEIMXTULBACGFRQK9IMGICLBKW9TTEVSDQMGWKBXPVCBMMCXWMNPDX'
const message = iota.utils.toTrytes('Hello World!')

const transfers = [
  {
    value: 0,
    address: address,
    message: message
  }
]

iota
  .prepareTransfers(seed, transfers)
  .then(trytes => iota.sendTrytes(trytes, 3, 9))
  .then(bundle => {
    console.log(bundle)
  })
  .catch(err => {
    // catch any errors
  })

Lets break this code down:
Trytes:
const address =
  'HELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDD'
const seed =
  'PUEOTSEITFEVEWCWBTSIZM9NKRGJEIMXTULBACGFRQK9IMGICLBKW9TTEVSDQMGWKBXPVCBMMCXWMNPDX'

We define address and seed here to be used in the prepareTransfer function. The seed here is random and contains no value.
Note: Seeds and Address are both comprised of Trytes and are 81 characters in length.
Message:
const message = Converter.asciiToTrytes('Hello World!')

This line create a constant that is the message Hello World and encodes it to the Trytes format that the IOTA network accepts.
Transfers:
const transfers = [
  {
    value: 0,
    address: address, // HELLOWORLDHELLOWORLDHELLO....WORLDD
    message: message
  }
]

This array lets you specify transfers you want to make from your Seed. In this case we want a transaction with Zero value, to our HELLOWORLD address with the message Hello World encoded in trytes.
Transfer Function:
iota
  .prepareTransfers(seed, transfers)
  .then(trytes => iota.sendTrytes(trytes, (depth = 3), (mwm = 9)))
  .then(bundle => {
    console.log(bundle)
  })
  .catch(err => {
    // catch any errors
  })

These functions invoke the library to do a number of steps to construct a transaction locally, via the prepareTransfers function, then send it too a Full Node, via the sendTrytes function, to be sent to other nodes on the network.
Lets take a second to figure out what the variables do:
KeyType?
Description
seedStringThis is the 'wallet' you are sending from. With 0 value transactions we do not need to have any tokens in the wallet. So this can be 81 random Trytes.
mwmNumberThis is the minimum weight magnitude (MWM) that specifies how mwm proof of work is required. On the Devnet, anything less than 9 is not going to be accepted
depthNumberThis specifies the number of bundles you will walk back and confirm.
transfersArrayThis array contains the value, address and message of your desired transaction. You can specify multiple transactions to different addresses.

Final Code

The resulting code should look like this:
Now if you run this code, you should see the Node's information logged to console but also you should see the transfer object you have just sent:

Output of a successful transaction

Congratulations 🎊. You've just sent your first IOTA transaction. Now, we just need to go retrieve it. This will be covered in the next article.