The TypeScript SDK is a NAPI-RS native addon. Type definitions are
auto-generated.
Install
Requires Node.js 18+. Prebuilt binaries ship for common platforms.
Exchange
import { Exchange } from "openpx";
class Exchange {
constructor(id: string, config: any);
websocket(): WebSocket;
get id(): string;
get name(): string;
describe(): any;
// Auto-paginates and returns every market.
fetchMarkets(): Promise<any>;
fetchMarket(marketId: string): Promise<any>;
createOrder(
marketId: string,
outcome: string,
side: string,
price: number,
size: number,
params?: any,
): Promise<any>;
cancelOrder(orderId: string, marketId?: string): Promise<any>;
fetchOrder(orderId: string, marketId?: string): Promise<any>;
fetchOpenOrders(marketId?: string): Promise<any>;
fetchPositions(marketId?: string): Promise<any>;
fetchBalance(): Promise<any>;
fetchOrderbook(
marketId: string,
outcome?: string,
tokenId?: string,
): Promise<any>;
fetchFills(marketId?: string, limit?: number): Promise<any>;
fetchPriceHistory(
marketId: string,
interval: string,
outcome?: string,
tokenId?: string,
conditionId?: string,
startTs?: number,
endTs?: number,
): Promise<any>;
fetchTrades(
marketId: string,
marketRef?: string,
outcome?: string,
tokenId?: string,
startTs?: number,
endTs?: number,
limit?: number,
cursor?: string,
): Promise<any>;
fetchOrderbookHistory(
marketId: string,
tokenId?: string,
startTs?: number,
endTs?: number,
limit?: number,
cursor?: string,
): Promise<any>;
}
fetchMarkets() auto-paginates and returns every active market. To page
manually or filter, drop down to the underlying Rust crate (or Python).
WebSocket
The TypeScript binding exposes a callback-based API rather than a stream.
class WebSocket {
constructor(id: string, config: any);
connect(): Promise<void>;
disconnect(): Promise<void>;
subscribe(marketId: string): Promise<void>;
unsubscribe(marketId: string): Promise<void>;
get state(): string;
// Resolves when the stream ends. Callback receives each update.
onOrderbookUpdate(
marketId: string,
callback: (err: Error | null, update: any) => void,
): Promise<void>;
onActivityUpdate(
marketId: string,
callback: (err: Error | null, event: any) => void,
): Promise<void>;
}
import { Exchange } from "openpx";
const ex = new Exchange("polymarket", {
private_key: process.env.POLYMARKET_PRIVATE_KEY!,
funder: process.env.POLYMARKET_FUNDER!,
});
const ws = ex.websocket();
await ws.connect();
await ws.subscribe("0x…token_id");
await ws.onOrderbookUpdate("0x…token_id", (err, update) => {
if (err) return console.error(err);
// update.kind is "Snapshot" | "Delta" | "Clear" | "Trade" | "Fill"
});
Errors
All async methods reject with a standard Error whose message carries the
underlying OpenPxError. The string mirrors the Rust enum variant.