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 Mar 26, 2024

#2 in #ws

Download history 363/week @ 2024-09-25 2809/week @ 2024-10-02 3812/week @ 2024-10-09 2283/week @ 2024-10-16 3917/week @ 2024-10-23 3339/week @ 2024-10-30 2928/week @ 2024-11-06 2158/week @ 2024-11-13 2186/week @ 2024-11-20 1673/week @ 2024-11-27 2453/week @ 2024-12-04 1583/week @ 2024-12-11 2027/week @ 2024-12-18 1551/week @ 2024-12-25 1536/week @ 2025-01-01 2513/week @ 2025-01-08

7,772 downloads per month

Apache-2.0

43KB
931 lines

Build Badge Crates Badge Docs Badge License Badge

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