Read the Latest peaq Network Block with JSON-RPC
Use a peaq-compatible EVM JSON-RPC endpoint from Node.js 18+ to request the current block number, validate the response, and distinguish RPC access from indexed data.
Use the standard eth_blockNumber JSON-RPC method to read the latest block number from a peaq endpoint. Choose an endpoint from the peaq network page, place it in the RPC_URL environment variable, and run the request below. This checks direct node access; it does not provide historical indexing, decoded entities, or a query API.
1. Prepare a peaq RPC URL
An RPC URL is the address your application sends JSON-RPC requests to. Obtain a URL from your infrastructure provider or node deployment, then keep it outside source control by storing it in RPC_URL. Do not paste credentials into a committed script.
The example uses eth_blockNumber, an EVM JSON-RPC method that returns the current block height as a hexadecimal quantity.
- Set
RPC_URLto the complete endpoint URL supplied for your peaq access. - Use Node.js 18 or newer, which provides the global
fetchAPI used here. - The script does not assume a particular host, path, credential format, or measured response time.
export RPC_URL='https://your-peaq-rpc-endpoint.example'
node peaq-head.jsReplace the example value with the endpoint you actually received; it is intentionally not a real endpoint.
2. Request the current block number
Create peaq-head.js with the following code. It checks configuration, HTTP status, JSON-RPC errors, and the returned quantity before printing the block number in both hexadecimal and decimal form.
- A successful response contains a JSON-RPC result such as
{"jsonrpc":"2.0","id":1,"result":"0x..."}. - The decimal value is calculated locally from the returned hexadecimal quantity; it is not a prefilled or measured network value.
const rpcUrl = process.env.RPC_URL;
if (!rpcUrl) {
console.error('Missing RPC_URL. Set it to your peaq JSON-RPC endpoint.');
process.exit(1);
}
let response;
try {
response = await fetch(rpcUrl, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: []
})
});
} catch (error) {
console.error(`Network request failed: ${error.message}`);
process.exit(1);
}
const bodyText = await response.text();
let body;
try {
body = JSON.parse(bodyText);
} catch {
console.error(`RPC returned non-JSON data (HTTP ${response.status}).`);
process.exit(1);
}
if (!response.ok) {
console.error(`HTTP ${response.status}: ${body.error?.message ?? bodyText}`);
process.exit(1);
}
if (body.error) {
console.error(`JSON-RPC error ${body.error.code}: ${body.error.message}`);
process.exit(1);
}
if (typeof body.result !== 'string' || !/^0x[0-9a-f]+$/i.test(body.result)) {
console.error('Unexpected eth_blockNumber result:', body.result);
process.exit(1);
}
console.log(JSON.stringify({
blockNumberHex: body.result,
blockNumberDecimal: BigInt(body.result).toString()
}, null, 2));Expected result: a JSON object with blockNumberHex and blockNumberDecimal, where the values come from your endpoint at run time. The exact block number will change as the network advances.
If the endpoint returns an HTTP error, JSON-RPC error, malformed JSON, or invalid result, the script exits with a diagnostic message instead of treating the request as successful.
3. Know what this check proves
A successful response proves that the configured endpoint accepted an RPC request and returned the node's current block-number response. It does not prove that every historical block is available, that logs are indexed for arbitrary queries, or that application-specific entities have been decoded.
For indexed access, use an indexing product or deploy an indexer that processes chain data into queryable entities. RPC and indexing solve different problems: RPC exposes node methods, while indexing supports application-shaped historical queries.
- Use RPC for targeted node operations such as reading the current head or calling a contract method.
- Use indexing when your feature needs repeatable historical filtering, joins, pagination, or decoded domain records.
- To measure freshness or request latency for your own endpoint, record timestamps around the request and compare returned block numbers over repeated runs; do not infer those measurements from this guide.
4. Extend the workflow
Once the head check works, add the contract calls or event requests your application actually needs and retain the same validation and error handling. If you need guidance on RPC setup and Web3 infrastructure concepts, continue with OnFinality Learn. You can also use the RPC Assistant to explore the next RPC request without hard-coding an endpoint assumption.
- Keep endpoint configuration in environment variables or a secrets manager.
- Log request identifiers and error details, but redact credentials and authorization headers.
- Treat a successful RPC response and successful indexed query as separate acceptance checks.