Skip to main content

polymarket_client/
lib.rs

1//! Unified Polymarket Rust SDK.
2//!
3//! Modeled after the official TypeScript SDK ([`@polymarket/client`](https://github.com/Polymarket/ts-sdk)).
4//!
5//! # Clients
6//!
7//! - [`PublicClient`] — read-only discovery, market data, and (with `account`) portfolio reads
8//! - [`SecureClient`] — authenticated trading, notifications, rewards, CTF wallet ops, and user websockets
9//!
10//! # Feature flags
11//!
12//! | Feature | Enables |
13//! |---------|---------|
14//! | *(default)* | HTTP discovery + CLOB market data |
15//! | `account` | Data API reads on [`PublicClient`] |
16//! | `websockets` | Realtime `subscribe()` on [`PublicClient`] |
17//! | `secure` | `account` + `websockets` + [`SecureClient`] trading and wallet ops |
18//!
19//! # Quickstart
20//!
21//! ```no_run
22//! use polymarket_client::{Environment, PublicClient};
23//!
24//! #[tokio::main]
25//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
26//!     let client = PublicClient::new(Environment::production());
27//!     let mut paginator = client.list_markets(polymarket_client::ListMarketsRequest {
28//!         closed: Some(false),
29//!         page_size: Some(5),
30//!         ..Default::default()
31//!     })?;
32//!     let page = paginator.first_page().await?;
33//!     for market in &page.items {
34//!         println!("{}: {}", market.id, market.question.as_deref().unwrap_or(""));
35//!     }
36//!     Ok(())
37//! }
38//! ```
39//!
40//! # Realtime (websockets)
41//!
42//! ```no_run
43//! # #[cfg(feature = "websockets")]
44//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
45//! use futures::StreamExt as _;
46//! use polymarket_client::{Environment, MarketSubscription, PublicClient, SubscriptionSpec};
47//!
48//! let client = PublicClient::new(Environment::production());
49//! let mut handle = client.subscribe(vec![SubscriptionSpec::Market(MarketSubscription {
50//!     token_ids: vec!["123".into()],
51//!     custom_feature_enabled: false,
52//! })])?;
53//! if let Some(Ok(event)) = handle.next().await {
54//!     println!("{event:?}");
55//! }
56//! handle.close();
57//! # Ok(())
58//! # }
59//! ```
60
61#![deny(unsafe_code)]
62
63mod environment;
64mod error;
65mod http;
66mod pagination;
67mod params;
68mod public_client;
69
70#[cfg(feature = "account")]
71pub(crate) mod account;
72#[cfg(feature = "account")]
73pub(crate) mod account_client;
74
75#[cfg(feature = "secure")]
76mod secure;
77
78#[cfg(feature = "websockets")]
79mod subscriptions;
80
81pub use environment::Environment;
82pub use error::{
83    unexpected_response, user_input, Error, FetchMarketError, FetchMidpointError,
84    FetchOrderBookError, ListEventsError, ListMarketsError, RateLimitError, RequestRejectedError,
85    TransportError, UnexpectedResponseError, UserInputError,
86};
87pub use pagination::{Page, Paginator};
88pub use polymarket_bindings::clob::{OrderBook, OrderBookLevel};
89pub use polymarket_bindings::gamma::{Event, Market};
90pub use polymarket_bindings::{OrderSide, OrderType};
91pub use polymarket_types::{
92    CtfConditionId, DecimalString, EventId, EvmAddress, MarketId, PaginationCursor, TokenId,
93};
94pub use public_client::{
95    FetchMarketRequest, FetchMidpointRequest, FetchOrderBookRequest, ListEventsRequest,
96    ListMarketsRequest, PublicClient, PublicClientBuilder,
97};
98
99#[cfg(feature = "account")]
100pub use account::{
101    Activity, FetchPortfolioValueError, FetchPortfolioValueRequest, ListActivityError,
102    ListActivityRequest, ListPositionsError, ListPositionsRequest, PortfolioValue, Position,
103};
104
105#[cfg(feature = "account")]
106pub use account_client::{ListActivityPaginator, ListPositionsPaginator};
107
108#[cfg(feature = "secure")]
109pub use polymarket_client_sdk_v2::PRIVATE_KEY_VAR;
110
111#[cfg(feature = "secure")]
112pub use secure::{
113    AccountTrade, ApiCredentials, BuildSecureClientError, CancelMarketOrdersRequest,
114    CancelOrderError, CancelOrderRequest, CancelOrderResponse, CurrentReward,
115    FetchNotificationsError, FetchOrderError, FetchOrderRequest, FetchOrderScoringError,
116    FetchOrderScoringRequest, ListAccountTradesError, ListAccountTradesRequest,
117    ListCurrentRewardsError, ListOpenOrdersError, ListOpenOrdersRequest, MarketOrderType,
118    MergePositionsRequest, Notification, OpenOrder, PlaceLimitOrderRequest,
119    PlaceMarketOrderRequest, PlaceOrderError, PlaceOrderResponse, RedeemPositionsRequest,
120    SecureClient, SecureClientBuilder, SetupTradingApprovalsError, SplitPositionRequest,
121    TransactionOutcome, WalletOperationError,
122};
123
124#[cfg(feature = "websockets")]
125pub use subscriptions::{
126    CommentsSubscription, CryptoPricesSubscription, EquityPricesSubscription, MarketStreamEvent,
127    MarketSubscription, SportsStreamEvent, StreamEvent, SubscribeError, SubscriptionHandle,
128    SubscriptionSpec, UserStreamEvent, UserSubscription,
129};