A simple Rust web API built with Axum and ethers-rs to fetch Ethereum ETH and ERC-20 token balances using Alchemy’s JSON-RPC API.
- Fetch ETH balance for any Ethereum address
- Fetch ERC-20 token balances via Alchemy API
- Built with async Rust for high performance and safety
- Rust (recommended latest stable)
- An Alchemy API key with Ethereum Mainnet access
-
Clone the repository
git clone https://github.com/RafaelPil/axum-ethers.git cd axum-ethers
-
Create a .env file in the project root with your Alchemy API key:
ETH_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY
-
Build and run the server:
cargo run
-
The server will run on http://localhost:3000
-
Get ETH balance for an address:
curl http://localhost:3000/eth/balance/address
-
Get ERC-20 token balances for an address:
curl curl http://localhost:3000/eth/tokens/address
- main.rs: app entry point, routing setup
- handlers.rs: request handlers for balances
- .env: environment variables (not committed to Git)
- Make sure .env is included in .gitignore to keep your API keys private.
- This project uses Alchemy’s API; ensure your API key has the necessary permissions.
- Extend or customize the token balance parsing to fit your needs.