Web3.js is a set of libraries that allow developers to interact with Ethereum nodes using HTTP, IPC or WebSocket protocols. Harmony has an Ethereum-like API available that is fully compatible with Ethereum-style JSON RPC invocations. Therefore, developers can leverage this compatibility and use the web3.js library to interact with a Harmony node as if they were doing so on Ethereum.
Setup Web3.js with Harmony
To get started with the web3.js library, we first need to install it using the following command:
1
npm install web3
Copied!
Once done, the simplest setup to start using the library and its methods is the following:
1
const Web3 =require('web3');
2
3
//Create web3 instance
4
const web3 =newWeb3(HMY_RPC_URL);
Copied!
Depending on which network you want to connect to, you can set the HMY_RPC_URL to the following values:
For Mainnet:
1
HMY_RPC_URL= https://api.s0.t.hmny.io
Copied!
For Testnet:
1
HMY_RPC_URL= https://api.s0.b.hmny.io
Copied!
Tutorials
In the case that you are interested in a more detailed step-by-step guide, you can go to our specific tutorials on using web3.js on a Harmony: