Quickstart Guide
Quickly establish a connection via websocket and start receiving streaming data and analytics.
Subscribing to the Endpoint
All subscriptions go through a single endpoint URL :
wss://pro.cryptostats.dev:8443/?options=:options
You can add your API key to gain access to your subscribed channels:
from websocket import create_connection
import urllib
options = {
api_key: [YOUR_API_KEY],
action: 'subscribe',
channels: [YOUR_CHANNELS]
}
options_url = urllib.parse.urlencode(options)
url = f'wss://pro.cryptostats.dev:8443/?options={options_url}'
ws = create_connection(url)
Channel subscriptions can be passed as part of the options parameters or can be sent separately after connecting to the WebSocket.
ws.send({action: 'subscribe', channel: [YOUR_CHANNEL]})
Full Implementation
Below is a working implementation subscribing to normalized trades on the BTC-PERPETUAL instrument at deribit:
# pip install websocket-client
# pip install ujson
import websocket
import ujson as json
from urllib.parse import quote
subscription = {
'action': 'subscribe',
'channel': ["deribit.BTC-PERPETUAL.trade"],
}
def on_open(msg):
print(f"Opened: {msg}")
def on_message(_, msg):
print(f"Message: {msg}")
def on_error(_, msg):
print(f"Error: {msg}")
def on_close(msg):
print(f"Closed: {msg}")
result = quote(json.dumps(subscription))
ws = websocket.WebSocketApp(f'wss://pro.cryptostats.dev:8443?options={result}',
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
"""
Helper Functions
"""
def send(action, channel):
ws.send(json.dumps({'action': action, 'channel': [channel]}))
def subscribe(channel):
send("subscribe", channel)
def unsubscribe(channel):
send("unsubscribe", channel)
ws.run_forever()
Live Runnable Example (Node.js)
Live Runnable Example (Python)
Last updated
Was this helpful?