polymarket-rs
A modern, type-safe Rust client library for the Polymarket CLOB (Central Limit Order Book) and Data API.
This project is a complete rewrite of polymarket-rs-client with improved ergonomics, additional API methods, and removal of generic type parameters for a cleaner API surface.
Features
- Full Authentication Support - L1 (EIP-712) and L2 (HMAC) authentication
- WebSocket Streaming - Real-time market data and user events with automatic reconnection
- Builder Pattern - Fluent APIs for configuration and order creation
- Async/Await - Built on
tokiofor high-performance async operations - Decimal Precision - Accurate financial calculations with
rust_decimal - Modular Design - Separated clients for different operations
- Zero Panics - Comprehensive error handling with custom
Resulttypes
Installation
Add this to your Cargo.toml:
[]
= { = "https://github.com/pawsengineer/polymarket-rs.git" }
Quick Start
Client Types
| Client | Purpose | Authentication |
|---|---|---|
ClobClient |
CLOB market data queries | None |
DataClient |
Position and portfolio data | None |
GammaClient |
Market discovery and metadata | None |
AuthenticatedClient |
API key management, account operations | L1 (EIP-712) or L2 (HMAC) |
TradingClient |
Order creation, cancellation, trade queries | L2 (HMAC) |
Public Market Data
Query market data without authentication:
use ;
let client = new;
let token_id = new;
// Get midpoint price, order books, spreads, etc.
let midpoint = client.get_midpoint.await?;
let book = client.get_order_book.await?;
See examples/clob_data.rs and examples/public_data.rs for complete examples.
Market Discovery (Gamma API)
Discover markets with rich metadata including events, categories, tags, and volume metrics:
use ;
let client = new;
// Get active markets with filtering
let params = new
.with_active
.with_limit;
let markets = client.get_markets.await?;
// Get market by ID
let market = client.get_market_by_id.await?;
// Get events, series, tags, and categories
let events = client.get_events.await?;
let series = client.get_series.await?;
let tags = client.get_tags.await?;
let categories = client.get_categories.await?;
The Gamma API provides comprehensive market metadata for discovery and filtering. All endpoints are public and require no authentication.
See examples/gamma_markets.rs for complete examples.
Authenticated Trading
Three-step process for authenticated trading:
use ;
// 1. Create or derive API credentials
let auth_client = new;
let api_creds = auth_client.create_or_derive_api_key.await?;
// 2. Create trading client with order builder
let order_builder = new;
let trading_client = new;
// 3. Create and post orders
let order_args = new;
trading_client.create_and_post_order.await?;
PolyProxy & PolyGnosisSafe Wallets: For proxy wallets, pass the proxy address to AuthenticatedClient and use SignatureType::PolyGnosisSafe in OrderBuilder. Proxy wallets have automatic allowance management.
See examples/authenticated_trading.rs for complete examples including proxy wallet setup.
WebSocket Streaming
Real-time market data and user events with automatic reconnection:
use ;
use StreamExt;
let client = new;
let config = default;
let mut stream = new;
while let Some = stream.next.await
See examples/websocket_market.rs and examples/websocket_user.rs for complete streaming examples.
Examples
Run examples from the examples/ directory:
# Public market data
# Market discovery (Gamma API)
# Authenticated trading
PRIVATE_KEY="0x..."
# WebSocket streaming
PRIVATE_KEY="0x..."
License
Licensed under either of:
- MIT license (LICENSE-MIT)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Disclaimer
This is an unofficial library and is not affiliated with Polymarket. Use at your own risk. Always test with small amounts first on testnet before using real funds.