Returns the block from the network based on the provided block number or hash.
Don’t have an API key?
Start using this method in your app today.
Description
Returns the block from the network based on the provided block number or hash. Transactions on the block are represented as an array of transaction hashes. To get the full transaction details on the block, use getBlockWithTransactions instead.
Parameters
Name | Type | Description |
---|---|---|
blockHashOrBlockTag | string | The block number or hash to get the block for. |
Response
Property | Type | Description |
---|---|---|
Promise<Block> | object | Returns a block object with the following fields or null when no block was found. |
Block
response object parameters
Block
response object parametersParameter | Type | Description |
---|---|---|
hash | string | 32 Bytes - hash of the block. null when its pending block. |
parentHash | string | 32 Bytes - hash of the parent block. |
number | number | The block number. null when its pending block. |
timestamp | number | The unix timestamp for when the block was collated. |
nonce | string | A nonce is a value that represents the number of transactions sent by the sender's address, ensuring the transactions are processed in order and preventing replay attacks. |
difficulty | number | Integer of the difficulty for this block. |
gasLimit | BigNumber | The maximum gas allowed in this block. |
gasUsed | BigNumber | The total used gas by all transactions in this block. |
miner | string | 20 Bytes - the address of the beneficiary to whom the mining rewards were given. |
transactions | array | An arrray containing transactions. |
Example Request and Response
Prerequisite: You will need to install the Alchemy SDK before making requests with it.
The commands for installing it using npm or yarn are given below:
npm install alchemy-sdk
yarn add alchemy-sdk
Request
Here is an example of how to make a getBlock
request using the Alchemy SDK:
// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");
// Configures the Alchemy SDK
const config = {
apiKey: "alchemy-replit", // Replace with your API key
network: Network.ETH_MAINNET, // Replace with your network
};
// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);
const main = async () => {
//Assign the hash to a variable
let txHash = "0x92fc42b9642023f2ee2e88094df80ce87e15d91afa812fef383e6e5cd96e2ed3"
//Response would return the block for provided hash
let response = await alchemy.core.getBlock(txHash)
//Logging the response to the console
console.log(response)
};
main();
Response
{
hash: '0x92fc42b9642023f2ee2e88094df80ce87e15d91afa812fef383e6e5cd96e2ed3',
parentHash: '0x6890edf8ad6900a5472c2a7ee3ef795f020ef6f907afb7f4ebf6a92d6aeb1804',
number: 15221026,
timestamp: 1658877717,
nonce: '0xd8c399035d6e6e8f',
difficulty: null,
gasLimit: BigNumber { _hex: '0x01ca35d2', _isBigNumber: true },
gasUsed: BigNumber { _hex: '0x01ca1ae1', _isBigNumber: true },
miner: '0x52bc44d5378309EE2abF1539BF71dE1b7d7bE3b5',
extraData: '0x6e616e6f706f6f6c2e6f7267',
transactions: [
'0xba4938ea41154427c8cb424ea89d9f150f139ed10065fe43ce11102dc82e1c37',
'0x98cc6b4b66453e184f65728fee265a726b030c5ddcfc1311a01ea5345c3959ab',
'0x2cbb4968cce66c73f2755fe7ef177981962afa7943f658b323e61850f31e4163',
'0x34e65a26b58ec93ec3a35c5c3269f1179801604e027cfd6a7cad8bef78e4fe62',
'0x745e34c019f924316863e5af9f2cfbe6f660071310054e8331e9dc1fcdfc26a8',
... 253 more items
],
baseFeePerGas: BigNumber { _hex: '0x02aa2d1d80', _isBigNumber: true },
_difficulty: BigNumber { _hex: '0x2a31d8a2cf4dd7', _isBigNumber: true }
}
Code Sandbox
You can test out the getBlock
method using the code sandbox below:
Related Methods
Here are the methods related to getBlock
:
- getBlockWithTransactions: Returns the block from the network based on the provided block number or hash. Includes full transactions from the block.