Example: http
To run this example:
- Clone the examples repository:
git clone git@github.com:alloy-rs/examples.git - Run:
cargo run --locked -p examples-providers --example http
//! Example of using the HTTP provider with the `reqwest` crate to get the latest block number.
use alloy::providers::{Provider, ProviderBuilder};
use example_support::rpc_url;
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Create a provider with the HTTP transport using the `reqwest` crate.
let rpc_url = rpc_url()?.parse()?;
let provider = ProviderBuilder::new().connect_http(rpc_url);
// Get latest block number.
let latest_block = provider.get_block_number().await?;
println!("Latest block number: {latest_block}");
Ok(())
}Find the source code on Github here.
