Skip to main content

Crate polymarket_us

Crate polymarket_us 

Source
Expand description

Unofficial Rust SDK for the Polymarket US Retail API.

The crate exposes a typed async REST client and a managed WebSocket stream. Requests to authenticated endpoints are signed with Ed25519 and carry the X-PM-* headers automatically.

§Getting started

Public endpoints need no credentials:

use polymarket_us::PolymarketUsClient;

let client = PolymarketUsClient::builder().build()?;
let markets = client.markets().list().await?;
println!("{} markets", markets.markets.len());

Authenticated endpoints read credentials from POLYMARKET_US_KEY_ID and POLYMARKET_US_SECRET_KEY:

use polymarket_us::{PolymarketUsClient, UsAuth};

let client = PolymarketUsClient::builder()
    .auth(UsAuth::from_env()?)
    .build()?;

let balances = client.account().balances().await?;

§Resources

Endpoints are grouped behind accessors on the client: PolymarketUsClient::markets, PolymarketUsClient::events, PolymarketUsClient::orders, PolymarketUsClient::account, PolymarketUsClient::portfolio, and PolymarketUsClient::search.

§Retries

Idempotent requests (GET, DELETE) are retried with exponential backoff and jitter, honouring a server-supplied Retry-After. POST is never retried automatically, so a submitted order cannot be duplicated by the transport layer. See RetryConfig.

§Streaming

PolymarketUsStreamClient maintains a WebSocket with automatic reconnect, re-subscribing on every reconnect. Connections that go silent are torn down after StreamConnectConfig::idle_timeout so a dead socket cannot stall the stream indefinitely.

use polymarket_us::{PolymarketUsStreamClient, StreamSubscription};

let stream = PolymarketUsStreamClient::from_gateway_base_url(
    "https://gateway.polymarket.us",
    None,
);

let mut managed = stream
    .connect(vec![StreamSubscription::market_data("BTC-USD")])
    .await?;

while let Some(message) = managed.next().await {
    println!("{:?}", message.kind);
}

Re-exports§

pub use auth::UsAuth;
pub use client::PolymarketUsClient;
pub use client::PolymarketUsClientBuilder;
pub use error::PolymarketUsError;
pub use resources::AccountClient;
pub use resources::EventsClient;
pub use resources::MarketsClient;
pub use resources::OrdersClient;
pub use resources::PortfolioClient;
pub use resources::SearchClient;
pub use retry::RetryConfig;
pub use stream::ManagedStream;
pub use stream::PolymarketUsStreamClient;
pub use stream::ReconnectConfig;
pub use stream::StreamConnectConfig;
pub use stream::StreamControlEvent;
pub use stream::StreamDataEvent;
pub use stream::StreamMessage;
pub use stream::StreamMessageKind;
pub use stream::StreamSubscription;
pub use stream::SubscriptionChannel;
pub use types::MarketStatus;
pub use types::OrderAction;
pub use types::OrderSide;
pub use types::OrderType;
pub use types::TimeInForce;

Modules§

auth
client
error
resources
retry
stream
types