Skip to main content

BybitRawHttpClient

Struct BybitRawHttpClient 

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

Raw HTTP client for low-level Bybit API operations.

This client handles request/response operations with the Bybit API, returning venue-specific response types. It does not parse to Nautilus domain types.

Implementations§

Source§

impl BybitRawHttpClient

Source

pub fn cancel_all_requests(&self)

Cancels all pending HTTP requests.

Source

pub fn reset_cancellation_token(&self)

Replaces the cancelled token with a fresh one so subsequent requests are not immediately short-circuited.

Source

pub fn cancellation_token(&self) -> CancellationToken

Returns a clone of the current cancellation token.

Source

pub fn new( base_url: Option<String>, timeout_secs: u64, max_retries: u32, retry_delay_ms: u64, retry_delay_max_ms: u64, recv_window_ms: u64, proxy_url: Option<String>, ) -> Result<Self, BybitHttpError>

Creates a new BybitRawHttpClient using the default Bybit HTTP URL.

§Errors

Returns an error if the retry manager cannot be created.

Source

pub fn with_credentials( api_key: String, api_secret: String, base_url: Option<String>, timeout_secs: u64, max_retries: u32, retry_delay_ms: u64, retry_delay_max_ms: u64, recv_window_ms: u64, proxy_url: Option<String>, ) -> Result<Self, BybitHttpError>

Creates a new BybitRawHttpClient configured with credentials.

§Errors

Returns an error if the HTTP client cannot be created.

Source

pub fn new_with_env( api_key: Option<String>, api_secret: Option<String>, base_url: Option<String>, demo: bool, testnet: bool, timeout_secs: u64, max_retries: u32, retry_delay_ms: u64, retry_delay_max_ms: u64, recv_window_ms: u64, proxy_url: Option<String>, ) -> Result<Self, BybitHttpError>

Creates a new BybitRawHttpClient with environment variable credential resolution.

If api_key or api_secret are not provided, they will be loaded from environment variables based on the environment flags:

  • Demo: BYBIT_DEMO_API_KEY, BYBIT_DEMO_API_SECRET
  • Testnet: BYBIT_TESTNET_API_KEY, BYBIT_TESTNET_API_SECRET
  • Mainnet: BYBIT_API_KEY, BYBIT_API_SECRET
§Errors

Returns an error if the HTTP client cannot be created.

Source

pub async fn get_server_time( &self, ) -> Result<BybitServerTimeResponse, BybitHttpError>

Fetches the current server time from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_instruments<T: DeserializeOwned + BybitResponseCheck>( &self, params: &BybitInstrumentsInfoParams, ) -> Result<T, BybitHttpError>

Fetches instrument information from Bybit for a given product category.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_instruments_spot( &self, params: &BybitInstrumentsInfoParams, ) -> Result<BybitInstrumentSpotResponse, BybitHttpError>

Fetches spot instrument information from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_instruments_linear( &self, params: &BybitInstrumentsInfoParams, ) -> Result<BybitInstrumentLinearResponse, BybitHttpError>

Fetches linear instrument information from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_instruments_inverse( &self, params: &BybitInstrumentsInfoParams, ) -> Result<BybitInstrumentInverseResponse, BybitHttpError>

Fetches inverse instrument information from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_instruments_option( &self, params: &BybitInstrumentsInfoParams, ) -> Result<BybitInstrumentOptionResponse, BybitHttpError>

Fetches option instrument information from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_klines( &self, params: &BybitKlinesParams, ) -> Result<BybitKlinesResponse, BybitHttpError>

Fetches kline/candlestick data from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_recent_trades( &self, params: &BybitTradesParams, ) -> Result<BybitTradesResponse, BybitHttpError>

Fetches recent trades from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_funding_history( &self, params: &BybitFundingParams, ) -> Result<BybitFundingResponse, BybitHttpError>

Fetches funding data from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_orderbook( &self, params: &BybitOrderbookParams, ) -> Result<BybitOrderbookResponse, BybitHttpError>

Fetches orderbook from Bybit.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_open_orders( &self, category: BybitProductType, symbol: Option<String>, base_coin: Option<String>, settle_coin: Option<String>, order_id: Option<String>, order_link_id: Option<String>, open_only: Option<BybitOpenOnly>, order_filter: Option<BybitOrderFilter>, limit: Option<u32>, cursor: Option<String>, ) -> Result<BybitOpenOrdersResponse, BybitHttpError>

Fetches open orders (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§Panics

Panics if the parameter builder fails (should never happen with valid inputs).

§References
Source

pub async fn place_order( &self, request: &Value, ) -> Result<BybitPlaceOrderResponse, BybitHttpError>

Places a new order (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_wallet_balance( &self, params: &BybitWalletBalanceParams, ) -> Result<BybitWalletBalanceResponse, BybitHttpError>

Fetches wallet balance (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_account_info( &self, ) -> Result<BybitAccountInfoResponse, BybitHttpError>

Fetches account information (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_account_details( &self, ) -> Result<BybitAccountDetailsResponse, BybitHttpError>

Fetches account details (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn update_sub_api_key( &self, params: &BybitUpdateSubApiParams, ) -> Result<BybitUpdateSubApiResponse, BybitHttpError>

Modifies a sub-account API key (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn update_master_api_key( &self, params: &BybitUpdateMasterApiParams, ) -> Result<BybitUpdateMasterApiResponse, BybitHttpError>

Modifies the master API key that issued the request (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_sub_members( &self, ) -> Result<BybitSubMembersResponse, BybitHttpError>

Fetches the sub-account list (up to 1000 rows, non-paginated).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_sub_members_paged( &self, params: &BybitSubMembersPageParams, ) -> Result<BybitSubMembersPagedResponse, BybitHttpError>

Fetches a cursor-paginated sub-account list (/v5/user/submembers).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_escrow_sub_members( &self, params: &BybitSubMembersPageParams, ) -> Result<BybitEscrowSubMembersResponse, BybitHttpError>

Fetches fund-custodial sub-accounts (/v5/user/escrow_sub_members).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_sub_api_keys( &self, params: &BybitSubApiKeysParams, ) -> Result<BybitSubApiKeysResponse, BybitHttpError>

Fetches all API keys belonging to a given sub-account.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn fetch_all_sub_members_paged( &self, page_size: Option<u32>, ) -> Result<Vec<BybitSubMember>, BybitHttpError>

Fetches every sub-account page via /v5/user/submembers and returns the flattened list. Walks the cursor until Bybit signals end-of-pages.

§Errors

Returns an error if any page request fails or the response cannot be parsed.

Source

pub async fn fetch_all_escrow_sub_members( &self, page_size: Option<u32>, ) -> Result<Vec<BybitSubMember>, BybitHttpError>

Fetches every fund-custodial page via /v5/user/escrow_sub_members and returns the flattened list. Walks the cursor until Bybit signals end-of-pages.

§Errors

Returns an error if any page request fails or the response cannot be parsed.

Source

pub async fn fetch_all_sub_api_keys( &self, sub_member_id: impl Into<String>, limit: Option<u32>, ) -> Result<Vec<BybitSubApiKeyInfo>, BybitHttpError>

Fetches every page of sub-account API keys for sub_member_id and returns the flattened list. Walks the cursor until Bybit signals end-of-pages.

§Errors

Returns an error if any page request fails or the response cannot be parsed.

Source

pub async fn get_fee_rate( &self, params: &BybitFeeRateParams, ) -> Result<BybitFeeRateResponse, BybitHttpError>

Fetches trading fee rates for symbols.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn set_margin_mode( &self, margin_mode: BybitMarginMode, ) -> Result<BybitSetMarginModeResponse, BybitHttpError>

Sets the margin mode for the account.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
§Panics

Panics if required parameters are not provided (should not happen with current implementation).

§References
Source

pub async fn set_leverage( &self, product_type: BybitProductType, symbol: &str, buy_leverage: &str, sell_leverage: &str, ) -> Result<BybitSetLeverageResponse, BybitHttpError>

Sets leverage for a symbol.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
§Panics

Panics if required parameters are not provided (should not happen with current implementation).

§References
Source

pub async fn switch_mode( &self, product_type: BybitProductType, mode: BybitPositionMode, symbol: Option<String>, coin: Option<String>, ) -> Result<BybitSwitchModeResponse, BybitHttpError>

Switches position mode for a product type.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
§Panics

Panics if required parameters are not provided (should not happen with current implementation).

§References
Source

pub async fn set_trading_stop( &self, params: &BybitSetTradingStopParams, ) -> Result<BybitSetTradingStopResponse, BybitHttpError>

Sets trading stop parameters including trailing stops.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
§References
Source

pub async fn borrow( &self, coin: &str, amount: &str, ) -> Result<BybitBorrowResponse, BybitHttpError>

Manually borrows coins for margin trading.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • Insufficient collateral for the borrow.
§Panics

Panics if the parameter builder fails (should never happen with valid inputs).

§References
Source

pub async fn no_convert_repay( &self, coin: &str, amount: Option<&str>, ) -> Result<BybitNoConvertRepayResponse, BybitHttpError>

Manually repays borrowed coins without asset conversion.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • Called during the hourly interest-calculation window (mm:04:00-mm:05:30 UTC each hour).
  • Insufficient spot balance for repayment.
§Panics

Panics if the parameter builder fails (should never happen with valid inputs).

§References
Source

pub async fn repay( &self, coin: Option<&str>, amount: Option<&str>, ) -> Result<BybitRepayResponse, BybitHttpError>

Manually repays borrowed coins, converting other assets if required.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • Called during the hourly interest-calculation window (mm:04:00-mm:05:30 UTC each hour).
  • Insufficient balance for repayment.
§Panics

Panics if the parameter builder fails (should never happen with valid inputs).

§References
Source

pub async fn withdraw( &self, params: &BybitWithdrawParams, ) -> Result<BybitWithdrawResponse, BybitHttpError>

Withdraws coins from the account.

The request is sent once. Bybit’s requestId is a client identifier, not a proven duplicate-detection window, so it is not a retry key.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API key lacks withdrawal permission or is not a master UID key.
  • The address is not in the account’s withdrawal address book.
§References
Source

pub async fn get_withdrawal_records( &self, params: &BybitWithdrawalRecordsParams, ) -> Result<BybitWithdrawalRecordsResponse, BybitHttpError>

Fetches withdrawal records.

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The response cannot be parsed.
§References
Source

pub async fn fetch_all_withdrawal_records( &self, params: &BybitWithdrawalRecordsParams, ) -> Result<Vec<BybitWithdrawalRecord>, BybitHttpError>

Fetches every page of withdrawal records matching params and returns the flattened list. Walks the cursor until Bybit signals end-of-pages.

§Errors

Returns an error if any page request fails or the response cannot be parsed.

Source

pub async fn get_tickers<T: DeserializeOwned + BybitResponseCheck>( &self, params: &BybitTickersParams, ) -> Result<T, BybitHttpError>

Fetches tickers for market data.

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_trade_history( &self, params: &BybitTradeHistoryParams, ) -> Result<BybitTradeHistoryResponse, BybitHttpError>

Fetches trade execution history (requires authentication).

§Errors

Returns an error if the request fails or the response cannot be parsed.

§References
Source

pub async fn get_positions( &self, params: &BybitPositionListParams, ) -> Result<BybitPositionListResponse, BybitHttpError>

Fetches position information (requires authentication).

§Errors

Returns an error if:

  • Credentials are missing.
  • The request fails.
  • The API returns an error.
§References
Source

pub fn base_url(&self) -> &str

Returns the base URL used for requests.

Source

pub fn recv_window_ms(&self) -> u64

Returns the configured receive window in milliseconds.

Source

pub fn credential(&self) -> Option<&Credential>

Returns the API credential if configured.

Trait Implementations§

Source§

impl Clone for BybitRawHttpClient

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BybitRawHttpClient

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for BybitRawHttpClient

Source§

fn default() -> Self

Returns the “default value” for a type. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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: Sized + 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: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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<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