polyoxide-clob
Rust client library for Polymarket CLOB (Central Limit Order Book) API.
Provides authenticated order creation, EIP-712 signing, and submission, plus read-only market data and order book access. Part of the polyoxide workspace.
More information about this crate can be found in the crate documentation.
Features
- Order Management: Create, sign, and post limit and market orders with EIP-712
- Market Data: Order books, prices, midpoints, spreads, last trade prices, and price history
- Account Management: Balances, allowances, trade history, and session heartbeats
- API Key Management: Create, list, and delete standard, read-only, and builder API keys
- Liquidity Rewards: Query earnings, percentages, and reward markets
- RFQ Trading: Request-for-quote creation, quoting, acceptance, and approval
- Notifications: List and dismiss user notifications
- WebSocket: Real-time market data and user order/trade updates (feature-gated)
Installation
cargo add polyoxide-clob
Feature Flags
| Feature | Default | Description |
|---|---|---|
gamma |
Yes | Enables the polyoxide-gamma dependency, used to auto-resolve proxy wallet addresses for proxy signature types |
ws |
No | Enables WebSocket support (tokio-tungstenite, futures-util) for real-time streaming |
keychain |
No | Enables OS keychain storage for credentials via keyring (macOS Keychain, Windows Credential Manager, Linux Secret Service) |
# With WebSocket support
cargo add polyoxide-clob --features ws
# Without the gamma dependency
cargo add polyoxide-clob --no-default-features
Authentication
The CLOB API uses two authentication layers:
- L1 (EIP-712): On-chain signing with a private key via
alloy. Used for API key creation/derivation. - L2 (HMAC-SHA256): Signing with API credentials. Used for order management, account operations, and all authenticated endpoints.
Both layers are managed through the Account type.
Environment Variables
POLYMARKET_PRIVATE_KEY # Hex-encoded private key
POLYMARKET_API_KEY # L2 API key
POLYMARKET_API_SECRET # L2 API secret (base64)
POLYMARKET_API_PASSPHRASE # L2 API passphrase
Usage
Client Construction
use ;
// Read-only client (no authentication, market data only)
let clob = public;
// Authenticated client from Account
let account = from_env?;
let clob = from_account?;
// Shorthand: private key + credentials directly
let clob = new?;
// Full builder control
let clob = new
.with_account
.chain
.base_url
.timeout_ms
.pool_size
.max_concurrent // default: 8
.build?;
Account Configuration
use ;
// From environment variables
let account = from_env?;
// From a JSON file
let account = from_file?;
// From the OS keychain (feature `keychain`)
let account = from_keychain?;
// Direct construction
let credentials = Credentials ;
let account = new?;
API Namespaces
The client organizes endpoints into namespaces. Public namespaces are always available; authenticated namespaces return Result<_, ClobError> and require an Account.
| Namespace | Access | Method |
|---|---|---|
markets() |
Public | clob.markets() |
health() |
Public | clob.health() |
orders() |
Authenticated | clob.orders()? |
account_api() |
Authenticated | clob.account_api()? |
auth() |
Authenticated | clob.auth()? |
rewards() |
Authenticated | clob.rewards()? |
rfq() |
Authenticated | clob.rfq()? |
notifications() |
Authenticated | clob.notifications()? |
Market Data (public)
use ;
let clob = public;
// List markets (paginated)
let markets = clob.markets.list.send.await?;
// Get a single market by condition ID
let market = clob.markets.get.send.await?;
// Order book
let book = clob.markets.order_book.send.await?;
println!;
// Price, midpoint, spread
let price = clob.markets.price.send.await?;
let mid = clob.markets.midpoint.send.await?;
let spread = clob.markets.spread.send.await?;
// Last trade price and price history
let last = clob.markets.last_trade_price.send.await?;
let history = clob.markets.prices_history.send.await?;
// Batch operations (multiple tokens at once)
use BookParams;
let params = vec!;
let books = clob.markets.order_books.await?;
Placing Orders (authenticated)
use ;
let account = from_env?;
let clob = from_account?;
// place_order: create + sign + post in one call
let params = CreateOrderParams ;
let response = clob.place_order.await?;
if response.success
// Or step-by-step: create, sign, then post
let order = clob.create_order.await?;
let signed = clob.sign_order.await?;
let response = clob.post_order.await?;
Order Management (authenticated)
// List your orders
let orders = clob.orders?.list.send.await?;
// Get a specific order
let order = clob.orders?.get.send.await?;
// Cancel a single order
let result = clob.orders?.cancel.send.await?;
// Cancel multiple orders (up to 3000)
let result = clob.orders?.cancel_many.await?;
// Cancel all open orders
let result = clob.orders?.cancel_all.await?;
// Check reward scoring status
let scoring = clob.orders?.is_scoring.send.await?;
Account Operations (authenticated)
// Token balance and allowance
let bal = clob.account_api?.balance_allowance.send.await?;
// USDC balance
let usdc = clob.account_api?.usdc_balance.send.await?;
// Trade history with filters
let trades = clob.account_api?.trades
.market
.after
.send
.await?;
// Builder trades
let builder_trades = clob.account_api?.builder_trades.send.await?;
Health and Latency
let clob = public;
// Measure API round-trip time
let latency = clob.health.ping.await?;
println!;
// Server time
let time = clob.health.server_time.send.await?;
WebSocket (feature ws)
Market Channel
Subscribe to real-time order book and price updates (no authentication required):
use ;
use StreamExt;
let mut ws = connect_market.await?;
while let Some = ws.next.await
User Channel
Subscribe to authenticated order and trade updates:
use ;
use StreamExt;
let credentials = from_env?;
let mut ws = connect_user.await?;
while let Some = ws.next.await
Auto-Ping with WebSocketBuilder
For long-running connections with automatic keep-alive:
use ;
use Duration;
let ws = new
.ping_interval
.connect_market
.await?;
ws.run.await?;
License
Licensed under either of MIT or Apache-2.0 at your option.