rs_bayse is a Rust client library for the Bayse Markets API — the culturally-native prediction market platform for Africa. It provides full coverage of the REST API and WebSocket feeds with ergonomic Rust types.
- Features
- Quick Start
- Prerequisites
- Installation
- Configuration
- Usage
- Architecture
- Examples
- Running Tests
- Development
- Contributing
- License
- System — Health check, version info
- User — Lookup, login, API key management (create, list, revoke, rotate)
- Trading — Events, quotes, order placement (single & batch), portfolio, PnL, orders, mint/burn shares, activities
- Wallet — Asset balances
- Market Data — Price history, order book, ticker, trades
- Market Makers — Liquidity rewards & maker rebates
- Market Data (
/ws/v1/markets) — Real-time activity feeds, price updates, order book snapshots - User Orders (
/ws/v1/user) — Real-time fill updates for authenticated users - Asset Prices (
/ws/v1/realtime) — Live crypto and FX price feeds
- Public — No authentication required
- Session —
x-auth-token+x-device-idheaders - Read —
X-Public-Keyheader - Write —
X-Public-Key+X-Timestamp+X-Signature(HMAC-SHA256)
use bayse::prelude::*;
#[tokio::main]
async fn main() -> Result<(), BayseError> {
// Public endpoints (no auth needed)
let sys = SystemManager::new(None, None);
let healthy = sys.health().await?;
println!("API healthy: {healthy}");
// Authenticated endpoints (with API key)
let trading = TradingManager::new(
Some("your_public_key".into()),
Some("your_secret_key".into()),
);
let events = trading.list_events(Some(1), Some(20)).await?;
println!("Events: {events:#?}");
Ok(())
}- Rust 1.75+ (edition 2021)
- Tokio runtime (for async operations)
- A Bayse Markets account and API credentials (for authenticated endpoints)
Add to your Cargo.toml:
[dependencies]
rs_bayse = "0.1.0"
tokio = { version = "1", features = ["full"] }| Variable | Description | Required |
|---|---|---|
BAYSE_API_KEY |
Your public API key | For authenticated endpoints |
BAYSE_API_SECRET |
Your secret API key | For write-level endpoints |
use bayse::prelude::*;
let config = Config::default()
.with_api_key("pub_key".into(), "sec_key".into())
.with_session_token("token".into(), "device_id".into());
let trading = TradingManager::new_with_config(config);use bayse::prelude::*;
#[tokio::main]
async fn main() -> Result<(), BayseError> {
let sys = SystemManager::new(None, None);
let market_data = MarketDataManager::new(None, None);
// Health check
println!("Healthy: {}", sys.health().await?);
// Version info
println!("Version: {:?}", sys.version().await?);
// Price history
let prices = market_data
.get_price_history("event_id", None, None, None)
.await?;
println!("Prices: {prices:#?}");
// Order book
let ob = market_data.get_order_book(&["market_id"], Some(10)).await?;
println!("Order book: {ob:#?}");
// Ticker
let ticker = market_data.get_ticker("market_id").await?;
println!("Ticker: {ticker:#?}");
// Recent trades
let trades = market_data.get_trades(Some(&["market_id"]), Some(50)).await?;
println!("Trades: {trades:#?}");
Ok(())
}use bayse::prelude::*;
#[tokio::main]
async fn main() -> Result<(), BayseError> {
let api_key = std::env::var("BAYSE_API_KEY").expect("BAYSE_API_KEY required");
let api_secret = std::env::var("BAYSE_API_SECRET").expect("BAYSE_API_SECRET required");
let trading = TradingManager::new(Some(api_key.clone()), Some(api_secret));
let wallet = WalletManager::new(Some(api_key), None);
// List events
let events = trading.list_events(Some(1), Some(20)).await?;
println!("Events: {events:#?}");
// Get portfolio
let portfolio = trading.get_portfolio().await?;
println!("Portfolio: {portfolio:#?}");
// Get wallet assets
let assets = wallet.get_assets().await?;
println!("Assets: {assets:#?}");
// Place an order
let order = trading
.place_order(
"event_id",
"market_id",
serde_json::json!({
"side": "buy",
"quantity": 10,
"price": 0.55,
}),
)
.await?;
println!("Order: {order:#?}");
Ok(())
}use bayse::prelude::*;
#[tokio::main]
async fn main() {
let ws: Stream = Bayse::new(None, None);
let sub = WsSubscription::new("subscribe", "prices")
.with_event_id("your_event_id".into());
ws.subscribe_market(sub, |msg| {
println!("Market update: {msg:#?}");
Ok::<_, std::io::Error>(())
})
.await
.ok();
}use bayse::prelude::*;
#[tokio::main]
async fn main() {
let api_key = std::env::var("BAYSE_API_KEY").unwrap();
let ws: Stream = Bayse::new(Some(api_key.clone()), None);
let auth_msg = serde_json::json!({
"type": "auth",
"apiKey": api_key,
}).to_string();
let sub = WsSubscription::new("subscribe", "orders");
ws.subscribe_user(auth_msg, sub, |msg| {
println!("Order update: {msg:#?}");
Ok::<_, std::io::Error>(())
})
.await
.ok();
}The library is organised around manager types, each responsible for a domain area:
| Manager | Domain | Auth Level |
|---|---|---|
SystemManager |
Health check, version | Public |
UserManager |
Login, API keys | Session / Public |
TradingManager |
Events, orders, portfolio, PnL | Read / Write |
WalletManager |
Asset balances | Read |
MarketDataManager |
Price history, order book, ticker, trades | Public |
MarketMakerManager |
Liquidity rewards, maker rebates | Read |
Stream |
WebSocket feeds | Public / Per-message |
Every manager struct implements the Bayse trait:
pub trait Bayse {
fn new(api_key: Option<String>, secret_key: Option<String>) -> Self;
fn new_with_config(config: Config) -> Self;
}Check the examples directory:
rest_api.rs— Demonstrates public and authenticated REST endpointswebsocket.rs— Demonstrates WebSocket subscriptions
# Run REST API examples
cargo run --example rest_api
# Run WebSocket examples
cargo run --example websocket market# Run all tests
cargo test
# Run tests with logging
RUST_LOG=debug cargo test
# Run specific test
cargo test test_namepip install pre-commit
pre-commit installThe project uses GitHub Actions for CI. Configuration is in .github/workflows.
Contributions are welcome! Please follow these guidelines:
- File an issue — Discuss your proposed change before implementing.
- Fork the repo — Create a feature branch.
- Write tests — Cover new functionality.
- Run
cargo fmtandcargo clippy— Ensure code quality. - Submit a PR — Reference the issue.
- Follow Rust standard formatting (
cargo fmt). - Run
cargo clippy -- -W clippy::all -W clippy::pedanticand fix warnings. - Document all public items with doc comments.
- Use
tracingcrate for structured logging.
This project is licensed under the MIT License — see the LICENSE file for details.
This library is inspired by the excellent rs_bybit crate (Bybit V5 API bindings) and follows similar architectural patterns.