A simple, type-safe, and ergonomic Rust wrapper for the Binance REST API.
It supports both public and signed endpoints, including market data, order management, and account information.
- ✅ Supports major Binance REST API endpoints
- 🔒 HMAC-SHA256 request signing for private endpoints
- ⚙️ Configurable API key, secret key, and endpoint
- 📦 Built on top of
reqwest,serde, andhmac - 🧱 Easy-to-use structure for extension
Add the following dependencies to your Cargo.toml:
[dependencies]
reqwest = { version = "0.12", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
hmac = "0.12"
sha2 = "0.10"
hex = "0.4"
anyhow = "1.0"You need to provide your Binance API credentials via the Config struct.
use your_crate_name::config::Config;
let config = Config {
api_key: "YOUR_BINANCE_API_KEY".to_string(),
secret_key: "YOUR_BINANCE_SECRET_KEY".to_string(),
endpoint: "https://api.binance.com".to_string(),
};use your_crate_name::client::BinanceClient;
use your_crate_name::enums::ExchangeInfoRequest;
fn main() -> anyhow::Result<()> {
// Initialize config and client
let config = Config {
api_key: "YOUR_API_KEY".into(),
secret_key: "YOUR_SECRET_KEY".into(),
endpoint: "https://api.binance.com".into(),
};
let client = BinanceClient::new(config);
// Ping the server
let pong = client.ping()?;
println!("Ping successful: {}", pong);
// Get server time
let time = client.get_server_time()?;
println!("Server time: {:?}", time);
// Get exchange info for BTCUSDT
let info = client.get_exchange_info(ExchangeInfoRequest::One("BTCUSDT".to_string()))?;
println!("Exchange info: {:?}", info);
// Get order book
let order_book = client.get_order_book("BTCUSDT", Some(5))?;
println!("Top 5 bids: {:?}", order_book.bids);
// Get account info (signed endpoint)
let account = client.get_account()?;
println!("Account info: {:?}", account);
Ok(())
}| Method | Endpoint | Description |
|---|---|---|
ping() |
/api/v3/ping |
Check API connectivity |
get_server_time() |
/api/v3/time |
Get Binance server time |
get_exchange_info(request) |
/api/v3/exchangeInfo |
Get exchange info for all or specific symbols |
get_order_book(symbol, limit) |
/api/v3/depth |
Get order book depth |
get_avg_price(symbol) |
/api/v3/avgPrice |
Get current average price |
get_recent_trades(symbol, limit) |
/api/v3/trades |
Get recent public trades |
get_historical_trades(symbol, limit, from_id) |
/api/v3/historicalTrades |
Get older market trades |
get_aggregate_trades(symbol, limit, from_id, start_time, end_time) |
/api/v3/aggTrades |
Get compressed aggregate trades |
| Method | Endpoint | Description |
|---|---|---|
get_account() |
/api/v3/account |
Get current account information |
get_all_orders(symbol) |
/api/v3/allOrders |
Get all orders for a symbol |
post_new_order(symbol, side, order_type, quantity, price, time_in_force) |
/api/v3/order |
Place a new order |
Example – placing a LIMIT BUY order:
let order = client.post_new_order(
"BTCUSDT",
"BUY",
"LIMIT",
0.001,
Some(65000.0),
Some("GTC")
)?;
println!("Order placed: {:?}", order);BinanceClient— main HTTP client that wraps API callsConfig— contains API key, secret key, and base endpoint URL
make_url(path)— builds full API URLsign(query)— signs query string using HMAC-SHA256timestamp()— current system time in milliseconds
- For signed endpoints, both
api_keyandsecret_keymust be set. - Binance’s server time and your local time might differ — ensure proper timestamp handling if you receive “timestamp for this request was 1000ms ahead/behind” errors.
- Always handle rate limits according to Binance API documentation.
You can test the connectivity by running:
cargo run --example pingOr create your own example under examples/ folder.
This project is licensed under the MIT License.
See LICENSE for details.