Skip to main content
The Rust crate is the canonical implementation — Python and TypeScript are thin wrappers around it.

Install

cargo add openpx
OpenPX requires tokio and serde_json:
cargo add tokio --features full
cargo add serde_json

Constructing an exchange

use openpx::ExchangeInner;
use serde_json::json;

let ex = ExchangeInner::new("polymarket", json!({
    "private_key": std::env::var("POLYMARKET_PRIVATE_KEY")?,
    "funder": std::env::var("POLYMARKET_FUNDER")?,
}))?;
ExchangeInner is enum-dispatched — match over Kalshi(_) and Polymarket(_), no dyn Exchange vtable.

Direct exchange types

If you don’t need the unified facade, construct exchanges directly:
use openpx::{Kalshi, KalshiConfig, Polymarket, PolymarketConfig};
Both implement the Exchange trait, so callers can be generic over either.

Re-exports

openpx::* re-exports everything from px-core, plus:
  • ExchangeInner and WebSocketInner — enum-dispatched facades
  • Kalshi, KalshiConfig
  • Polymarket, PolymarketConfig, PolymarketSignatureType
  • CryptoPriceWebSocket (from px-crypto)
  • SportsWebSocket (from px-sports)

WebSocket

use openpx::WebSocketInner;
use px_core::websocket::OrderBookWebSocket;
use serde_json::json;
use futures::StreamExt;

let mut ws = WebSocketInner::new("polymarket", json!({}))?;
let mut updates = ws.updates().expect("first call");
let mut session = ws.session_events().expect("first call");

ws.connect().await?;
ws.subscribe("0x…token_id").await?;

while let Some(update) = updates.next().await {
    match update? {
        px_core::WsUpdate::Snapshot { book, .. } => { /* ... */ }
        px_core::WsUpdate::Delta { changes, .. } => { /* ... */ }
        px_core::WsUpdate::Trade { trade, .. } => { /* ... */ }
        px_core::WsUpdate::Fill { fill, .. } => { /* ... */ }
        px_core::WsUpdate::Clear { reason, .. } => { /* ... */ }
    }
}

Errors

All methods return Result<T, OpenPxError>. The hierarchy is defined in engine/core/src/error.rs:
  • OpenPxError::Network
  • OpenPxError::Exchange(ExchangeError) — includes NotSupported, MarketNotFound, Authentication, InsufficientFunds, Api, RateLimited
  • OpenPxError::Config
  • OpenPxError::Serialization
  • OpenPxError::WebSocket(WebSocketError)
Per-exchange error types map into ExchangeError. See the define_exchange_error! macro for the canonical pattern.