Skip to main content

Get a Block

You can retrieve a block's data by calling the Client.get_block(&block_id) function.

You can also retrieve a block's metadata by calling the Client.get_block_metadata(&block_id) function.

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Create a block with no payload.
  3. Retrieve and log the block's metadata.
  4. Retrieve and log the block's data.
  5. Get the block by its block ID.
Iota.js

You can also find this guide in the native iota.js library

Code Example

Dotenv

This example uses dotenv, which is not safe to use in production environments.

// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Calls `GET /api/core/v2/blocks/{blockId}`.
//! Returns block data as JSON by its identifier.
//! Run: `cargo run --example node_api_core_get_block --release -- [NODE URL] [BLOCK ID]`.

use std::str::FromStr;

use iota_client::{block::BlockId, Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args().nth(1).unwrap_or_else(|| {
// This example uses dotenv, which is not safe for use in production.
dotenv::dotenv().ok();
std::env::var("NODE_URL").unwrap()
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;

// Take the block ID from command line argument or...
let block_id = if let Some(Ok(block_id)) = std::env::args().nth(2).map(|s| BlockId::from_str(&s)) {
block_id
} else {
// ... fetch one from the node.
client.get_tips().await?[0]
};

// Get the block.
let block = client.get_block(&block_id).await?;

println!("{block:#?}");

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example node_api_core_get_block --release -- https://api.testnet.shimmer.network 0xa9f11aba1e9965173dc21a47ec4fbfe5b953a6e60277857a3f7a5c1499e7c454
  • You can replace https://api.testnet.shimmer.network with any node url.
  • You can replace 0x78680faa5ee34d77399ad7eff5fec9b9ceb99fea86822a16f57f49aa69771ee5 with any other Block ID.

Expected Output

Block {
protocol_version: 2,
parents: Parents(
BoxedSlicePrefix([
BlockId(0x2ec007718ebd1f8dd10be36c76820321fd24fd1739a52a7176f3314f1158ea44),
BlockId(0x536f8b2beba143278bda5074a311c827ef37c5756d80ea90372b2688b92cd5ae),
BlockId(0x6b6bcad97ba4f8d6f75aeea1dfc945425d2d6ead793af302dd5040590f5e409d),
BlockId(0xcff2436f2d65438c7351946fe793af3a8f059b485250d0ee2c5875d0aed71884),
]),
),
payload: OptionalPayload(
Some(
Transaction(
TransactionPayload {
essence: Regular(
RegularTransactionEssence {
network_id: 1856588631910923207,
inputs: BoxedSlicePrefix([
Utxo(
UtxoInput(0xcb664cb88dcb4182df622fb82661e2d82e81ff95f3f04d5f3b071484a10fdc490100),
),
Utxo(
UtxoInput(0xeede6413d25f1cb1fc68187cda339ec22e66e4fb945b8397c7251d46164a0c495900),
),
Utxo(
UtxoInput(0x45a6247d6af3214828fcab1206f6b6c8e61a359fec821d48cb578b3f70a291a40000),
),
Utxo(
UtxoInput(0x71638c38cf0be640f082edf9b1a9dc1ea898932762ede49b164f0894826009703b00),
),
]),
inputs_commitment: InputsCommitment(0x10cb15a61c30e10e569f10513834d5fd0ee53049a77223ccf43a921ea22e1675),
outputs: BoxedSlicePrefix([
Alias(
AliasOutput {
amount: BoundedU64(
53700,
),
native_tokens: NativeTokens(
BoxedSlicePrefix([]),
),
alias_id: AliasId(0x0000000000000000000000000000000000000000000000000000000000000000),
state_index: 0,
state_metadata: BoxedSlicePrefix([]),
foundry_counter: 0,
unlock_conditions: UnlockConditions(
BoxedSlicePrefix([
StateControllerAddress(
StateControllerAddressUnlockCondition(
Ed25519(
Ed25519Address(0x4f56285a1876d31b62f085c1bb35a85c0edc29889074631b81172991c3e4d6ff),
),
),
),
GovernorAddress(
GovernorAddressUnlockCondition(
Ed25519(
Ed25519Address(0x4f56285a1876d31b62f085c1bb35a85c0edc29889074631b81172991c3e4d6ff),
),
),
),
]),
),
features: Features(
BoxedSlicePrefix([]),
),
immutable_features: Features(
BoxedSlicePrefix([
Issuer(
IssuerFeature(
Ed25519(
Ed25519Address(0x4f56285a1876d31b62f085c1bb35a85c0edc29889074631b81172991c3e4d6ff),
),
),
),
]),
),
},
),
Basic(
BasicOutput {
amount: BoundedU64(
1000047500,
),
native_tokens: NativeTokens(
BoxedSlicePrefix([]),
),
unlock_conditions: UnlockConditions(
BoxedSlicePrefix([
Address(
AddressUnlockCondition(
Ed25519(
Ed25519Address(0x4f56285a1876d31b62f085c1bb35a85c0edc29889074631b81172991c3e4d6ff),
),
),
),
]),
),
features: Features(
BoxedSlicePrefix([]),
),
},
),
]),
payload: OptionalPayload(
Some(
TaggedData(
TaggedDataPayload {
tag: "0x484f524e4554205370616d6d65722053656d692d4c617a79",
data: "0x57652061726520616c6c206d616465206f662073746172647573742e0a436f756e743a203030353539320a54696d657374616d703a20323032322d30392d30315431343a32323a33345a0a54697073656c656374696f6e3a2031312e3832376d73",
},
),
),
),
},
),
unlocks: Unlocks(
BoxedSlicePrefix([
Signature(
SignatureUnlock(
Ed25519(
Ed25519Signature {
public_key: 0x229172a3883abbeb5ff7e680a6a0a0f5c31bf222d48fd1747d9d0ead24155a6b,
signature: 0x354174e8f7981aa05eca6d936cd2c7bf0eb9e2954b1f550e683959eb412194f8edab0f8536c370e576eb812da434496749221c3b96b40d6d2c9de4c06d97030f,
},
),
),
),
Reference(
ReferenceUnlock(
BoundedU16(
0,
),
),
),
Reference(
ReferenceUnlock(
BoundedU16(
0,
),
),
),
Reference(
ReferenceUnlock(
BoundedU16(
0,
),
),
),
]),
),
},
),
),
),
nonce: 1785168781326821022,
}