Skip to main content

BybitClient

Struct BybitClient 

Source
pub struct BybitClient { /* private fields */ }
Expand description

Bybit HTTP API client.

Implementations§

Source§

impl BybitClient

Source

pub fn new(config: ClientConfig) -> Result<Self>

Create a new client with the given configuration.

Source

pub fn with_credentials( api_key: impl Into<String>, api_secret: impl Into<String>, ) -> Result<Self>

Create a new client with API credentials using default settings.

Source

pub fn testnet( api_key: impl Into<String>, api_secret: impl Into<String>, ) -> Result<Self>

Create a new client for testnet.

Source

pub fn demo( api_key: impl Into<String>, api_secret: impl Into<String>, ) -> Result<Self>

Create a new client for demo environment.

Source

pub fn config(&self) -> &ClientConfig

Get the client configuration.

Source

pub async fn get_public<T: DeserializeOwned>( &self, endpoint: &str, params: &[(&str, &str)], ) -> Result<T>

Send a public GET request (no authentication).

Source

pub async fn get<T: DeserializeOwned>( &self, endpoint: &str, params: &[(&str, &str)], ) -> Result<T>

Send an authenticated GET request.

Source

pub async fn post<T: DeserializeOwned, B: Serialize>( &self, endpoint: &str, body: &B, ) -> Result<T>

Send an authenticated POST request.

Source§

impl BybitClient

Source

pub async fn get_wallet_balance( &self, account_type: AccountType, ) -> Result<WalletBalance>

Get wallet balance.

§Arguments
  • account_type - Account type (UNIFIED, CONTRACT, etc.)
Source

pub async fn get_account_info(&self) -> Result<AccountInfo>

Get account info.

Source

pub async fn get_fee_rate( &self, category: Category, symbol: Option<&str>, ) -> Result<FeeRates>

Get fee rate.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
Source

pub async fn get_transaction_log( &self, category: Option<Category>, limit: Option<u32>, ) -> Result<TransactionLogs>

Get transaction log.

§Arguments
  • category - Optional category filter
  • limit - Optional limit (default 20)
Source

pub async fn set_margin_mode(&self, margin_mode: MarginMode) -> Result<Value>

Set margin mode.

§Arguments
  • margin_mode - Margin mode (REGULAR_MARGIN, PORTFOLIO_MARGIN)
Source

pub async fn get_collateral_info( &self, currency: Option<&str>, ) -> Result<CollateralInfo>

Get collateral info.

§Arguments
  • currency - Optional currency filter
Source

pub async fn get_borrow_history( &self, currency: Option<&str>, limit: Option<u32>, ) -> Result<BorrowHistory>

Get borrow history.

§Arguments
  • currency - Optional currency filter
  • limit - Optional limit (default 20)
Source§

impl BybitClient

Source

pub async fn get_coin_info( &self, coin: Option<&str>, ) -> Result<CoinInfoResponse>

Get coin info.

§Arguments
  • coin - Optional coin filter
Source

pub async fn internal_transfer( &self, coin: &str, amount: &str, from_account: &str, to_account: &str, ) -> Result<TransferResponse>

Internal transfer between accounts.

§Arguments
  • coin - Coin to transfer
  • amount - Amount to transfer
  • from_account - Source account type
  • to_account - Destination account type
Source

pub async fn get_internal_transfer_list( &self, coin: Option<&str>, limit: Option<u32>, ) -> Result<TransferList>

Get internal transfer list.

§Arguments
  • coin - Optional coin filter
  • limit - Optional limit (default 20)
Source

pub async fn get_deposit_address( &self, coin: &str, chain_type: Option<&str>, ) -> Result<DepositAddressResponse>

Get deposit address.

§Arguments
  • coin - Coin name
  • chain_type - Optional chain type
Source

pub async fn get_deposit_records( &self, coin: Option<&str>, limit: Option<u32>, ) -> Result<DepositRecords>

Get deposit records.

§Arguments
  • coin - Optional coin filter
  • limit - Optional limit (default 50)
Source

pub async fn withdraw(&self, params: WithdrawParams) -> Result<WithdrawResponse>

Withdraw funds (REQUIRES STRICT VALIDATION).

§Arguments
  • params - Withdraw parameters
§Safety

This function validates all parameters before sending to prevent fund loss.

Source

pub async fn cancel_withdraw(&self, withdraw_id: &str) -> Result<Value>

Cancel a pending withdrawal.

§Arguments
  • withdraw_id - Withdraw ID to cancel
Source

pub async fn get_withdraw_records( &self, coin: Option<&str>, limit: Option<u32>, ) -> Result<WithdrawRecords>

Get withdraw records.

§Arguments
  • coin - Optional coin filter
  • limit - Optional limit (default 50)
Source

pub async fn get_withdrawable_amount( &self, coin: &str, ) -> Result<WithdrawableAmount>

Get withdrawable amount.

§Arguments
  • coin - Coin name
Source§

impl BybitClient

Source

pub async fn get_server_time(&self) -> Result<ServerTime>

Get server time.

§Example
let client = BybitClient::testnet("key", "secret")?;
let time = client.get_server_time().await?;
println!("Server time: {}", time.time_second);
Source

pub async fn get_instruments_info( &self, category: Category, symbol: Option<&str>, ) -> Result<InstrumentsInfo>

Get instruments info.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
§Example
let client = BybitClient::testnet("key", "secret")?;
let info = client.get_instruments_info(Category::Linear, Some("BTCUSDT")).await?;
Source

pub async fn get_orderbook( &self, category: Category, symbol: &str, limit: Option<u32>, ) -> Result<Orderbook>

Get orderbook.

§Arguments
  • category - Product category
  • symbol - Symbol name
  • limit - Optional depth limit (1-500, default 25)
Source

pub async fn get_tickers( &self, category: Category, symbol: Option<&str>, ) -> Result<Tickers>

Get tickers.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter (returns all if None)
Source

pub async fn get_klines( &self, category: Category, symbol: &str, interval: Interval, limit: Option<u32>, ) -> Result<Klines>

Get klines (candlestick data).

§Arguments
  • category - Product category
  • symbol - Symbol name
  • interval - Kline interval
  • limit - Optional limit (1-1000, default 200)
Source

pub async fn get_funding_history( &self, category: Category, symbol: &str, limit: Option<u32>, ) -> Result<FundingHistory>

Get funding rate history.

§Arguments
  • category - Product category (linear or inverse)
  • symbol - Symbol name
  • limit - Optional limit (default 200)
Source

pub async fn get_recent_trades( &self, category: Category, symbol: &str, limit: Option<u32>, ) -> Result<RecentTrades>

Get recent trades.

§Arguments
  • category - Product category
  • symbol - Symbol name
  • limit - Optional limit (1-1000, default 500)
Source

pub async fn get_open_interest( &self, category: Category, symbol: &str, interval_time: &str, limit: Option<u32>, ) -> Result<OpenInterest>

Get open interest.

§Arguments
  • category - Product category (linear or inverse)
  • symbol - Symbol name
  • interval_time - Interval (5min, 15min, 30min, 1h, 4h, 1d)
  • limit - Optional limit (default 50)
Source

pub async fn get_risk_limit( &self, category: Category, symbol: Option<&str>, ) -> Result<RiskLimits>

Get risk limit info.

§Arguments
  • category - Product category (linear or inverse)
  • symbol - Optional symbol filter
Source

pub async fn get_mark_price_kline( &self, category: Category, symbol: &str, interval: Interval, limit: Option<u32>, ) -> Result<Klines>

Get mark price kline.

Source

pub async fn get_index_price_kline( &self, category: Category, symbol: &str, interval: Interval, limit: Option<u32>, ) -> Result<Klines>

Get index price kline.

Source

pub async fn get_premium_index_price_kline( &self, category: Category, symbol: &str, interval: Interval, limit: Option<u32>, ) -> Result<Klines>

Get premium index price kline.

Source§

impl BybitClient

Source

pub async fn get_positions( &self, category: Category, symbol: Option<&str>, settle_coin: Option<&str>, ) -> Result<PositionList>

Get position list.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
  • settle_coin - Optional settle coin filter (e.g., “USDT”)
Source

pub async fn set_leverage( &self, category: Category, symbol: &str, buy_leverage: &str, sell_leverage: &str, ) -> Result<Value>

Set leverage.

§Arguments
  • category - Product category
  • symbol - Symbol name
  • buy_leverage - Buy leverage
  • sell_leverage - Sell leverage
Source

pub async fn set_trading_stop(&self, params: TradingStopParams) -> Result<Value>

Set trading stop (TP/SL).

§Arguments
  • params - Trading stop parameters
Source

pub async fn switch_position_mode( &self, category: Category, mode: PositionMode, ) -> Result<Value>

Switch position mode.

§Arguments
  • category - Product category
  • mode - Position mode (0=merged, 3=both sides)
Source

pub async fn set_risk_limit( &self, category: Category, symbol: &str, risk_id: i32, ) -> Result<Value>

Set risk limit.

§Arguments
  • category - Product category
  • symbol - Symbol name
  • risk_id - Risk limit ID
Source

pub async fn add_margin( &self, category: Category, symbol: &str, margin: &str, ) -> Result<Value>

Add or reduce margin.

§Arguments
  • category - Product category
  • symbol - Symbol name
  • margin - Margin amount (positive to add, negative to reduce)
Source

pub async fn get_closed_pnl( &self, category: Category, symbol: Option<&str>, limit: Option<u32>, ) -> Result<ClosedPnlList>

Get closed PnL history.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
  • limit - Optional limit (default 20)
Source

pub async fn get_executions( &self, category: Category, symbol: Option<&str>, limit: Option<u32>, ) -> Result<ExecutionList>

Get execution list (trade history).

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
  • limit - Optional limit (default 50)
Source§

impl BybitClient

Source

pub async fn place_order( &self, params: PlaceOrderParams, ) -> Result<OrderResponse>

Place an order.

§Arguments
  • params - Order parameters
§Example
let client = BybitClient::testnet("key", "secret")?;
let params = PlaceOrderParams::market(Category::Linear, "BTCUSDT", Side::Buy, "0.01");
let result = client.place_order(params).await?;
println!("Order ID: {}", result.order_id);
Source

pub async fn amend_order( &self, params: AmendOrderParams, ) -> Result<OrderResponse>

Amend an existing order.

Source

pub async fn cancel_order( &self, params: CancelOrderParams, ) -> Result<OrderResponse>

Cancel an order.

Source

pub async fn cancel_all_orders( &self, category: Category, symbol: Option<&str>, ) -> Result<CancelAllResponse>

Cancel all orders.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter (cancel all if None)
Source

pub async fn place_batch_order( &self, category: Category, orders: Vec<PlaceOrderParams>, ) -> Result<BatchOrderResponse>

Place batch orders (up to 10 orders).

§Arguments
  • category - Product category
  • orders - List of orders to place
Source

pub async fn amend_batch_order( &self, category: Category, orders: Vec<AmendOrderParams>, ) -> Result<BatchOrderResponse>

Amend batch orders.

Source

pub async fn cancel_batch_order( &self, category: Category, orders: Vec<CancelOrderParams>, ) -> Result<BatchOrderResponse>

Cancel batch orders.

Source

pub async fn get_open_orders( &self, category: Category, symbol: Option<&str>, ) -> Result<OrdersList>

Get open orders (realtime).

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
Source

pub async fn get_order_history( &self, category: Category, symbol: Option<&str>, limit: Option<u32>, ) -> Result<OrdersList>

Get order history.

§Arguments
  • category - Product category
  • symbol - Optional symbol filter
  • limit - Optional limit (default 20)

Trait Implementations§

Source§

impl Clone for BybitClient

Source§

fn clone(&self) -> BybitClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BybitClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more