createPolymarketMarketDataClient()
Share public market data across application views on one connection.
Create one client per application. Matching views share stores and reads; outcomes share a public socket. Final unsubscribe releases a view. Dispose only when destroying the application owner. No CRISP credentials are required.
live-market-data workflow · All SDK methods
Import and runtime
@crisp-trade/partner-sdk/market-data — browser-safe public Polymarket HTTP and WebSocket data; no CRISP credentials.
Client setup and shared behavior
Arguments
| Argument | Presence | Released TypeScript type |
|---|---|---|
options | Optional | |
Named types are expanded under “Exact released types” below. Parameters are in call order; optional does not imply nullable.
Example
Start in your UI lifecycle and call stop on cleanup. Replace the illustrative outcome token ID.
import { createPolymarketMarketDataClient } from '@crisp-trade/partner-sdk/market-data';
const client = createPolymarketMarketDataClient();
const market = client.market({ tokenId: '123', history: false });
const unsubscribe = market.subscribe(() =>
console.log(market.getSnapshot()),
);
// On view cleanup:
unsubscribe();
// On application shutdown:
client.dispose();Returns
The exact return type is shown below; this helper does not use the ordinary HTTP response wrapper.
Return type
{
/** Raw public frames for specialized midpoint/quote reducers, on the same socket. */
subscribe: (tokens: readonly string[], observer: import("./marketStream.js").MarketStreamObserver) => {
update(tokens: readonly string[]): void;
close(): void;
};
market(query: Pick<PolymarketMarketDataOptions, "tokenId" | "history">): {
getSnapshot: () => PolymarketMarketDataSnapshot;
getServerSnapshot: () => PolymarketMarketDataSnapshot;
subscribe(listener: () => void): () => void;
};
dispose(): void;
}Errors and lifecycle
Loading, stale state and reconnect behavior. Read failures retain the last display; stop the store on cleanup.
Related HTTP operations
This utility does not directly map to one HTTP request.
Exact released types
Extracted from the shipped SDK 0.13.0, not separately maintained signatures.
Full signature
createPolymarketMarketDataClient(options?: Omit<PolymarketMarketDataOptions, "tokenId" | "history" | "signal" | "stream">): {
/** Raw public frames for specialized midpoint/quote reducers, on the same socket. */
subscribe: (tokens: readonly string[], observer: import("./marketStream.js").MarketStreamObserver) => {
update(tokens: readonly string[]): void;
close(): void;
};
market(query: Pick<PolymarketMarketDataOptions, "tokenId" | "history">): {
getSnapshot: () => PolymarketMarketDataSnapshot;
getServerSnapshot: () => PolymarketMarketDataSnapshot;
subscribe(listener: () => void): () => void;
};
dispose(): void;
}PolymarketMarketDataOptions
export interface PolymarketMarketDataOptions {
/** One outcome token per store. Create a new store when changing outcome. */
tokenId: string;
/** Default: last day, sampled every five minutes. false disables chart reads. */
history?: Omit<PolymarketPriceHistoryQuery, "tokenId"> | false;
/** Optional backend loaders when direct browser HTTP reads are unavailable. */
loadBook?: (tokenId: string, signal: AbortSignal) => Promise<MarketDataReadResult<PolymarketOrderBook>>;
loadHistory?: (query: PolymarketPriceHistoryQuery, signal: AbortSignal) => Promise<MarketDataReadResult<{
history: MarketDataPoint[];
}>>;
fetch?: typeof fetch;
createSocket?: (endpoint: string) => MarketDataSocket;
/** Share transport with catalog subscriptions and other outcome stores. */
stream?: PolymarketMarketStream;
signal?: AbortSignal;
timeoutMs?: number;
/** Refresh canonical history/REST fallback. Default 60s; minimum 1s. */
refreshIntervalMs?: number;
/** Maximum retained historical and provisional chart points. Default 2000. */
maxPoints?: number;
/** Coalesce subscriber notifications. Default 50ms. */
notifyIntervalMs?: number;
/** Initial retry delay, doubles to 30s with jitter. Default 500ms. */
reconnectDelayMs?: number;
/** Default 10s, matching upstream heartbeat requirements. */
heartbeatMs?: number;
}PolymarketPriceHistoryQuery
export interface PolymarketPriceHistoryQuery {
/** Outcome token ID, not the Gamma market ID or condition ID. */
tokenId: string;
interval?: "max" | "all" | "1m" | "1w" | "1d" | "6h" | "1h";
/** Unix seconds. Supply both bounds instead of interval. */
startTs?: number;
endTs?: number;
/** Sampling resolution in minutes. */
fidelity?: number;
}MarketDataReadResult
type MarketDataReadResult<T> = {
data: T;
fetchedAt: string;
stale: boolean;
};PolymarketOrderBook
export interface PolymarketOrderBook {
conditionId: string;
tokenId: string;
timestamp: string;
hash: string | null;
bids: Array<{
price: string;
size: string;
}>;
asks: Array<{
price: string;
size: string;
}>;
minOrderSize: string | null;
tickSize: string | null;
negRisk: boolean | null;
lastTradePrice: string | null;
}MarketDataPoint
type MarketDataPoint = {
timestamp: number;
price: number;
};MarketDataSocket
type MarketDataSocket = Pick<WebSocket, "addEventListener" | "removeEventListener" | "send" | "close">;PolymarketMarketDataSnapshot
export interface PolymarketMarketDataSnapshot {
readonly tokenId: string;
readonly connection: "idle" | "connecting" | "synchronizing" | "live" | "reconnecting" | "stopped";
readonly book: {
readonly data: PolymarketOrderBook | null;
readonly source: "rest" | "stream" | null;
readonly stale: boolean;
readonly updatedAt: string | null;
readonly error: string | null;
};
readonly chart: {
readonly points: ReadonlyArray<MarketDataPoint & {
source: "history" | "live_trade";
}>;
readonly stale: boolean;
readonly refreshing: boolean;
readonly truncated: boolean;
readonly updatedAt: string | null;
readonly error: string | null;
};
readonly lastTrade: {
readonly price: string;
readonly size: string | null;
readonly timestamp: number;
} | null;
readonly streamError: string | null;
readonly resolved: boolean;
}