polaris-data 0.1.0
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 +327 -0
- package/dist/index.cjs +751 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +323 -0
- package/dist/index.d.ts +323 -0
- package/dist/index.js +717 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
# polaris-data
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for the Polaris market data API, optimised for server-side workflows, trading scripts, and TypeScript projects.
|
|
4
|
+
|
|
5
|
+
Documentation can be found at [polaris.supply/docs](https://polaris.supply/docs).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install polaris-data
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add polaris-data
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
yarn add polaris-data
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
bun add polaris-data
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quickstart
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { PolarisClient } from "polaris-data";
|
|
29
|
+
|
|
30
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const rows = await client.events({
|
|
34
|
+
source: "binance",
|
|
35
|
+
market: "BTC-USDT",
|
|
36
|
+
from: "2024-01-01T00:00:00Z",
|
|
37
|
+
to: "2024-01-01T01:00:00Z",
|
|
38
|
+
});
|
|
39
|
+
console.log(`Fetched ${rows.length} events`);
|
|
40
|
+
} finally {
|
|
41
|
+
client.close();
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The `apiKey` is optional — omit it to use public endpoints, or set the `POLARIS_API_KEY` environment variable.
|
|
46
|
+
|
|
47
|
+
### Async disposal (Node ≥ 18 / TypeScript ≥ 5.2)
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { PolarisClient } from "polaris-data";
|
|
51
|
+
|
|
52
|
+
await using client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
53
|
+
|
|
54
|
+
const rows = await client.events({
|
|
55
|
+
source: "binance",
|
|
56
|
+
market: "BTC-USDT",
|
|
57
|
+
from: "2024-01-01T00:00:00Z",
|
|
58
|
+
to: "2024-01-01T01:00:00Z",
|
|
59
|
+
});
|
|
60
|
+
console.log(`Fetched ${rows.length} events`);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Snapshot-first architecture
|
|
64
|
+
|
|
65
|
+
Standardised historical data (`events`, `trades`, `ohlcv`, and `replay`) uses a **snapshot-first** approach that matches the Python SDK:
|
|
66
|
+
|
|
67
|
+
1. Daily `.jsonl.zst` snapshot files are discovered via `GET /snapshots` and downloaded to a local dataset root on first access.
|
|
68
|
+
2. Subsequent calls for the same date range read from the local cache — no network round-trips.
|
|
69
|
+
3. If a requested date has no available snapshot, the SDK raises a `PolarisError` rather than silently falling back.
|
|
70
|
+
|
|
71
|
+
### Local dataset root
|
|
72
|
+
|
|
73
|
+
The default root follows the platform convention so the CLI and SDK share the same files:
|
|
74
|
+
|
|
75
|
+
| Platform | Default root |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| macOS | `~/Library/Application Support/polaris` |
|
|
78
|
+
| Linux | `$XDG_DATA_HOME/polaris` or `~/.local/share/polaris` |
|
|
79
|
+
| Windows | `%APPDATA%\polaris` |
|
|
80
|
+
|
|
81
|
+
Inside the root:
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
<root>/
|
|
85
|
+
data/ # Downloaded snapshot files (key-based paths)
|
|
86
|
+
daily/ # Materialised daily artifacts: <source>/<market>/<date>.jsonl.zst
|
|
87
|
+
tmp/ # Temporary download parts
|
|
88
|
+
cache/
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Override with the `datasetRoot` constructor option or the `POLARIS_ROOT` environment variable.
|
|
92
|
+
|
|
93
|
+
## `PolarisClient`
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
new PolarisClient({
|
|
97
|
+
apiKey?: string, // optional — omit for public access, or set POLARIS_API_KEY
|
|
98
|
+
baseUrl?: string, // defaults to "https://api.polaris.supply"
|
|
99
|
+
timeout?: number, // request timeout in ms (default 30 000)
|
|
100
|
+
fetch?: FetchLike, // custom fetch for testing / proxies
|
|
101
|
+
datasetRoot?: string, // override local dataset root
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Discovery
|
|
106
|
+
|
|
107
|
+
| Method | Description |
|
|
108
|
+
| --- | --- |
|
|
109
|
+
| `health()` | Check API availability |
|
|
110
|
+
| `catalog(opts?)` | Browse supported sources and markets |
|
|
111
|
+
| `listSnapshots(opts)` | List available snapshot files for a time range |
|
|
112
|
+
|
|
113
|
+
### Historical data (snapshot-first)
|
|
114
|
+
|
|
115
|
+
| Method | Description |
|
|
116
|
+
| --- | --- |
|
|
117
|
+
| `replay(opts)` | Stream events from local snapshots as an async iterable |
|
|
118
|
+
| `events(opts)` | Return all standardised events from local snapshots |
|
|
119
|
+
| `trades(opts)` | Return trade events from local snapshots |
|
|
120
|
+
| `ohlcv(opts)` | Aggregate OHLCV bars from local snapshots |
|
|
121
|
+
| `ohlcvTradingView(opts)` | TradingView-shaped OHLCV from local snapshots |
|
|
122
|
+
|
|
123
|
+
All snapshot-based methods require `from` and `to` (ISO 8601 strings, `Date`, or epoch microseconds).
|
|
124
|
+
|
|
125
|
+
### Raw data (API-only)
|
|
126
|
+
|
|
127
|
+
| Method | Description |
|
|
128
|
+
| --- | --- |
|
|
129
|
+
| `raw(opts)` | Raw venue-native payloads via `/raw` (requires API key) |
|
|
130
|
+
|
|
131
|
+
### Downloads
|
|
132
|
+
|
|
133
|
+
| Method | Description |
|
|
134
|
+
| --- | --- |
|
|
135
|
+
| `downloadSnapshot(opts)` | Download a snapshot file (returns native `Response`) |
|
|
136
|
+
| `getSnapshotDownloadUrl(opts)` | Get a pre-signed download URL |
|
|
137
|
+
|
|
138
|
+
## Examples
|
|
139
|
+
|
|
140
|
+
### Catalog
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { PolarisClient } from "polaris-data";
|
|
144
|
+
|
|
145
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
146
|
+
|
|
147
|
+
const catalog = await client.catalog();
|
|
148
|
+
console.log(catalog);
|
|
149
|
+
|
|
150
|
+
const markets = await client.catalog({ source: "hyperliquid" });
|
|
151
|
+
console.log(markets.sources[0].markets.map((m) => m.id));
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Events & trades (from local snapshots)
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { PolarisClient } from "polaris-data";
|
|
158
|
+
|
|
159
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
160
|
+
|
|
161
|
+
// First call downloads the snapshot; subsequent calls read locally
|
|
162
|
+
const rows = await client.events({
|
|
163
|
+
source: "binance",
|
|
164
|
+
market: "BTC-USDT",
|
|
165
|
+
from: "2024-01-01T00:00:00Z",
|
|
166
|
+
to: "2024-01-01T01:00:00Z",
|
|
167
|
+
});
|
|
168
|
+
console.log(rows.length);
|
|
169
|
+
|
|
170
|
+
const trades = await client.trades({
|
|
171
|
+
source: "binance",
|
|
172
|
+
market: "BTC-USDT",
|
|
173
|
+
from: "2024-01-01T00:00:00Z",
|
|
174
|
+
to: "2024-01-01T01:00:00Z",
|
|
175
|
+
});
|
|
176
|
+
console.log(trades.length);
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Replay (streaming from local snapshots)
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import { PolarisClient } from "polaris-data";
|
|
183
|
+
|
|
184
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
185
|
+
|
|
186
|
+
let count = 0;
|
|
187
|
+
for await (const row of client.replay({
|
|
188
|
+
source: "binance",
|
|
189
|
+
market: "BTC-USDT",
|
|
190
|
+
from: "2024-01-01T00:00:00Z",
|
|
191
|
+
to: "2024-01-01T01:00:00Z",
|
|
192
|
+
})) {
|
|
193
|
+
count++;
|
|
194
|
+
}
|
|
195
|
+
console.log(`Replayed ${count} rows`);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### OHLCV (aggregated from local snapshots)
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import { PolarisClient } from "polaris-data";
|
|
202
|
+
|
|
203
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
204
|
+
|
|
205
|
+
// Array of bars
|
|
206
|
+
const bars = await client.ohlcv({
|
|
207
|
+
source: "hyperliquid",
|
|
208
|
+
market: "BTC",
|
|
209
|
+
from: "2024-01-01T00:00:00Z",
|
|
210
|
+
to: "2024-01-02T00:00:00Z",
|
|
211
|
+
interval: "1m",
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// TradingView format
|
|
215
|
+
const tv = await client.ohlcvTradingView({
|
|
216
|
+
source: "hyperliquid",
|
|
217
|
+
market: "BTC",
|
|
218
|
+
from: "2024-01-01T00:00:00Z",
|
|
219
|
+
to: "2024-01-02T00:00:00Z",
|
|
220
|
+
interval: "1m",
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Supported intervals: `100ms`, `1s`, `10s`, `1m`, `5m`, `15m`, `1h`.
|
|
225
|
+
|
|
226
|
+
### Snapshots
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import { PolarisClient } from "polaris-data";
|
|
230
|
+
|
|
231
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
232
|
+
|
|
233
|
+
// List available snapshots
|
|
234
|
+
const snapshots = await client.listSnapshots({
|
|
235
|
+
source: "hyperliquid",
|
|
236
|
+
market: "BTC-USD",
|
|
237
|
+
from: "2024-01-01T00:00:00Z",
|
|
238
|
+
to: "2024-01-07T00:00:00Z",
|
|
239
|
+
});
|
|
240
|
+
for (const s of snapshots) {
|
|
241
|
+
console.log(s.key, s.filename);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Download a snapshot file
|
|
245
|
+
const response = await client.downloadSnapshot({
|
|
246
|
+
key: "snapshots/standard/hyperliquid/BTC-USD/2024-01-01.jsonl.zst",
|
|
247
|
+
});
|
|
248
|
+
const buffer = await response.arrayBuffer();
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Error handling
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import {
|
|
255
|
+
PolarisClient,
|
|
256
|
+
PolarisError,
|
|
257
|
+
RateLimitedError,
|
|
258
|
+
UnauthorizedError,
|
|
259
|
+
} from "polaris-data";
|
|
260
|
+
|
|
261
|
+
const client = new PolarisClient({ apiKey: "polaris_key_your_key" });
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
await client.events({
|
|
265
|
+
source: "binance",
|
|
266
|
+
market: "BTC-USDT",
|
|
267
|
+
from: "2024-01-01T00:00:00Z",
|
|
268
|
+
to: "2024-01-01T01:00:00Z",
|
|
269
|
+
});
|
|
270
|
+
} catch (err) {
|
|
271
|
+
if (err instanceof UnauthorizedError) {
|
|
272
|
+
console.error("API key is required");
|
|
273
|
+
} else if (err instanceof RateLimitedError) {
|
|
274
|
+
console.error(`Rate limited. Reset at: ${err.resetAt}`);
|
|
275
|
+
} else if (err instanceof PolarisError) {
|
|
276
|
+
console.error(`Polaris error: ${err.message} (status=${err.statusCode})`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Error classes
|
|
282
|
+
|
|
283
|
+
| Class | HTTP status | When |
|
|
284
|
+
| --- | --- | --- |
|
|
285
|
+
| `UnauthorizedError` | 401 | Missing or invalid API key |
|
|
286
|
+
| `NotFoundError` | 404 | Resource not found |
|
|
287
|
+
| `RateLimitedError` | 429 | Too many requests; check `resetAt` |
|
|
288
|
+
| `StreamDecodeError` | — | Failed to decode a streamed response |
|
|
289
|
+
| `DownloadNotAllowedError` | — | Server policy blocks the download |
|
|
290
|
+
|
|
291
|
+
All errors extend `PolarisError` which extends `Error`.
|
|
292
|
+
|
|
293
|
+
## Types
|
|
294
|
+
|
|
295
|
+
The SDK ships with full TypeScript definitions. Key types:
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
import type {
|
|
299
|
+
// Events
|
|
300
|
+
StandardEvent,
|
|
301
|
+
TradeEvent,
|
|
302
|
+
TradeData,
|
|
303
|
+
|
|
304
|
+
// OHLCV
|
|
305
|
+
OhlcvBar,
|
|
306
|
+
OhlcvInterval,
|
|
307
|
+
|
|
308
|
+
// Snapshots
|
|
309
|
+
SnapshotEntry,
|
|
310
|
+
SnapshotsResponse,
|
|
311
|
+
|
|
312
|
+
// Catalog
|
|
313
|
+
CatalogResponse,
|
|
314
|
+
CatalogSource,
|
|
315
|
+
CatalogMarket,
|
|
316
|
+
} from "polaris-data";
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## Runtime dependencies
|
|
320
|
+
|
|
321
|
+
- **`fzstd`** — Pure JavaScript zstd decompression for `.jsonl.zst` snapshot files.
|
|
322
|
+
|
|
323
|
+
No other runtime dependencies. HTTP is handled by the native `fetch` API (Node.js 18+, Deno, Bun).
|
|
324
|
+
|
|
325
|
+
## License
|
|
326
|
+
|
|
327
|
+
MIT
|