7 releases (4 breaking)
new 0.6.1 | Jan 15, 2025 |
---|---|
0.5.0 | Nov 15, 2024 |
0.2.2 | Jul 11, 2024 |
0.1.4 |
|
#2 in #ws
7,772 downloads per month
43KB
931 lines
Backpack Exchange API Crate
This crate provides both REST and WebSocket APIs for interacting with the Backpack Exchange:
Features
- REST API: Access public and private (authenticated) endpoints.
- WebSocket API: Subscribe to private streams for real-time updates (requires
ws
feature).
The official API documentation is available at https://docs.backpack.exchange/.
Installation
Add this crate to your Cargo.toml
:
[dependencies]
bpx_api_client = "x.y.z" # Replace with the latest version
To enable WebSocket support:
[dependencies]
bpx_api_client = { version = "x.y.z", features = ["ws"] }
Usage
REST API example:
use bpx_api_client::{BpxClient, BACKPACK_API_BASE_URL};
use std::env;
#[tokio::main]
async fn main() {
let base_url = env::var("BASE_URL").unwrap_or_else(|_| BACKPACK_API_BASE_URL.to_string());
let secret = env::var("SECRET").expect("Missing SECRET environment variable");
let client = BpxClient::init(base_url, secret, None)
.expect("Failed to initialize Backpack API client");
match client.get_open_orders(Some("SOL_USDC")).await {
Ok(orders) => println!("Open Orders: {:?}", orders),
Err(err) => tracing::error!("Error: {:?}", err),
}
}
WebSocket API example:
use anyhow::Result;
use bpx_api_client::{BpxClient, BACKPACK_API_BASE_URL, BACKPACK_WS_URL};
use bpx_api_types::rfq::RequestForQuote;
use std::env;
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> Result<()> {
let base_url = env::var("BASE_URL").unwrap_or_else(|_| BACKPACK_API_BASE_URL.to_string());
let ws_url = env::var("WS_URL").unwrap_or_else(|_| BACKPACK_WS_URL.to_string());
let secret = env::var("SECRET").expect("Missing SECRET environment variable");
let client = BpxClient::init_with_ws(base_url, ws_url, &secret, None)?;
let (tx, mut rx) = mpsc::channel::<RequestForQuote>(100);
tokio::spawn(async move {
while let Some(rfq) = rx.recv().await {
println!("Received RFQ: {:?}", rfq);
}
});
client.subscribe_to_rfqs(tx).await;
Ok(())
}
Development
This project uses Just to manage various build and development tasks.
To see the available commands, run:
just
Dependencies
~8–20MB
~286K SLoC