About ◈ Prerequisites ◈ Installation ◈ Getting started ◈ API reference ◈ Examples ◈ Supporting the project ◈ Joining the discussion
This is the official JavaScript client library, which allows you to do the following:
- Create transactions
- Sign transactions
- Generate addresses
- Interact with an IRI node
This is beta software, so there may be performance and stability issues. Please report any issues in our issue tracker.
To use the library, your computer must have one of the following supported versions of Node.js:
- Node.js 10 or higher. Recommended version is latest LTS.
- Node.js 8
To install library packages, your computer must have one of the following package managers:
A package.json
file is required. It can be generated with npm init
or yarn init
To install the IOTA JavaScript client library and its dependencies, you can use one of the following options:
- Install the library with npm
npm install @iota/core
- Install the library with Yarn
yarn add @iota/core
After you've installed the library, you can connect to an IRI and interface with it.
An extended guide can be found on our documentation portal, we strongly recommend you to go here for starting off. A quick starting tutorial is shown below.
To connect to a local IRI node, do the following:
import { composeAPI } from '@iota/core'
const iota = composeAPI({
provider: 'http://localhost:14265'
})
iota.getNodeInfo()
.then(info => console.log(info))
.catch(error => {
console.log(`Request error: ${error.message}`)
})
For details on all available API methods, see the reference page.
-
.attachToTangle(trunkTransaction, branchTransaction, minWeightMagnitude, trytes, [callback])
-
.promoteTransaction(tail, depth, minWeightMagnitude, transfer, [options], [callback])
-
.sendTrytes(trytes, depth, minWeightMagnitude, [reference], [callback])
We have a list of test cases in the examples
directory that you can use as a reference when developing apps with IOTA.
Here's how you could send a zero-value transaction and read the message in it from the Tangle, using the library. For the guides, see the documentation portal.
const Iota = require('@iota/core');
const Converter = require('@iota/converter');
const Extract = require('@iota/extract-json')
// Connect to a node
const iota = Iota.composeAPI({
provider: 'https://nodes.devnet.thetangle.org:443'
});
const depth = 3;
const minimumWeightMagnitude = 9;
// Define a seed and an address.
// These do not need to belong to anyone or have IOTA tokens.
// They must only contain a mamximum of 81 trytes
// or 90 trytes with a valid checksum
const address =
'HEQLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWORLDHELLOWOR99D';
const seed =
'PUEOTSEITFEVEWCWBTSIZM9NKRGJEIMXTULBACGFRQK9IMGICLBKW9TTEVSDQMGWKBXPVCBMMCXWMNPDX';
// Define a JSON message to send.
// This message must include only ASCII characters.
const message = JSON.stringify({"message": "Hello world"});
// Convert the message to trytes
const messageInTrytes = Converter.asciiToTrytes(message);
// Define a zero-value transaction object
// that sends the message to the address
const transfers = [
{
value: 0,
address: address,
message: messageInTrytes
}
];
// Create a bundle from the `transfers` array
// and send the transaction to the node
iota
.prepareTransfers(seed, transfers)
.then(trytes => iota.sendTrytes(trytes, depth, minimumWeightMagnitude))
.then(bundle => {
// The message can be read from the Tangle, using the tail transaction hash
const tailTransactionHash = bundle[0].hash; // NOLIVEQNRHIIRPDPHYTGJOVSGOUXVAACDNAPNTTRFNNCVNJMDZFPURTDNVTAKHPSLSJRYZGQHYBBAE999
console.log(tailTransactionHash);
// Get the tail transaction's bundle from the Tangle
return iota.getBundle(tailTransactionHash)
.then(bundle => {
// Get your hello world message from the transaction's `signatureMessageFragment` field and print it to the console
console.log(JSON.parse(Extract.extractJson(bundle)));
})
})
.catch(err => {
console.error(err)
});
If the IOTA JavaScript client library has been useful to you and you feel like contributing, consider posting a bug report, feature request or a pull request.
See the contributing guidelines for more information.
-
Click the Fork button in the top-right corner
-
Clone your fork and change directory into it
-
Bootstrap your environment by doing the following:
npm run init
This step will download all dependencies, build and link the packages together. iota.js uses Lerna to manage multiple packages. You can re-bootstrap your setup at any point with lerna bootstrap
command.
Make your changes on a single package or across multiple packages and test the system by running the following from the root directory:
npm test
To run tests of specific package, change directory into the package's directory and run npm test
from there.
Please update the documention when needed by editing JSDoc
annotations and running npm run docs
from the root directory.
If you want to get involved in the community, need help with getting setup, have any issues related with the library or just want to discuss IOTA, Distributed Registry Technology (DRT) and IoT with other people, feel free to join our Discord.