7 releases (4 breaking)

0.6.1 Jan 15, 2025
0.5.0 Nov 15, 2024
0.2.2 Jul 11, 2024
0.1.4 Mar 26, 2024

#37 in WebSocket

Download history 3668/week @ 2024-10-07 3529/week @ 2024-10-14 2284/week @ 2024-10-21 4234/week @ 2024-10-28 3467/week @ 2024-11-04 1883/week @ 2024-11-11 2313/week @ 2024-11-18 1589/week @ 2024-11-25 2600/week @ 2024-12-02 2015/week @ 2024-12-09 1760/week @ 2024-12-16 1685/week @ 2024-12-23 1529/week @ 2024-12-30 2727/week @ 2025-01-06 1290/week @ 2025-01-13 99/week @ 2025-01-20

5,806 downloads per month

Apache-2.0

46KB
932 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–21MB
~286K SLoC