net_peerCount - Optimism
Example code for the net_peerCount json-rpc method. Сomplete guide on how to use net_peerCount json-rpc in GetBlock.io Web3 documentation.
This method returns the number of peers the node is connected to in the P2P network. A higher peer count generally indicates better network connectivity and reliability. This is useful for monitoring node health.
Parameters
None
Request Sample
curl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "net_peerCount",
"params": [],
"id": "getblock.io"
}'const axios = require('axios');
const url = 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/';
const payload = {
jsonrpc: '2.0',
method: 'net_peerCount',
params: [],
id: 'getblock.io'
};
axios.post(url, payload, {
headers: { 'Content-Type': 'application/json' }
})
.then(response => {
const peerCount = parseInt(response.data.result, 16);
console.log('Peer Count:', peerCount);
})
.catch(error => console.error(error));Response
A successful response returns the following:
{
"jsonrpc": "2.0",
"id": "getblock.io",
"result": "0x12d"
}Response Parameters
id: A unique request identifier, matching theidsent in the request body.jsonrpc: Specifies the use of JSON-RPC version 2.0.result: The number of connected peers as a hexadecimal string.0x19equals 25 peers.
Use Case
The net_peerCount method is commonly used for:
Network connectivity monitoring
Node health assessment
P2P network analysis
Web3 Integration
Last updated
Was this helpful?