polymarket-us
Unofficial Rust SDK for the Polymarket US Retail API.
Features
- Resource-based API — Organized into focused clients (
client.markets(),client.orders(),client.events(), etc.) - Ed25519 request signing — Automatic X-PM-* authentication headers
- Typed async REST client — Markets, events, orders, portfolio, account, and search endpoints
- Async WebSocket streaming — Market data and order updates with automatic reconnect
- Order book & pricing data — Get order books, best bid/offer, settlement prices
- Builder-based configuration — Base URLs, timeouts, custom HTTP client
- Backward compatible — All legacy methods still work (deprecated)
Installation
This crate is currently easiest to consume from source or git:
[]
= { = "../polymarket-us" }
= { = "1", = ["macros", "rt-multi-thread"] }
Or via git:
[]
= { = "https://github.com/mbordash/DRADIS", = "polymarket-us" }
= { = "1", = ["macros", "rt-multi-thread"] }
Authentication
Authenticated endpoints require:
POLYMARKET_US_KEY_IDPOLYMARKET_US_SECRET_KEY
POLYMARKET_US_SECRET_KEY must be Base64 that decodes to either:
- 64 bytes (keypair format, first 32 bytes are used as signing seed), or
- 32 bytes (raw Ed25519 seed).
Example:
Quick start
use PolymarketUsClient;
async
Resource-Based API
The SDK is organized into focused resource clients for better discoverability and maintainability:
Markets
Market discovery, order books, and pricing data.
// List markets
let markets = client.markets.list.await?;
// List with filters
let query = ;
let page = client.markets.list_with_query.await?;
// Order book and pricing
let book = client.markets.order_book.await?;
let bbo = client.markets.bbo.await?; // Best bid/offer
let settlement = client.markets.settlement_price.await?;
Events
Event-level metadata and context.
// List all events
let events = client.events.list.await?;
// Get event by ID or slug
let event = client.events.retrieve.await?;
let event = client.events.retrieve_by_slug.await?;
Orders
Complete order lifecycle management. All operations are authenticated.
use types;
let order_req = PlaceOrderRequest ;
// Place order
let order = client.orders.create.await?;
// Get open orders
let open = client.orders.open.await?;
// Modify, cancel, preview
client.orders.modify.await?;
client.orders.cancel.await?;
let estimate = client.orders.preview.await?;
// Close position
client.orders.close_position.await?;
Account
Account balances and buying power (authenticated).
let balances = client.account.balances.await?;
for balance in balances.balances
Portfolio
Holdings and activity history (authenticated).
// Get positions
let positions = client.portfolio.positions.await?;
// Get activity with pagination
let query = ;
let activities = client.portfolio.activities.await?;
Search
Full-text search across markets and events.
let query = ;
let results = client.search.search.await?;
// Search specific resource
let markets = client.search.markets.await?;
let events = client.search.events.await?;
Advanced market queries
Use list_with_query() for filters, cursors, and pagination:
use PolymarketUsClient;
use Serialize;
async
If your account tier requires authenticated access for some filters, use list_authenticated_with_query():
Streaming market data
The SDK exposes an async WebSocket client via client.streaming(). It supports reconnects,
typed subscription helpers, and dynamic subscribe/unsubscribe while connected.
use ;
async
Supported event families include:
- Market:
market_data,market_data_lite,order_book_delta,trade,heartbeat - Private:
order_snapshot,order_update,position_snapshot,position_update,balance_snapshot,balance_update
Endpoint coverage
Markets (client.markets()):
list()— List all marketslist_with_query(q)— List markets with filters/paginationlist_authenticated()— Authenticated market listinglist_authenticated_with_query(q)— Authenticated with filtersorder_book(symbol)— Get market order bookbbo(symbol)— Get best bid/offersettlement_price(symbol)— Get settlement price
Events (client.events()):
list()— List all eventslist_with_query(q)— List events with filtersretrieve(id)— Get event by IDretrieve_by_slug(slug)— Get event by slug
Orders (client.orders()):
create(req)— Create orderplace(req)— Place order (alternative endpoint)place_batch(req)— Place multiple orders atomicallyopen(q)— Get open ordersretrieve(id)— Get order by IDcancel(id, params)— Cancel ordercancel_trading(id)— Cancel via trading endpointcancel_all(params)— Cancel all ordersmodify(id, req)— Modify open orderpreview(req)— Preview order estimateclose_position(req)— Close position
Account (client.account()):
balances()— Get account balances and buying power
Portfolio (client.portfolio()):
positions()— Get positionsactivities(q)— Get activity with pagination
Search (client.search()):
search(q)— Full-text search across markets/eventsmarkets(q)— Search marketsevents(q)— Search events
Streaming (client.streaming()):
- Typed channels via
SubscriptionChannel - Subscription helpers on
StreamSubscription - Dynamic
subscribe(...)/unsubscribe(...) - Async WebSocket client with automatic reconnect and subscription replay
Backward Compatibility
All legacy methods (e.g., client.markets_list(), client.order_create()) are still available but deprecated. They're aliases to the new resource-based API. Your existing code will continue to work—migrate at your own pace:
// Old style (deprecated, but still works)
let markets = client.markets_list.await?;
// New style (preferred)
let markets = client.markets.list.await?;
Configuration
use ;
use Duration;
Error handling
use ;
async
Retries, Correlation IDs, and Rate Limits
Automatic Retries
GET and DELETE requests are automatically retried with exponential backoff and jitter.
POST requests (order creation, placement, etc.) are never retried automatically to
prevent duplicate submissions.
use ;
use Duration;
// Default: 3 retries, 200ms initial backoff, 10s cap, 25% jitter
let client = builder.build?;
// Aggressive retry for high-availability workflows
let client = builder
.retry
.build?;
// Disable retries entirely
let client = builder
.retry
.build?;
// Fine-grained control
let client = builder
.retry
.build?;
Retries occur on:
- HTTP 429 (respects
Retry-Afterheader if present) - HTTP 500, 502, 503, 504
- Transport-level errors (connection refused, timeout)
Correlation IDs
Every request automatically includes an X-Correlation-ID header (pmrs-{uuid_v4}) for
tracing requests across your logs and Polymarket support conversations.
// Custom prefix — useful to distinguish SDK requests by service/environment
let client = builder
.correlation_id_prefix
.build?;
// Sends: X-Correlation-ID: my-service-prod-550e8400-e29b-41d4-a716-446655440000
Rate Limit Awareness
When Polymarket returns a 429, the Retry-After header is parsed and surfaced in the
RateLimited error variant so your application can react precisely:
match client.markets.list.await
For idempotent endpoints, the SDK already honours this automatically — the Retry-After
duration is used directly instead of the configured backoff.
Testing
The SDK includes comprehensive unit tests for all resource clients and type serialization/deserialization:
# Run all tests
# Run with output
# Run specific test module
# Run a single test
Current test coverage includes:
- ✅ Resource client creation and type checking (6 resources × 2 tests = 12 tests)
- ✅ Request/Response serialization for all order types (typed enums + wire compatibility)
- ✅ Type deserialization for markets, events, positions, balances
- ✅ Streaming event parsing and subscription helper coverage
- ✅ Retry/backoff policy tests and builder configuration tests
Total: 55 tests, all passing
Acknowledgements
Initial implementation originated in the DRADIS project and was extracted into this crate.
- Project link:
https://github.com/mbordash/DRADIS - Attribution is kept for provenance and maintenance history.