Skip to the content

Help / Developers

Live data over WebSocket

Prices, candles and trades as they happen, over one WebSocket. Updates are pushed to you, so streaming costs no requests after you connect.

Connect and subscribe#

Connect to /v1/stream and send one JSON message per subscription. Each names an id you choose; everything that arrives for it carries the id back, and the server confirms with { "type": "subscribed", "id": … }.

// Node, with the ws package (from our own pages the sign-in does instead)
import WebSocket from 'ws';

const ws = new WebSocket('wss://<server>/v1/stream', {
  headers: { 'x-api-key': process.env.OPENTAPEFOLIO_API_KEY },
});
ws.onopen = () => {
  ws.send(JSON.stringify({ op: 'subscribe-bars', id: 'b1', instrumentId: 'hyperliquid:BTC', resolution: '1m' }));
};
ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  if (message.type === 'bar') console.log(message.bar.close);
};

Messages#

You sendYou getWhat it is
subscribe-barsbarThe current bar as it forms, at a resolution you name; a new time means the last one closed
subscribe-tradestradesEvery trade, in batches
subscribe-tickertickerLast price and 24-hour stats; with "polled": true when it’s refreshed every 30 seconds instead of streamed (more stocks than your Alpaca plan streams at once)
subscribe-alertsalertYour alerts as they fire (needs an API key)
unsubscribeStops the subscription with that id
→ { "op": "subscribe-bars", "id": "b1", "instrumentId": "hyperliquid:BTC", "resolution": "1m" }
← { "type": "subscribed", "id": "b1" }
← { "type": "bar", "id": "b1", "bar": { "time": 1791262800000, "open": 85620.1, "high": …, "close": … } }
→ { "op": "subscribe-trades", "id": "t1", "instrumentId": "hyperliquid:BTC" }
→ { "op": "subscribe-ticker", "id": "k1", "instrumentId": "hyperliquid:ETH" }
→ { "op": "unsubscribe", "id": "b1" }

Your alerts#

subscribe-alerts needs to know who you are: connect from a server or script with your key in the x-api-key header. (Browsers can’t set headers on a WebSocket, so this works from code, not a web page.)

// Node, with the ws package
import WebSocket from 'ws';

const ws = new WebSocket('wss://<server>/v1/stream', {
  headers: { 'x-api-key': process.env.OPENTAPEFOLIO_API_KEY },
});
ws.on('open', () => ws.send(JSON.stringify({ op: 'subscribe-alerts', id: 'a1' })));
ws.on('message', (data) => console.log(JSON.parse(data.toString())));

Errors and reconnecting#

  • A problem with one message comes back as { "type": "error", "id": …, "message": …, "code": … }. The codes: unknown_instrument, too_many_subscriptions (more than 200 at once), slow_down (subscribing too fast), needs_plan and sign_in: that subscription isn’t running. id_in_use refuses a second subscribe with an id already running, which carries on. The socket stays open.
  • An unsubscribe sent before its subscription was confirmed cancels it: switching symbols quickly never leaves a stream running that you no longer want.
  • A forming bar may carry "partial": true while its earlier minutes can’t be read (the data source failing or rate-limiting): it’s built from live trades only, and its open, high, low and volume may be short until the next update without it.
  • Only our own pages may open the stream from a browser (another site’s page is refused); programs send no Origin and use a key. One address may hold 20 connections, and each may subscribe 120 times a minute.
  • Every 15 seconds the gateway sends { "type": "heartbeat", "time": … } (skip it) and a WebSocket ping. Browsers and the ws package answer pings for you; a client that doesn’t is dropped after about 30 seconds. Nothing at all for 45 seconds means the connection is dead: close it and reconnect.
  • Connections drop now and then (a deploy, a network change, or a client too slow to read, close code 1013). Reconnect after a second or two, backing off if it keeps failing, and send your subscriptions again. Fetch the bars you missed with /v1/bars.

TradingView and Pine Script are trademarks of TradingView, Inc. OpenTapefolio is not affiliated with or endorsed by TradingView.