Summary
Learn how to fetch the latest Ethereum block using the eth_getBlockByNumber JSON-RPC method, including curl and ethers.js examples, response fields, and common pitfalls. Compare public, managed, and dedicated RPC options to choose the right endpoint for your dApp.
Quick Answer: Use eth_getBlockByNumber with "latest"
To get the latest Ethereum block, call the eth_getBlockByNumber JSON-RPC method with the parameter "latest" and false for the second argument (to omit full transaction objects). Here's a minimal curl example against a public Ethereum endpoint:
curl -X POST https://eth.api.onfinality.io/public \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}'
The response includes the block header, transaction hashes, gas usage, and timestamp. This is the standard way to read the current head of the chain for indexing, monitoring, or building user-facing features like block explorers.
Decision Guide: Which RPC Endpoint Should You Use?
Before you write code, decide which Ethereum RPC endpoint fits your workload. The right choice depends on how often you poll, whether you need WebSocket subscriptions, and whether you need historical data.
| Workload | Recommended Endpoint | Why |
|---|---|---|
| Occasional reads, prototyping | Public endpoint (e.g., https://eth.api.onfinality.io/public) | Free, no signup, fine for low volume |
| Production dApp, moderate traffic | Managed RPC service (e.g., OnFinality API) | Better reliability, rate limits, and support |
| High-frequency polling, WebSocket | Dedicated node (e.g., OnFinality Dedicated Node) | No shared rate limits, low latency, custom config |
| Archive data, deep history | Archive node (often via dedicated node) | Needed for eth_getLogs on old blocks |
If you're building a production app, start with a managed RPC provider to avoid the operational burden of running your own node. For high-throughput or latency-sensitive use cases, consider a dedicated node. See RPC pricing and supported RPC networks for details.
What Is eth_getBlockByNumber?
eth_getBlockByNumber is an Ethereum JSON-RPC method that returns information about a block specified by its number or tag. The method accepts two parameters:
blockNumber(QUANTITY or TAG): The block number as a hex string, or one of the tags"earliest","latest","pending","safe", or"finalized".fullTx(BOOLEAN): Iftrue, returns full transaction objects; iffalse, returns only transaction hashes.
When you pass "latest", the node returns the most recent block in the canonical chain. This is equivalent to the block that would be referenced by the latest tag in other Ethereum APIs.
How to Fetch the Latest Block with curl
Here's a complete curl example that fetches the latest block with transaction hashes only:
curl -X POST https://eth.api.onfinality.io/public \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}'
To get full transaction objects, change false to true. Note that this can produce a very large response if the block has many transactions.
How to Fetch the Latest Block with ethers.js
In ethers.js v6, you can use the getBlock method on a provider:
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider("https://eth.api.onfinality.io/public");
async function getLatestBlock() {
const block = await provider.getBlock("latest");
console.log(block.number);
console.log(block.hash);
console.log(block.timestamp);
console.log(block.transactions); // array of tx hashes
}
getLatestBlock();
If you need full transaction objects, use provider.getBlock("latest", true).
Understanding the Response Fields
The response object contains many fields. Here are the most useful ones for developers:
| Field | Description |
|---|---|
number | The block number (hex). |
hash | The block hash. |
parentHash | Hash of the parent block. |
timestamp | Unix timestamp of when the block was proposed. |
transactions | Array of transaction hashes or full transaction objects, depending on fullTx. |
gasUsed | Total gas used by all transactions in the block. |
gasLimit | Block gas limit. |
miner | Address of the block producer (post-merge, this is the fee recipient). |
baseFeePerGas | The base fee per gas for this block (EIP-1559). |
Common Pitfalls and How to Avoid Them
- Using the wrong tag:
"latest"is the head of the chain, but it can be reorged. If you need a stable reference, use"safe"or"finalized"for applications that require finality. - Large responses: Requesting
fullTx=trueon a busy block can return megabytes of data. Usefalseand fetch individual transactions if needed. - Rate limits: Public endpoints often have rate limits. If you're polling frequently, you may hit errors. Consider a managed RPC service or dedicated node.
- WebSocket vs HTTP: For real-time updates, use WebSocket subscriptions instead of polling
eth_getBlockByNumber. OnFinality supports WebSocket on Ethereum mainnet; see Ethereum network page for details.
When to Use WebSocket Instead of Polling
Polling eth_getBlockByNumber every few seconds is inefficient. If your app needs to react to new blocks instantly, subscribe to new block headers via WebSocket:
import { ethers } from "ethers";
const provider = new ethers.WebSocketProvider("wss://eth.api.onfinality.io/public");
provider.on("block", (blockNumber) => {
console.log("New block:", blockNumber);
});
This pushes block numbers to your client as they occur, reducing latency and unnecessary requests.
Production Readiness Checklist
Before you go live, review this checklist:
- Choose the right RPC provider based on your traffic and reliability needs.
- Implement retry logic with exponential backoff for transient errors.
- Use
"safe"or"finalized"tags for applications that require finality. - Monitor your RPC usage and set up alerts for rate limit errors.
- Consider a dedicated node if you need consistent performance.
Key Takeaways
- Use
eth_getBlockByNumberwith the"latest"tag to fetch the current head block. - The method returns block metadata and transaction hashes or full transactions.
- For production, choose a managed RPC provider or dedicated node to avoid rate limits and downtime.
- WebSocket subscriptions are more efficient than polling for real-time block updates.
Frequently Asked Questions
What is the difference between "latest" and "safe" blocks?
"latest" is the most recent block in the canonical chain, but it can be reorged. "safe" is a block that is unlikely to be reorged, and "finalized" is guaranteed final. Use "safe" or "finalized" for applications that require finality.
How do I get the latest block number only?
You can call eth_blockNumber to get the latest block number as a hex string. This is lighter than fetching the full block.
Why does my request return an error?
Common errors include rate limiting, invalid JSON, or using an unsupported tag. Check your endpoint URL and ensure you're using a valid JSON-RPC payload.
Can I get the latest block on a testnet?
Yes, use the same method on a testnet endpoint, such as https://eth-sepolia.api.onfinality.io/public for Sepolia. See the Sepolia network page for details.
What is the best RPC provider for Ethereum?
There is no single "best" provider; it depends on your workload. Compare public, managed, and dedicated options in our Ethereum RPC providers comparison. OnFinality offers both managed and dedicated nodes; see RPC pricing for details.