pub struct BybitClient { /* private fields */ }Expand description
Main client for interacting with the Bybit API.
§Example
use bybit_client::{BybitClient, ClientConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a public-only client.
let client = BybitClient::public_only()?;
// Or create an authenticated client.
let client = BybitClient::new("api_key", "api_secret")?;
// Use testnet.
let client = BybitClient::with_config(
ClientConfig::new("api_key", "api_secret").testnet()
)?;
Ok(())
}Implementations§
Source§impl BybitClient
impl BybitClient
Sourcepub fn new(
api_key: impl Into<String>,
api_secret: impl Into<String>,
) -> Result<Self, BybitError>
pub fn new( api_key: impl Into<String>, api_secret: impl Into<String>, ) -> Result<Self, BybitError>
Create a new client with API credentials.
Sourcepub fn public_only() -> Result<Self, BybitError>
pub fn public_only() -> Result<Self, BybitError>
Create a client for public endpoints only (no authentication).
Sourcepub fn with_config(config: ClientConfig) -> Result<Self, BybitError>
pub fn with_config(config: ClientConfig) -> Result<Self, BybitError>
Create a client with custom configuration.
Sourcepub fn http(&self) -> &HttpClient
pub fn http(&self) -> &HttpClient
Get the underlying HTTP client.
Sourcepub fn config(&self) -> &ClientConfig
pub fn config(&self) -> &ClientConfig
Get the client configuration.
Sourcepub async fn sync_time(&self) -> Result<i64, BybitError>
pub async fn sync_time(&self) -> Result<i64, BybitError>
Synchronize time with the server.
This is useful when your system clock is not accurate. The client will adjust timestamps in authenticated requests based on the calculated offset.
Sourcepub fn has_credentials(&self) -> bool
pub fn has_credentials(&self) -> bool
Check if the client has authentication credentials.
Sourcepub fn market(&self) -> MarketService
pub fn market(&self) -> MarketService
Get the market data service for public endpoints.
§Example
let client = BybitClient::public_only()?;
let params = GetTickersParams::new(Category::Linear).symbol("BTCUSDT");
let tickers = client.market().get_tickers(¶ms).await?;Sourcepub fn trade(&self) -> TradeService
pub fn trade(&self) -> TradeService
Get the trade service for order management endpoints.
Note: Trading endpoints require authentication.
§Example
let client = BybitClient::new("api_key", "api_secret")?;
// Place a market order.
let params = OrderParams::market(Category::Linear, "BTCUSDT", Side::Buy, "0.001");
let result = client.trade().submit_order(¶ms).await?;
println!("Order ID: {}", result.order_id);Sourcepub fn position(&self) -> PositionService
pub fn position(&self) -> PositionService
Get the position service for position management endpoints.
Note: Position endpoints require authentication.
§Example
let client = BybitClient::new("api_key", "api_secret")?;
let params = GetPositionInfoParams::new(Category::Linear).symbol("BTCUSDT");
let result = client.position().get_position_info(¶ms).await?;
for pos in &result.list {
println!("{}: {} @ {}", pos.symbol, pos.size, pos.avg_price);
}Sourcepub fn account(&self) -> AccountService
pub fn account(&self) -> AccountService
Get the account service for wallet and account management endpoints.
Note: Account endpoints require authentication.
§Example
let client = BybitClient::new("api_key", "api_secret")?;
let params = GetWalletBalanceParams::new(AccountType::Unified);
let result = client.account().get_wallet_balance(¶ms).await?;
for wallet in &result.list {
println!("Total equity: {}", wallet.total_equity);
}Sourcepub async fn get<T, P>(
&self,
endpoint: &str,
params: Option<&P>,
) -> Result<T, BybitError>
pub async fn get<T, P>( &self, endpoint: &str, params: Option<&P>, ) -> Result<T, BybitError>
Make a public GET request.
Sourcepub async fn get_signed<T, P>(
&self,
endpoint: &str,
params: Option<&P>,
) -> Result<T, BybitError>
pub async fn get_signed<T, P>( &self, endpoint: &str, params: Option<&P>, ) -> Result<T, BybitError>
Make an authenticated GET request.
Sourcepub async fn post_signed<T, B>(
&self,
endpoint: &str,
body: Option<&B>,
) -> Result<T, BybitError>
pub async fn post_signed<T, B>( &self, endpoint: &str, body: Option<&B>, ) -> Result<T, BybitError>
Make an authenticated POST request.
Trait Implementations§
Source§impl Clone for BybitClient
impl Clone for BybitClient
Source§fn clone(&self) -> BybitClient
fn clone(&self) -> BybitClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more