pub struct BybitSignedRequestBuilder<'a> { /* private fields */ }Expand description
Builder for creating authenticated Bybit API requests.
This builder encapsulates the common signing workflow:
- Credential validation
- Millisecond timestamp generation
- Parameter signing with HMAC-SHA256 (hex encoded)
- Authentication header injection (X-BAPI-* headers)
- HTTP request execution
§Bybit Signature Format
Bybit uses a unique signature format:
- Sign string:
timestamp + api_key + recv_window + params - For GET: params is the query string
- For POST: params is the JSON body string
- Signature is hex-encoded HMAC-SHA256
§Example
let bybit = Bybit::new(ExchangeConfig::default())?;
let data = bybit.signed_request("/v5/account/wallet-balance")
.param("accountType", "UNIFIED")
.optional_param("coin", Some("BTC"))
.execute()
.await?;Implementations§
Source§impl<'a> BybitSignedRequestBuilder<'a>
impl<'a> BybitSignedRequestBuilder<'a>
Sourcepub fn new(bybit: &'a Bybit, endpoint: impl Into<String>) -> Self
pub fn new(bybit: &'a Bybit, endpoint: impl Into<String>) -> Self
Creates a new signed request builder.
§Arguments
bybit- Reference to the Bybit exchange instanceendpoint- API endpoint path (e.g., “/v5/account/wallet-balance”)
§Example
let bybit = Bybit::new(ExchangeConfig::default()).unwrap();
let builder = BybitSignedRequestBuilder::new(&bybit, "/v5/account/wallet-balance");Sourcepub fn method(self, method: HttpMethod) -> Self
pub fn method(self, method: HttpMethod) -> Self
Sourcepub fn param(self, key: impl Into<String>, value: impl ToString) -> Self
pub fn param(self, key: impl Into<String>, value: impl ToString) -> Self
Adds a required parameter.
§Arguments
key- Parameter namevalue- Parameter value (will be converted to string)
§Example
let bybit = Bybit::new(ExchangeConfig::default())?;
let data = bybit.signed_request("/v5/order/realtime")
.param("category", "spot")
.param("symbol", "BTCUSDT")
.execute()
.await?;Sourcepub fn optional_param<T: ToString>(
self,
key: impl Into<String>,
value: Option<T>,
) -> Self
pub fn optional_param<T: ToString>( self, key: impl Into<String>, value: Option<T>, ) -> Self
Adds an optional parameter (only if value is Some).
§Arguments
key- Parameter namevalue- Optional parameter value
§Example
let bybit = Bybit::new(ExchangeConfig::default())?;
let since: Option<i64> = Some(1234567890000);
let limit: Option<u32> = None;
let data = bybit.signed_request("/v5/order/history")
.param("category", "spot")
.optional_param("startTime", since)
.optional_param("limit", limit) // Won't be added since it's None
.execute()
.await?;Sourcepub fn params(self, params: BTreeMap<String, String>) -> Self
pub fn params(self, params: BTreeMap<String, String>) -> Self
Adds multiple parameters from a BTreeMap.
§Arguments
params- Map of parameter key-value pairs
§Example
let bybit = Bybit::new(ExchangeConfig::default())?;
let mut params = BTreeMap::new();
params.insert("category".to_string(), "spot".to_string());
params.insert("symbol".to_string(), "BTCUSDT".to_string());
let data = bybit.signed_request("/v5/order/create")
.params(params)
.execute()
.await?;Sourcepub fn body(self, body: Value) -> Self
pub fn body(self, body: Value) -> Self
Sets the request body for POST/DELETE requests.
§Arguments
body- JSON value to use as request body
§Example
let bybit = Bybit::new(ExchangeConfig::default())?;
let body = json!({
"category": "spot",
"symbol": "BTCUSDT",
"side": "Buy",
"orderType": "Limit"
});
let data = bybit.signed_request("/v5/order/create")
.method(ccxt_exchanges::bybit::signed_request::HttpMethod::Post)
.body(body)
.execute()
.await?;Sourcepub async fn execute(self) -> Result<Value>
pub async fn execute(self) -> Result<Value>
Executes the signed request and returns the response.
This method:
- Validates that credentials are configured
- Gets the current millisecond timestamp
- Signs the request with HMAC-SHA256 (hex encoded)
- Adds authentication headers (X-BAPI-*)
- Executes the HTTP request
§Bybit Signature Details
- Sign string format:
timestamp + api_key + recv_window + params - For GET requests: params is the query string
- For POST requests: params is the JSON body string
- Signature is hex-encoded (not Base64 like OKX/Bitget)
§Returns
Returns the raw serde_json::Value response for further parsing.
§Errors
- Returns authentication error if credentials are missing
- Returns network error if the request fails
- Returns parse error if response parsing fails
§Example
let bybit = Bybit::new(ExchangeConfig::default())?;
let data = bybit.signed_request("/v5/account/wallet-balance")
.param("accountType", "UNIFIED")
.execute()
.await?;
println!("Response: {:?}", data);Auto Trait Implementations§
impl<'a> Freeze for BybitSignedRequestBuilder<'a>
impl<'a> !RefUnwindSafe for BybitSignedRequestBuilder<'a>
impl<'a> Send for BybitSignedRequestBuilder<'a>
impl<'a> Sync for BybitSignedRequestBuilder<'a>
impl<'a> Unpin for BybitSignedRequestBuilder<'a>
impl<'a> !UnwindSafe for BybitSignedRequestBuilder<'a>
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