|
| 1 | +# RealMarketAPI JavaScript SDK (Beta) |
| 2 | + |
| 3 | +Official beta JavaScript/TypeScript SDK for RealMarketAPI REST and WebSocket market data endpoints. |
| 4 | + |
| 5 | +Website: https://realmarketapi.com/ |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install realmarketapi-js-sdk-beta |
| 11 | +``` |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- Node.js 18+ (recommended). |
| 16 | +- RealMarketAPI key. |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +### REST |
| 21 | + |
| 22 | +```ts |
| 23 | +import { RealMarketApiClient } from "realmarketapi-js-sdk-beta"; |
| 24 | + |
| 25 | +const client = new RealMarketApiClient({ |
| 26 | + apiKey: process.env.REALMARKET_API_KEY! |
| 27 | +}); |
| 28 | + |
| 29 | +const price = await client.getPrice("EURUSD", "M1"); |
| 30 | +console.log(price.closePrice); |
| 31 | +``` |
| 32 | + |
| 33 | +### WebSocket |
| 34 | + |
| 35 | +```ts |
| 36 | +import { RealMarketApiWebSocket } from "realmarketapi-js-sdk-beta"; |
| 37 | + |
| 38 | +const ws = new RealMarketApiWebSocket({ |
| 39 | + apiKey: process.env.REALMARKET_API_KEY! |
| 40 | +}); |
| 41 | + |
| 42 | +const stop = ws.streamPrice("BTCUSDT", "M1", { |
| 43 | + onOpen: () => console.log("connected"), |
| 44 | + onMessage: msg => console.log(msg), |
| 45 | + onError: err => console.error(err), |
| 46 | + onClose: () => console.log("closed") |
| 47 | +}); |
| 48 | + |
| 49 | +setTimeout(() => stop(), 30_000); |
| 50 | +``` |
| 51 | + |
| 52 | +## Client Configuration |
| 53 | + |
| 54 | +### `RealMarketApiClient` |
| 55 | + |
| 56 | +```ts |
| 57 | +const client = new RealMarketApiClient({ |
| 58 | + apiKey: "YOUR_API_KEY", |
| 59 | + baseUrl: "https://api.realmarketapi.com", // optional |
| 60 | + fetchImpl: fetch // optional custom fetch |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +Options: |
| 65 | +- `apiKey` (required): your RealMarketAPI key. |
| 66 | +- `baseUrl` (optional): defaults to `https://api.realmarketapi.com`. |
| 67 | +- `fetchImpl` (optional): inject a custom fetch implementation. |
| 68 | + |
| 69 | +### `RealMarketApiWebSocket` |
| 70 | + |
| 71 | +```ts |
| 72 | +const ws = new RealMarketApiWebSocket({ |
| 73 | + apiKey: "YOUR_API_KEY", |
| 74 | + baseUrl: "https://api.realmarketapi.com", // optional |
| 75 | + reconnectDelayMs: 2000 // optional |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +Options: |
| 80 | +- `apiKey` (required): your RealMarketAPI key. |
| 81 | +- `baseUrl` (optional): defaults to `https://api.realmarketapi.com`. |
| 82 | +- `reconnectDelayMs` (optional): reconnect delay after disconnect, default `2000` ms. |
| 83 | + |
| 84 | +## REST API Reference |
| 85 | + |
| 86 | +### `getPrice(symbolCode, timeFrame)` |
| 87 | +- Returns latest ticker payload for one symbol/timeframe. |
| 88 | + |
| 89 | +### `getCandles(symbolCode, timeFrame)` |
| 90 | +- Returns latest candle array for one symbol/timeframe. |
| 91 | + |
| 92 | +### `getHistory(symbolCode, startTime, endTime, pageNumber?, pageSize?)` |
| 93 | +- Returns paginated historical ticker data in the provided ISO 8601 time range. |
| 94 | + |
| 95 | +### `getSymbols()` |
| 96 | +- Returns list of available symbols. |
| 97 | + |
| 98 | +### `getSma(symbolCode, timeFrame, period?)` |
| 99 | +- Returns SMA points for one symbol/timeframe. |
| 100 | + |
| 101 | +## WebSocket API Reference |
| 102 | + |
| 103 | +### `streamPrice(symbolCode, timeFrame, handlers)` |
| 104 | +- Streams live price updates. |
| 105 | +- Returns a stop function `() => void`. |
| 106 | + |
| 107 | +### `streamCandles(symbolCode, timeFrame, handlers)` |
| 108 | +- Streams live candle updates. |
| 109 | +- Returns a stop function `() => void`. |
| 110 | + |
| 111 | +Handlers: |
| 112 | +- `onMessage(message)` required. |
| 113 | +- `onError(error)` optional. |
| 114 | +- `onOpen()` optional. |
| 115 | +- `onClose()` optional. |
| 116 | + |
| 117 | +## Type Shapes |
| 118 | + |
| 119 | +```ts |
| 120 | +type ListResult<T> = { data: T[] }; |
| 121 | + |
| 122 | +type PagedResult<T> = { |
| 123 | + data: T[]; |
| 124 | + pageNumber: number; |
| 125 | + pageSize: number; |
| 126 | + totalPages: number; |
| 127 | + totalRecords: number; |
| 128 | +}; |
| 129 | + |
| 130 | +type PriceTickerResult = { |
| 131 | + symbolCode: string; |
| 132 | + timeFrame: string; |
| 133 | + openTime: string; |
| 134 | + closePrice: number; |
| 135 | + askPrice?: number; |
| 136 | + bidPrice?: number; |
| 137 | +}; |
| 138 | + |
| 139 | +type PriceCandleResult = { |
| 140 | + symbolCode: string; |
| 141 | + timeFrame: string; |
| 142 | + openTime: string; |
| 143 | + openPrice: number; |
| 144 | + highPrice: number; |
| 145 | + lowPrice: number; |
| 146 | + closePrice: number; |
| 147 | + volume: number; |
| 148 | +}; |
| 149 | + |
| 150 | +type SymbolResult = { |
| 151 | + symbolCode: string; |
| 152 | + name?: string; |
| 153 | + category?: string; |
| 154 | +}; |
| 155 | + |
| 156 | +type IndicatorPoint = { |
| 157 | + openTime: string; |
| 158 | + value: number; |
| 159 | +}; |
| 160 | +``` |
| 161 | + |
| 162 | +## Error Handling |
| 163 | + |
| 164 | +REST methods throw `Error` when the API response is not successful. |
| 165 | + |
| 166 | +```ts |
| 167 | +try { |
| 168 | + const data = await client.getSymbols(); |
| 169 | + console.log(data.data.length); |
| 170 | +} catch (error) { |
| 171 | + console.error("Request failed:", error); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +## Beta Notes |
| 176 | + |
| 177 | +- Current REST scope: price, candles, history, symbols, SMA. |
| 178 | +- Current WebSocket scope: price and candles with automatic reconnect. |
| 179 | +- API surface may expand before stable `1.0`. |
| 180 | + |
| 181 | +## Links |
| 182 | + |
| 183 | +- Main site: https://realmarketapi.com/ |
| 184 | +- API base URL default: https://api.realmarketapi.com |
0 commit comments