Getting Started with Node.js
In a production setup, do not store passwords in the host's environment variables or in the source code. See our backup and security recommendations for production setups.
Requirements
- One of the following Node.js versions: '16.x', '18.x';
Install the IOTA SDK
Install Using a Package Manager
To start using the IOTA SDK in your Node.js project, you can install the IOTA SDK from your package manager of choice:
- npm
- Yarn
npm i @iota/sdk
yarn add @iota/sdk
Build the Binding from Source
Requirements
If you want to build the IOTA SDK from source, please ensure you have the installed the following:
- A supported version of Node and Rust.
- Python < 3.11
- Yarn v1
Windows
On Windows, you will also need LLVM. Our workflow uses
https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.1/LLVM-11.0.1-win64.exe
. You may also need to set
an environment variable RUSTFLAGS
to -C target-feature=+crt-static
.
If you use Chocolatey you can install LLVM by executing choco install llvm
Build
You can build the IOTA SDK from source by doing the following:
- Download the repository to any directory you choose:
git clone https://github.com/iotaledger/iota-sdk
- Move to the Node.js binding directory:
cd iota-sdk/bindings/nodejs
- Install the necessary dependencies with either of the following commands:
- npm
- Yarn
npm install
yarn install
- Build the SDK:
- npm
- Yarn
npm run build
yarn build
This command uses the cargo-cp-artifact utility to run the Rust
build and copy the built library into ./build/Release/index.node
.
Prebuild requires that the binary is in build/Release
as though it was built with node-gyp.
Usage
Client
After you installed the library, you can create a Client
instance and interface with it.
const { Client, initLogger } = require('@iota/sdk');
async function run() {
initLogger();
const client = new Client({
nodes: ['https://api.testnet.shimmer.network'],
localPow: true,
});
try {
const nodeInfo = await client.getInfo();
console.log('Node info: ', nodeInfo);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());
Wallet
After you installed the library, you can create a Wallet
instance and interact with it.
import { Wallet, CoinType, WalletOptions } from '@iota/sdk';
const walletOptions: WalletOptions = {
storagePath: `Alice`, // A name to associate with the created account.
clientOptions: {
nodes: ['https://api.testnet.shimmer.network'], // The node to connect to.
},
coinType: CoinType.Shimmer,
secretManager: {
// Setup Stronghold secret manager
stronghold: {
snapshotPath: 'vault.stronghold', // The path to store the account snapshot.
password: 'a-secure-password', // A password to encrypt the stored data. WARNING: Never hardcode passwords in production code.
},
},
};
const wallet = new Wallet(walletOptions);
What's next?
How-To Guides
Once you have installed the IOTA SDK, you can start building your application. You can find usage examples in this Wiki's how-to guides.
More Examples
You can use the provided code examples to get acquainted with the IOTA SDK. You can use the following commands to run any example:
cd examples
yarn
yarn run-example ./[example folder]/[example file]
- Where
[example file]
is the file name from the example folder. For example:
yarn run-example examples/client/00_get_info.ts