Fetch the header for a specific block if available
Retrieve the header and DA extension for a specific block using the v2/blocks header endpoint.
Note
LOOKING FOR INSTRUCTIONS ON HOW TO RUN YOUR OWN LIGHT CLIENT?
You can check out our docs here for instructions on how to run your own light client.
You can check out our docs here for instructions on how to run your own light client.
block_number- block number (required)
HTTP/1.1 200 OK
Content-Type: application/json
{
"hash": "{hash}",
"parent_hash": "{parent-hash}",
"number": {number},
"state_root": "{state-root}",
"extrinsics_root": "{extrinsics-root}",
"extension": {
"rows": {rows},
"cols": {cols},
"data_root": "{data-root}", // Optional
"commitments": [
"{commitment}", ...
],
"app_lookup": {
"size": {size},
"index": [
{
"app_id": {app-id},
"start": {start}
}
]
}
}
}HTTP/1.1 400 Bad Requestcurl "localhost:7007/v2/blocks/{insert a suitable block number}/header"use reqwest::{StatusCode, Client};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct BlockHeaderResponse {
hash: String,
parent_hash: String,
number: u32,
state_root: String,
extrinsics_root: String,
extension: Extension,
}
#[derive(Debug, Serialize, Deserialize)]
struct Extension {
rows: u32,
cols: u32,
data_root: Option<String>,
commitments: Vec<String>,
app_lookup: AppLookup,
}
#[derive(Debug, Serialize, Deserialize)]
struct AppLookup {
size: u32,
index: Vec<AppIndex>,
}
#[derive(Debug, Serialize, Deserialize)]
struct AppIndex {
app_id: Option<u32>,
start: u32,
}
const LIGHT_CLIENT_URL: &str = "https://api.lightclient.mainnet.avail.so";
#[tokio::main]
async fn main() {
let block_number = 592445; // Example block number
let client = Client::new();
let header_url = format!("{LIGHT_CLIENT_URL}/v2/blocks/{block_number}/header");
let response = client.get(header_url).send().await.unwrap();
match response.status() {
StatusCode::OK => {
let block_header: BlockHeaderResponse = response.json().await.unwrap();
println!("Block header: {:?}", block_header);
}
StatusCode::BAD_REQUEST => {
println!("Header not available for block {}", block_number);
}
_ => {
eprintln!("Failed to get block header: {}", response.status());
}
}
// ...error handling...
}