Connect to the Network
How to connect with to VeChainThor blockchain using @vechain/sdk-network
Interacting with VeChainThor requires only an instance of ThorClient
that is configured for the relevant network. Once the connection is established, this instance can be utilized to interact in an asynchronous manner. Below is a snippet that demonstrates importing the library and initializing a client:
import { ThorClient } from "@vechain/sdk-network";
const thor = ThorClient.at("https://mainnet.vechain.org");
The snippet connects to the MainNet, where all production related activity is found.
Additionally there is a TestNet available for testing and development purposes. This allows developers to experiment without risking real assets.
To connect to the TestNet use https://testnet.vechain.org
as URL. If you have deployed a thor solo node, it is normally available on http://localhost:8669
.
To test the connectivity we'll get request the first and last block from the chain and output their number & id:
// get the current latest & best block from the chain
const bestBlock = await thor.blocks.getBestBlockCompressed();
console.log("Best Block:", bestBlock.number, bestBlock.id);
// get the first and genesis block from the chain
const genesisBlock = await thor.blocks.getBlockCompressed(0);
console.log("Genesis Block:", genesisBlock.number, genesisBlock.id);
You can test the above snippet here:
Last updated
Was this helpful?