Skip to content

ntdat104/rbinance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦀 Binance Rust Client

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.


✨ Features

  • ✅ 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, and hmac
  • 🧱 Easy-to-use structure for extension

📦 Installation

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"

⚙️ Configuration

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(),
};

🚀 Quick Start

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(())
}

🧩 Available Methods

🔹 Public Endpoints

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

🔒 Private (Signed) Endpoints

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);

🧠 Design Overview

Structs

  • BinanceClient — main HTTP client that wraps API calls
  • Config — contains API key, secret key, and base endpoint URL

Internal Helpers

  • make_url(path) — builds full API URL
  • sign(query) — signs query string using HMAC-SHA256
  • timestamp() — current system time in milliseconds

⚠️ Notes

  • For signed endpoints, both api_key and secret_key must 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.

🧪 Testing

You can test the connectivity by running:

cargo run --example ping

Or create your own example under examples/ folder.


📜 License

This project is licensed under the MIT License.
See LICENSE for details.

About

🦀 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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages