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.lib.js @iota/curl-remote

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:

// Require the use of IOTA library
const IOTA = require("iota.lib.js")
// Create a new instance of the IOTA class object.
// Use 'provider' variable to specify which Full Node to talk to
const iota = new IOTA({provider: "https://nodes.testnet.iota.org:443"})
// Call the 'getNodeInfo call to check that the node is working
iota.api.getNodeInfo((error, success) => {
if (error) {
console.log(error)
} else {
console.log(success)
}
})

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 testnet with the following message: Hello World!

We define trytes here to be used in two places, one to feed into the sendTransfer function as a seed and second to use as the address where we will send the message. We can pick a random seed because we are not attempting to send value.

Note: Seeds and Address are both comprised of Trytes and are 81 characters in length.

Message:

const message = iota.utils.toTrytes('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.

The error attachToTangle is not available is a common error on Public IOTA nodes.

This is because this function uses heavy CPU load to compute the Proof of Work for the transaction. If node operators left this open people could abuse the function and slow down the node significantly.

So will we have publish a transaction without using the attachToTanglecommand of the node. This is achieved by overriding the behaviour of the library with a PoW service. There are three to choose from:

ccurl.interface.js - This is for node.js environments and allows for PoW to be done locally.

curl.lib.js - This is for WebGL 2 enabled browsers to compute PoW using the GPU of the user's computer

@iota/curl-remote - This is used to mimic a remote node handling the PoW. In reality this is a standalone service built to provide Pow-as-a-Service.

We will use the public Sandbox on the IOTA Testnet.

We must add the following code into index.jsafter we initialise the IOTA library but before we do our sendTransfer call.