pub struct Binance { /* private fields */ }Expand description
Main entry point for the Binance API client.
The Binance struct provides access to all API modules and handles
configuration and authentication.
Implementations§
Source§impl Binance
impl Binance
Sourcepub fn new_unauthenticated() -> Result<Self>
pub fn new_unauthenticated() -> Result<Self>
Create a new unauthenticated Binance client for public endpoints only.
This client can only access public market data endpoints.
For account and trading endpoints, use Binance::new() with credentials.
§Example
use binance_api_client::Binance;
let client = Binance::new_unauthenticated()?;Sourcepub fn with_config<S: Into<String>>(
config: Config,
credentials: Option<(S, S)>,
) -> Result<Self>
pub fn with_config<S: Into<String>>( config: Config, credentials: Option<(S, S)>, ) -> Result<Self>
Create a new Binance client with custom configuration.
§Arguments
config- Custom configuration (testnet, Binance.US, etc.)credentials- Optional credentials tuple (api_key, secret_key)
§Example
use binance_api_client::{Binance, Config};
// Testnet with credentials
let config = Config::testnet();
let client = Binance::with_config(config, Some(("api_key", "secret_key")))?;
// Production without credentials
let config = Config::default();
let client = Binance::with_config(config, None::<(&str, &str)>)?;Sourcepub fn from_env() -> Result<Self>
pub fn from_env() -> Result<Self>
Create a new Binance client from environment variables.
Expects BINANCE_API_KEY and BINANCE_SECRET_KEY environment variables.
§Example
use binance_api_client::Binance;
let client = Binance::from_env()?;Sourcepub fn testnet(
api_key: impl Into<String>,
secret_key: impl Into<String>,
) -> Result<Self>
pub fn testnet( api_key: impl Into<String>, secret_key: impl Into<String>, ) -> Result<Self>
Create a new testnet Binance client with credentials.
§Example
use binance_api_client::Binance;
let client = Binance::testnet("api_key", "secret_key")?;Sourcepub fn testnet_unauthenticated() -> Result<Self>
pub fn testnet_unauthenticated() -> Result<Self>
Create a new testnet Binance client without credentials.
§Example
use binance_api_client::Binance;
let client = Binance::testnet_unauthenticated()?;Sourcepub fn binance_us(
api_key: impl Into<String>,
secret_key: impl Into<String>,
) -> Result<Self>
pub fn binance_us( api_key: impl Into<String>, secret_key: impl Into<String>, ) -> Result<Self>
Create a new Binance.US client with credentials.
§Example
use binance_api_client::Binance;
let client = Binance::binance_us("api_key", "secret_key")?;Sourcepub fn client(&self) -> &Client
pub fn client(&self) -> &Client
Get the underlying HTTP client.
This is useful for advanced use cases where you need direct access to the client.
Sourcepub fn has_credentials(&self) -> bool
pub fn has_credentials(&self) -> bool
Check if this client has credentials for authenticated endpoints.
Sourcepub fn market(&self) -> Market
pub fn market(&self) -> Market
Access market data API endpoints.
Market data endpoints are public and don’t require authentication.
§Example
let client = Binance::new_unauthenticated()?;
// Get current BTC price
let price = client.market().price("BTCUSDT").await?;
println!("BTC/USDT: {}", price.price);
// Get order book
let depth = client.market().depth("BTCUSDT", Some(10)).await?;Sourcepub fn user_stream(&self) -> UserStream
pub fn user_stream(&self) -> UserStream
Access user data stream API endpoints.
User data streams provide real-time updates for account balance changes, order updates, and other account events via WebSocket.
Requires authentication.
§Example
let client = Binance::new("api_key", "secret_key")?;
// Start a user data stream
let listen_key = client.user_stream().start().await?;
// Keep it alive (call every 30 minutes)
client.user_stream().keepalive(&listen_key).await?;
// Close when done
client.user_stream().close(&listen_key).await?;Sourcepub fn account(&self) -> Account
pub fn account(&self) -> Account
Access account and trading API endpoints.
Account and trading endpoints require authentication. Use these to manage orders, query account balances, and view trade history.
Requires authentication.
§Example
let client = Binance::new("api_key", "secret_key")?;
// Get account info
let account = client.account().get_account().await?;
// Place a limit buy order
let order = client.account().limit_buy("BTCUSDT", "0.001", "50000.00").await?;
// Or use the order builder for more control
use binance_api_client::{OrderBuilder, OrderSide, OrderType, TimeInForce};
let order = OrderBuilder::new("BTCUSDT", OrderSide::Buy, OrderType::Limit)
.quantity("0.001")
.price("50000.00")
.time_in_force(TimeInForce::GTC)
.build();
let response = client.account().create_order(&order).await?;Sourcepub fn wallet(&self) -> Wallet
pub fn wallet(&self) -> Wallet
Access wallet SAPI endpoints.
Wallet endpoints provide access to deposit/withdrawal operations, asset management, universal transfers, and account status.
Requires authentication.
§Example
let client = Binance::new("api_key", "secret_key")?;
// Check system status
let status = client.wallet().system_status().await?;
if status.is_normal() {
println!("System is operational");
}
// Get all coin information
let coins = client.wallet().all_coins().await?;
// Get deposit address
let address = client.wallet().deposit_address("BTC", None).await?;
println!("Deposit BTC to: {}", address.address);
// Get trade fees
let fees = client.wallet().trade_fee(Some("BTCUSDT")).await?;Sourcepub fn margin(&self) -> Margin
pub fn margin(&self) -> Margin
Access margin trading SAPI endpoints.
Margin endpoints provide access to cross-margin and isolated margin trading, loans, transfers, and account management.
Requires authentication.
§Example
let client = Binance::new("api_key", "secret_key")?;
// Get cross-margin account details
let account = client.margin().account().await?;
println!("Margin level: {}", account.margin_level);
// Check max borrowable
let max = client.margin().max_borrowable("BTC", None).await?;
println!("Max borrowable BTC: {}", max.amount);
// Transfer to margin account
use binance_api_client::MarginTransferType;
let result = client.margin()
.transfer("USDT", "100.0", MarginTransferType::MainToMargin)
.await?;
// Borrow
let loan = client.margin().loan("USDT", "50.0", false, None).await?;Sourcepub fn websocket(&self) -> WebSocketClient
pub fn websocket(&self) -> WebSocketClient
Access WebSocket streaming API.
The WebSocket client provides real-time market data streams including trades, klines, tickers, and order book updates. It also supports user data streams for account updates when connected with a listen key.
§Example
use binance_api_client::Binance;
let client = Binance::new_unauthenticated()?;
let ws = client.websocket();
// Connect to aggregate trade stream
let stream_name = ws.agg_trade_stream("btcusdt");
let mut conn = ws.connect(&stream_name).await?;
// Receive events
while let Some(event) = conn.next().await {
println!("{:?}", event?);
}