pub struct RestClient { /* private fields */ }Implementations§
Source§impl RestClient
impl RestClient
Sourcepub fn new(api_key_pair: ApiKeyPair, base_url: String) -> RestClient
pub fn new(api_key_pair: ApiKeyPair, base_url: String) -> RestClient
Examples found in repository?
examples/simple_example.rs (line 12)
5async fn main() -> anyhow::Result<()> {
6 println!("Bybit Rust API - Simple Example");
7 println!("{}", "=".repeat(50));
8
9 // Public endpoints (no API key required)
10 let api_key_pair = ApiKeyPair::new("public".to_string(), "".to_string(), "".to_string());
11
12 let rest_client = RestClient::new(api_key_pair, "https://api.bybit.com".to_string());
13
14 let market_client = MarketClient::new(rest_client);
15
16 // Get server time
17 println!("\n📍 Server Time:");
18 match market_client.get_server_time().await {
19 Ok(response) => {
20 println!(" Time: {}", response.result.time_second);
21 }
22 Err(e) => println!(" Error: {}", e),
23 }
24
25 // Get tickers
26 println!("\n📊 Market Tickers:");
27 match market_client
28 .get_tickers(Category::Spot, None, None, None)
29 .await
30 {
31 Ok(response) => {
32 // The response is already deserialized, just print it
33 println!(" Response received successfully: {:#?}", response);
34 }
35 Err(e) => println!(" Error: {}", e),
36 }
37
38 Ok(())
39}More examples
examples/account_example.rs (line 16)
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let api_key = env::var("BYBIT_API_KEY").unwrap_or_else(|_| "YOUR_API_KEY".to_string());
9 let api_secret = env::var("BYBIT_API_SECRET").unwrap_or_else(|_| "YOUR_API_SECRET".to_string());
10
11 // Using testnet by default
12 let base_url = "https://api-testnet.bybit.com".to_string();
13
14 // ApiKeyPair takes (profile_name, key, secret)
15 let key_pair = ApiKeyPair::new("account_demo".to_string(), api_key, api_secret);
16 let client = RestClient::new(key_pair, base_url);
17
18 // AccountClient takes ownership of RestClient
19 let account_client = AccountClient::new(client);
20
21 println!("--- Testing get_wallet_balance ---");
22 let params = GetWalletBalanceParams {
23 account_type: AccountType::UNIFIED,
24 coin: None,
25 };
26
27 match account_client.get_wallet_balance(params).await {
28 Ok(response) => println!("Success: {:?}", response),
29 Err(e) => println!("Error: {:?}", e),
30 }
31
32 println!("\n--- Testing get_fee_rate ---");
33 match account_client.get_fee_rate("spot", None, None).await {
34 Ok(response) => println!("Success: {:?}", response),
35 Err(e) => println!("Error: {:?}", e),
36 }
37
38 Ok(())
39}examples/market_example.rs (line 11)
6async fn main() -> anyhow::Result<()> {
7 // Create API key pair (for public endpoints, you can use empty strings)
8 let api_key_pair = ApiKeyPair::new("default".to_string(), "".to_string(), "".to_string());
9
10 // Create REST client
11 let rest_client = RestClient::new(api_key_pair, "https://api.bybit.com".to_string());
12
13 // Create Market client
14 let market_client = MarketClient::new(rest_client);
15
16 // Get server time
17 println!("Getting server time...");
18 let server_time = market_client.get_server_time().await?;
19 println!("Server time: {:?}", server_time.result);
20
21 // Get BTC/USDT kline data for spot market
22 println!("\nGetting BTC/USDT kline data...");
23 let kline_data = market_client
24 .get_kline(
25 Category::Spot,
26 "BTCUSDT",
27 Interval::OneHour,
28 None,
29 None,
30 Some(10),
31 )
32 .await?;
33 println!("Kline data count: {}", kline_data.result.list.len());
34
35 // Get orderbook
36 println!("\nGetting BTC/USDT orderbook...");
37 let orderbook = market_client
38 .get_orderbook(Category::Spot, "BTCUSDT", Some(5))
39 .await?;
40 println!("Orderbook: {:?}", orderbook.result);
41
42 // Get tickers
43 println!("\nGetting tickers...");
44 let tickers = market_client
45 .get_tickers(Category::Spot, Some("BTCUSDT"), None, None)
46 .await?;
47 println!("Tickers: {:?}", tickers.result);
48
49 Ok(())
50}examples/trading_example.rs (line 19)
10async fn main() -> anyhow::Result<()> {
11 // Get API credentials from environment variables
12 let api_key = env::var("BYBIT_API_KEY").expect("BYBIT_API_KEY not set");
13 let api_secret = env::var("BYBIT_API_SECRET").expect("BYBIT_API_SECRET not set");
14
15 // Create API key pair
16 let api_key_pair = ApiKeyPair::new("trading".to_string(), api_key, api_secret);
17
18 // Create REST client for testnet
19 let rest_client = RestClient::new(api_key_pair, "https://api-testnet.bybit.com".to_string());
20
21 // Create Order client
22 let order_client = OrderClient::new(rest_client);
23
24 // Place a limit order
25 println!("Placing a limit order...");
26 let place_order_request = PlaceOrderRequest {
27 category: Category::Spot,
28 symbol: "BTCUSDT".to_string(),
29 side: Side::Buy,
30 order_type: OrderType::Limit,
31 qty: "0.001".to_string(),
32 price: Some("40000".to_string()),
33 time_in_force: Some(TimeInForce::GTC),
34 order_link_id: Some(format!("rust_sdk_test_{}", chrono::Utc::now().timestamp())),
35 is_leverage: None,
36 trigger_price: None,
37 trigger_direction: None,
38 trigger_by: None,
39 order_filter: None,
40 order_iv: None,
41 position_idx: None,
42 take_profit: None,
43 stop_loss: None,
44 tp_trigger_by: None,
45 sl_trigger_by: None,
46 reduce_only: None,
47 close_on_trigger: None,
48 smp_type: None,
49 mmp: None,
50 tpsl_mode: None,
51 tp_limit_price: None,
52 sl_limit_price: None,
53 tp_order_type: None,
54 sl_order_type: None,
55 };
56
57 match order_client.place_order(place_order_request).await {
58 Ok(response) => {
59 println!("Order placed successfully!");
60 println!("Order ID: {}", response.result.order_id);
61 println!("Order Link ID: {}", response.result.order_link_id);
62
63 // Get open orders
64 println!("\nGetting open orders...");
65 let open_orders = order_client
66 .get_open_orders(
67 Category::Spot,
68 Some("BTCUSDT"),
69 None,
70 None,
71 None,
72 None,
73 None,
74 None,
75 Some(10),
76 None,
77 )
78 .await?;
79 println!("Open orders count: {}", open_orders.result.list.len());
80
81 // Cancel the order
82 println!("\nCancelling the order...");
83 let cancel_request = CancelOrderRequest {
84 category: Category::Spot,
85 symbol: "BTCUSDT".to_string(),
86 order_id: Some(response.result.order_id.clone()),
87 order_link_id: None,
88 order_filter: None,
89 };
90
91 let cancel_response = order_client.cancel_order(cancel_request).await?;
92 println!("Order cancelled successfully!");
93 println!("Cancelled Order ID: {}", cancel_response.result.order_id);
94 }
95 Err(e) => {
96 eprintln!("Failed to place order: {}", e);
97 }
98 }
99
100 // Get order history
101 println!("\nGetting order history...");
102 let order_history = order_client
103 .get_order_history(
104 Category::Spot,
105 Some("BTCUSDT"),
106 None,
107 None,
108 None,
109 None,
110 None,
111 None,
112 None,
113 None,
114 Some(10),
115 None,
116 )
117 .await?;
118 println!("Order history count: {}", order_history.result.list.len());
119
120 Ok(())
121}pub async fn get<A: DeserializeOwned>( &self, endpoint: &str, query: Value, sec_type: SecType, ) -> Result<A, Error>
pub async fn post<A: DeserializeOwned>( &self, endpoint: &str, body: Value, sec_type: SecType, ) -> Result<A, Error>
Auto Trait Implementations§
impl Freeze for RestClient
impl !RefUnwindSafe for RestClient
impl Send for RestClient
impl Sync for RestClient
impl Unpin for RestClient
impl !UnwindSafe for RestClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more