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.fromUrl("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 solo node of VeChain, it is normally available on http://localhost:8669.

The ThorClient uses a HttpClient underneath to communicate with the VeChain nodes with a JSON API.

The default HttpClient is an Axios implementation and can be replaced with any custom implementation as long as the interfaces are met. Example use cases for custom implementations can be custom headers to access protected nodes.

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:

The ThorClient provides a multitude of properties with access to all relevant functionality on VeChain. You can find examples and details about all of them in the Thor Client docs.

Last updated