A production-grade Rust SDK for Bybit V5 API.
- Async-first: Built on tokio for high-performance async I/O
- Type-safe: Strongly typed request/response models with serde
- Zero-panic: No
unwrap()orexpect()in library code - Fund safety: Parameter validation before sending orders/withdrawals
- Complete API coverage: REST API + WebSocket support
- Production ready: Timeout protection, error handling, reconnection
Add to your Cargo.toml:
[dependencies]
bybit-api = "0.1"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }use bybit_api::{BybitClient, Category};
#[tokio::main]
async fn main() -> bybit_api::Result<()> {
// Create client for testnet
let client = BybitClient::testnet("your_api_key", "your_api_secret")?;
// Get tickers (public endpoint)
let tickers = client.get_tickers(Category::Linear, Some("BTCUSDT")).await?;
println!("BTC Price: {}", tickers.list[0].last_price);
// Get wallet balance (private endpoint)
let balance = client.get_wallet_balance("UNIFIED").await?;
println!("Total Equity: {}", balance.list[0].total_equity);
Ok(())
}use bybit_api::{BybitClient, Category, Side};
use bybit_api::trade::PlaceOrderParams;
#[tokio::main]
async fn main() -> bybit_api::Result<()> {
let client = BybitClient::testnet("your_api_key", "your_api_secret")?;
// Place a market order
let params = PlaceOrderParams::market(
Category::Linear,
"BTCUSDT",
Side::Buy,
"0.001",
);
let result = client.place_order(params).await?;
println!("Order ID: {}", result.order_id);
Ok(())
}use bybit_api::websocket::BybitWebSocket;
use bybit_api::TESTNET_WS_PUBLIC_LINEAR;
#[tokio::main]
async fn main() -> bybit_api::Result<()> {
// Create public WebSocket client
let mut ws = BybitWebSocket::public(TESTNET_WS_PUBLIC_LINEAR);
ws.connect().await?;
// Subscribe to orderbook
ws.subscribe(vec!["orderbook.50.BTCUSDT".to_string()], |msg| {
println!("Received: {:?}", msg.topic);
}).await?;
// Keep connection alive
tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
ws.disconnect().await?;
Ok(())
}// Mainnet (production)
let client = BybitClient::with_credentials("key", "secret")?;
// Testnet
let client = BybitClient::testnet("key", "secret")?;
// Demo trading
let client = BybitClient::demo("key", "secret")?;get_server_time()- Server timeget_instruments_info()- Instruments infoget_orderbook()- Orderbook depthget_tickers()- 24hr tickerget_klines()- Kline/candlestick dataget_funding_history()- Funding rate historyget_recent_trades()- Recent tradesget_open_interest()- Open interest
place_order()- Place orderamend_order()- Amend ordercancel_order()- Cancel ordercancel_all_orders()- Cancel all ordersplace_batch_order()- Batch place ordersget_open_orders()- Get open ordersget_order_history()- Order history
get_positions()- Get positionsset_leverage()- Set leverageset_trading_stop()- Set TP/SLget_closed_pnl()- Closed PnLget_executions()- Execution list
get_wallet_balance()- Wallet balanceget_account_info()- Account infoget_fee_rate()- Fee ratesget_transaction_log()- Transaction log
get_coin_info()- Coin infointernal_transfer()- Internal transferget_deposit_address()- Deposit addresswithdraw()- Withdraw (with validation)
- Public:
orderbook,trade,ticker,kline - Private:
position,order,wallet,execution
use bybit_api::{BybitClient, BybitError};
match client.place_order(params).await {
Ok(result) => println!("Order placed: {}", result.order_id),
Err(BybitError::Api { code, msg }) => {
println!("API error {}: {}", code, msg);
if code == 10006 {
println!("Rate limited, please slow down");
}
}
Err(BybitError::Timeout) => println!("Request timed out"),
Err(e) => println!("Other error: {}", e),
}See the examples directory for more:
market_ticker.rs- Fetch market dataplace_order.rs- Place ordersget_positions.rs- Get positionsget_wallet_balance.rs- Get wallet balancewebsocket_orderbook.rs- Subscribe to orderbookwebsocket_private.rs- Private channel subscriptions
Run examples:
export BYBIT_API_KEY=your_key
export BYBIT_API_SECRET=your_secret
cargo run --example market_tickerMIT License - see LICENSE for details.