eth_getBlockTransactionCountByNumber - Base

The eth_getBlockTransactionCountByNumber method returns the number of transactions in a block matching the given block number. This provides a quick way to assess block activity without fetching full block data.

Parameters

ParameterTypeRequiredDescription
blockParameterstringYesBlock number in hex, or "latest", "earliest", "pending"

Request

cURL

bashcurl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "eth_getBlockTransactionCountByNumber",
    "params": ["latest"],
    "id": "getblock.io"
}'

axios.js

javascriptconst axios = require('axios');

const response = await axios.post('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/', {
    jsonrpc: '2.0',
    method: 'eth_getBlockTransactionCountByNumber',
    params: ['latest'],
    id: 'getblock.io'
}, {
    headers: { 'Content-Type': 'application/json' }
});

const txCount = parseInt(response.data.result, 16);
console.log('Transaction Count:', txCount);

requests.py

pythonimport requests

response = requests.post(
    'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'eth_getBlockTransactionCountByNumber',
        'params': ['latest'],
        'id': 'getblock.io'
    }
)

result = response.json()
tx_count = int(result['result'], 16)
print(f'Transaction Count: {tx_count}')

main.rs

rustuse reqwest::Client;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "eth_getBlockTransactionCountByNumber",
            "params": ["latest"],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;
    
    println!("Transaction Count: {}", response["result"]);
    Ok(())
}

Response

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": "0xc"
}

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC protocol version ("2.0")
idstringRequest identifier matching the request
resultstringNumber of transactions in the block (hex)

Use Cases

  • Block Monitoring: Track transaction counts in recent blocks
  • Network Activity: Measure current network throughput
  • Historical Analysis: Analyze transaction patterns over time
  • Resource Planning: Estimate processing requirements
  • Dashboard Metrics: Display real-time block statistics

Error Handling

Error CodeMessageDescription
-32602Invalid paramsInvalid block number format
-32603Internal errorBlock not found

Web3 Integration

ethers.js

javascriptconst { ethers } = require('ethers');

const provider = new ethers.JsonRpcProvider('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

async function getTxCountByNumber(blockNumber = 'latest') {
    const block = await provider.getBlock(blockNumber);
    
    if (block) {
        console.log('Block Number:', block.number);
        console.log('Transaction Count:', block.transactions.length);
        return block.transactions.length;
    } else {
        console.log('Block not found');
        return null;
    }
}

// Get latest block tx count
getTxCountByNumber();

// Get specific block tx count
getTxCountByNumber(19800000);

viem.js

javascriptimport { createPublicClient, http } from 'viem';
import { base } from 'viem/chains';

const client = createPublicClient({
    chain: base,
    transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

async function getTxCountByNumber(blockNumber) {
    const count = await client.getBlockTransactionCount({
        blockNumber: blockNumber ? BigInt(blockNumber) : undefined
    });
    
    console.log('Transaction Count:', count);
    return count;
}

// Get latest block tx count
getTxCountByNumber();

// Get specific block tx count
getTxCountByNumber(19800000);