This directory contains examples demonstrating how to use the ccxt-rust library.
# Run any example
cargo run --example <example_name> --features full
# List all examples
ls examples/*.rs| Example | Description |
|---|---|
| basic_usage | Fundamental operations: creating exchanges, fetching markets, tickers, order books |
| multi_exchange | Working with multiple exchanges simultaneously |
| trading_operations | Order creation, cancellation, and management |
| Example | Description |
|---|---|
| websocket_streaming | Generic WebSocket usage patterns |
| binance_ws_example | Binance real-time data streaming |
| hyperliquid_ws_example | Hyperliquid DEX WebSocket |
| Example | Exchange | Features |
|---|---|---|
| backpack_example | Backpack | REST API |
| backpack_ws_example | Backpack | WebSocket |
| bullish_ws_example | Bullish | WebSocket |
| coinone_ws_example | Coinone | WebSocket (Korean) |
| exmo_ws_example | EXMO | WebSocket |
| hashkey_ws_example | HashKey | WebSocket |
| hitbtc_ws_example | HitBTC | WebSocket |
| hollaex_ws_example | HollaEx | WebSocket |
| independentreserve_ws_example | Independent Reserve | WebSocket |
| krakenfutures_ws_example | Kraken Futures | WebSocket |
| kucoinfutures_ws_example | KuCoin Futures | WebSocket |
| onetrading_ws_example | OneTrading | WebSocket |
| toobit_example | Toobit | REST API |
| toobit_ws_example | Toobit | WebSocket |
| Example | Exchange | Features |
|---|---|---|
| dex_trading | Hyperliquid | DEX trading operations |
| hyperliquid_ws_example | Hyperliquid | WebSocket streaming |
| Example | Description |
|---|---|
| error_handling | Error types, retry logic, recovery patterns |
| advanced_config | Rate limiting, proxies, timeouts, caching |
cargo run --example basic_usage --features cexcargo run --example websocket_streaming --features cexcargo run --example dex_trading --features dexcargo run --example multi_exchange --features fulluse ccxt_rust::exchanges::cex::Binance;
use ccxt_rust::client::ExchangeConfig;
use ccxt_rust::types::Exchange;
let config = ExchangeConfig::new();
let exchange = Binance::new(config)?;// Fetch all markets
let markets = exchange.fetch_markets().await?;
// Fetch ticker
let ticker = exchange.fetch_ticker("BTC/USDT").await?;
// Fetch order book
let orderbook = exchange.fetch_order_book("BTC/USDT", Some(10)).await?;use ccxt_rust::exchanges::cex::BinanceWs;
use ccxt_rust::types::WsExchange;
let ws = BinanceWs::new();
let mut rx = ws.watch_ticker("BTC/USDT").await?;
while let Some(msg) = rx.recv().await {
println!("Received: {:?}", msg);
}use ccxt_rust::CcxtError;
match exchange.fetch_ticker("INVALID/SYMBOL").await {
Ok(ticker) => println!("Ticker: {:?}", ticker),
Err(CcxtError::BadSymbol { symbol }) => {
println!("Invalid symbol: {}", symbol);
}
Err(CcxtError::RateLimitExceeded { retry_after_ms, .. }) => {
if let Some(delay) = retry_after_ms {
tokio::time::sleep(Duration::from_millis(delay)).await;
}
}
Err(e) => println!("Error: {}", e),
}use ccxt_rust::client::{ExchangeConfig, RetryConfig, RateLimiter};
let config = ExchangeConfig::new()
.with_timeout(30000)
.with_retry(RetryConfig::default())
.with_rate_limit(true);Examples require specific features to be enabled:
| Feature | Description | Examples |
|---|---|---|
cex |
Centralized exchanges (default) | Most examples |
dex |
Decentralized exchanges | dex_trading, hyperliquid_ws_example |
full |
All features | Any example |
-
API Keys: Most examples use public endpoints. For private endpoints (trading), set environment variables:
export BINANCE_API_KEY="your_key" export BINANCE_SECRET="your_secret"
-
Rate Limiting: Examples include delays to avoid rate limiting. Adjust as needed.
-
Network: Examples require internet access to reach exchange APIs.
When adding new examples:
- Follow the naming convention:
{exchange}_example.rsor{exchange}_ws_example.rs - Add comprehensive comments explaining each step
- Include error handling
- Update this README with the new example
- Test with
cargo run --example <name> --features full