Updated on 2023-08-04 GMT+08:00

Using Ethers.js to Send JSON-RPC API Requests

Request example:

const ethers = require('ethers');
const url = 'https://your-http-endpoint/your-credential'
const provider = new ethers.providers.JsonRpcProvider(url)
provider.getBlockNumber((error, blockNumber) => {
	if(!error) {
		console.log(blockNumber);
	} else {
		console.log(error);
	}
});

Response example:

{
"jsonrpc": "2.0",
"id": 1,
"result": "00f3c34b"
}