@structbuild/sdk 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # @structbuild/sdk
2
+
3
+ TypeScript SDK for prediction market data via [api.struct.to](https://api.struct.to). Access real-time and historical data for markets, events, trades, portfolios, and more. Supports REST and WebSocket APIs with full type safety.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @structbuild/sdk
9
+ # or
10
+ pnpm add @structbuild/sdk
11
+ # or
12
+ bun add @structbuild/sdk
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { StructClient } from "@structbuild/sdk";
19
+
20
+ const client = new StructClient({
21
+ apiKey: "your-api-key",
22
+ });
23
+
24
+ const markets = await client.markets.getMarkets();
25
+ console.log(markets.data);
26
+ ```
27
+
28
+ ## REST API
29
+
30
+ The client exposes namespaced methods that map to the Struct API:
31
+
32
+ ```typescript
33
+ const client = new StructClient({
34
+ apiKey: "your-api-key",
35
+ venue: "polymarket", // default
36
+ timeout: 10000, // request timeout in ms
37
+ retry: { // auto-retry on 429/5xx
38
+ maxRetries: 3,
39
+ initialDelay: 500,
40
+ },
41
+ });
42
+ ```
43
+
44
+ ### Markets
45
+
46
+ ```typescript
47
+ const markets = await client.markets.getMarkets({ limit: 10 });
48
+ const market = await client.markets.getMarket({ condition_id: "0x..." });
49
+ const marketBySlug = await client.markets.getMarketBySlug({ slug: "will-x-happen" });
50
+ const trades = await client.markets.getTrades({ condition_id: "0x..." });
51
+ const candles = await client.markets.getCandlestick({ condition_id: "0x...", timeframe: "1d", interval: "1h" });
52
+ const metrics = await client.markets.getMarketMetrics({ condition_id: "0x..." });
53
+ const volumeChart = await client.markets.getMarketVolumeChart({ condition_id: "0x..." });
54
+ ```
55
+
56
+ ### Events
57
+
58
+ ```typescript
59
+ const events = await client.events.getEvents({ limit: 10 });
60
+ const event = await client.events.getEvent({ id: "123" });
61
+ const eventBySlug = await client.events.getEventBySlug({ slug: "us-election" });
62
+ const eventMetrics = await client.events.getEventMetrics({ event_id: "123" });
63
+ ```
64
+
65
+ ### Trader / Portfolio
66
+
67
+ ```typescript
68
+ const portfolio = await client.trader.getPortfolio({ address: "0x..." });
69
+ const positions = await client.trader.getPortfolioPositions({ address: "0x..." });
70
+ const trades = await client.trader.getTraderTrades({ address: "0x..." });
71
+ const profile = await client.trader.getTraderProfile({ address: "0x..." });
72
+ const pnl = await client.trader.getTraderPnl({ address: "0x..." });
73
+ const pnlByMarket = await client.trader.getTraderMarketPnl({ address: "0x..." });
74
+ const pnlByEvent = await client.trader.getTraderEventPnl({ address: "0x..." });
75
+ const pnlCandles = await client.trader.getTraderPnlCandles({ address: "0x..." });
76
+ const volumeChart = await client.trader.getTraderVolumeChart({ address: "0x..." });
77
+ ```
78
+
79
+ ### Holders
80
+
81
+ ```typescript
82
+ const marketHolders = await client.holders.getMarketHolders({ condition_id: "0x..." });
83
+ const eventHolders = await client.holders.getEventHolders({ event_slug: "us-election" });
84
+ const positionHolders = await client.holders.getPositionHolders({ position_id: "123" });
85
+ const history = await client.holders.getMarketHoldersHistory({ condition_id: "0x..." });
86
+ ```
87
+
88
+ ### Series
89
+
90
+ ```typescript
91
+ const series = await client.series.getSeriesList();
92
+ const detail = await client.series.getSeriesDetail({ identifier: "my-series" });
93
+ const seriesEvents = await client.series.getSeriesEvents({ identifier: "my-series" });
94
+ ```
95
+
96
+ ### Search, Tags, Bonds
97
+
98
+ ```typescript
99
+ const results = await client.search.search({ query: "election" });
100
+ const tags = await client.tags.getTags();
101
+ const bonds = await client.bonds.getBonds();
102
+ ```
103
+
104
+ ## Pagination
105
+
106
+ Use the `paginate` helper to iterate through all results:
107
+
108
+ ```typescript
109
+ import { StructClient, paginate } from "@structbuild/sdk";
110
+
111
+ const client = new StructClient({ apiKey: "your-api-key" });
112
+
113
+ for await (const market of paginate(
114
+ (params) => client.markets.getMarkets(params),
115
+ { limit: 100 },
116
+ )) {
117
+ console.log(market);
118
+ }
119
+ ```
120
+
121
+ ## Error Handling
122
+
123
+ ```typescript
124
+ import { HttpError, TimeoutError, NetworkError } from "@structbuild/sdk";
125
+
126
+ try {
127
+ await client.markets.getMarket({ condition_id: "0x..." });
128
+ } catch (error) {
129
+ if (error instanceof HttpError) {
130
+ console.log(error.status, error.body);
131
+ } else if (error instanceof TimeoutError) {
132
+ console.log("Request timed out");
133
+ } else if (error instanceof NetworkError) {
134
+ console.log("Network error");
135
+ }
136
+ }
137
+ ```
138
+
139
+ ## Request Hooks
140
+
141
+ ```typescript
142
+ const client = new StructClient({
143
+ apiKey: "your-api-key",
144
+ onRequest: (info) => {
145
+ console.log(`${info.method} ${info.url}`);
146
+ },
147
+ onResponse: (info) => {
148
+ console.log(`${info.status} in ${info.duration}ms`);
149
+ },
150
+ });
151
+ ```
152
+
153
+ ## License
154
+
155
+ MIT
@@ -0,0 +1,25 @@
1
+ import type { Venue } from "./types/common.js";
2
+ import type { RetryConfig, RequestHookInfo, ResponseHookInfo } from "./types/index.js";
3
+ import { HoldersNamespace, ScoringNamespace, TagsNamespace, EventsNamespace, MarketsNamespace, SeriesNamespace, TraderNamespace, BondsNamespace, SearchNamespace } from "./namespaces/index.js";
4
+ export interface StructClientConfig {
5
+ apiKey: string;
6
+ baseUrl?: string;
7
+ venue?: Venue;
8
+ headers?: Record<string, string>;
9
+ timeout?: number;
10
+ retry?: RetryConfig;
11
+ onRequest?: (info: RequestHookInfo) => void | Promise<void>;
12
+ onResponse?: (info: ResponseHookInfo) => void | Promise<void>;
13
+ }
14
+ export declare class StructClient {
15
+ readonly holders: HoldersNamespace;
16
+ readonly scoring: ScoringNamespace;
17
+ readonly tags: TagsNamespace;
18
+ readonly events: EventsNamespace;
19
+ readonly markets: MarketsNamespace;
20
+ readonly series: SeriesNamespace;
21
+ readonly trader: TraderNamespace;
22
+ readonly bonds: BondsNamespace;
23
+ readonly search: SearchNamespace;
24
+ constructor(config: StructClientConfig);
25
+ }
@@ -0,0 +1,24 @@
1
+ export declare class StructError extends Error {
2
+ constructor(message: string, options?: ErrorOptions);
3
+ }
4
+ export declare class HttpError extends StructError {
5
+ readonly status: number;
6
+ readonly statusText: string;
7
+ readonly body: unknown;
8
+ readonly responseHeaders: Headers | undefined;
9
+ constructor(status: number, statusText: string, body: unknown, responseHeaders?: Headers);
10
+ }
11
+ export declare class NetworkError extends StructError {
12
+ constructor(message: string, options?: ErrorOptions);
13
+ }
14
+ export declare class TimeoutError extends StructError {
15
+ constructor(timeout: number);
16
+ }
17
+ export declare class WebSocketError extends StructError {
18
+ constructor(message: string, options?: ErrorOptions);
19
+ }
20
+ export declare class WebSocketClosedError extends WebSocketError {
21
+ readonly code: number;
22
+ readonly reason: string;
23
+ constructor(code: number, reason: string);
24
+ }