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 :
1
wss://pro.cryptostats.dev:8443/?options=:options
Copied!
This endpoint is SSL encrypted and supported by a high capacity, low-latency C++ websocket implementation to reduce latency whilst maintaining security.
You can add your API key to gain access to your subscribed channels:
Python
JavaScript
Java
1
from websocket import create_connection
2
import urllib
3
4
options = {
5
api_key: [YOUR_API_KEY],
6
action: 'subscribe',
7
channels: [YOUR_CHANNELS]
8
}
9
10
options_url = urllib.parse.urlencode(options)
11
url = f'wss://pro.cryptostats.dev:8443/?options={options_url}'
12
ws = create_connection(url)
13
Copied!
1
const options = {
2
api_key: [YOUR_API_KEY],
3
action: 'subscribe',
4
channels: [YOUR CHANNELS]
5
}
6
7
const options_URL = encodeURIComponent(JSON.stringify(options))
8
const url = `wss://pro.cryptostats.dev:8443/?options=${options_URL}`
9
const ws = new WebSocket(url)
10
Copied!
1
Please see implementation here: https://github.com/Cryptostats/Cryptostats-Java
Copied!
Channel subscriptions can be passed as part of the options parameters or can be sent separately after connecting to the WebSocket.
1
ws.send({action: 'subscribe', channel: [YOUR_CHANNEL]})
Copied!

Full Implementation

Below is a working implementation subscribing to normalized trades on the BTC-PERPETUAL instrument at deribit:
Python
Node.js
Java
1
# pip install websocket-client
2
# pip install ujson
3
import websocket
4
import ujson as json
5
from urllib.parse import quote
6
7
subscription = {
8
'action': 'subscribe',
9
'channel': ["deribit.BTC-PERPETUAL.trade"],
10
}
11
12
13
def on_open(msg):
14
print(f"Opened: {msg}")
15
16
17
def on_message(_, msg):
18
print(f"Message: {msg}")
19
20
21
def on_error(_, msg):
22
print(f"Error: {msg}")
23
24
25
def on_close(msg):
26
print(f"Closed: {msg}")
27
28
result = quote(json.dumps(subscription))
29
ws = websocket.WebSocketApp(f'wss://pro.cryptostats.dev:8443?options={result}',
30
on_open=on_open,
31
on_message=on_message,
32
on_error=on_error,
33
on_close=on_close)
34
35
"""
36
Helper Functions
37
"""
38
39
40
def send(action, channel):
41
ws.send(json.dumps({'action': action, 'channel': [channel]}))
42
43
44
def subscribe(channel):
45
send("subscribe", channel)
46
47
48
def unsubscribe(channel):
49
send("unsubscribe", channel)
50
51
52
ws.run_forever()
53
Copied!
1
const WebSocket = require('ws');
2
3
function urlencode(options) {
4
return encodeURIComponent(JSON.stringify(options))
5
}
6
7
const subscription = {
8
'action': 'subscribe',
9
'channel': "deribit.BTC-PERPETUAL.trade",
10
}
11
12
const url = `wss://pro.cryptostats.dev:8443?options=${urlencode(subscription)}`
13
const ws = new WebSocket(url)
14
15
ws.on('open', function open() {
16
console.log("open")
17
});
18
19
ws.on('close', function incoming(data) {
20
console.log("Close")
21
console.log(data);
22
});
23
24
25
ws.on('message', function incoming(data) {
26
console.log(data);
27
});
28
29
// Helper Functions for subscribing to new channels
30
async function send(action, channel){
31
const message = {
32
action: action,
33
channel: [channel]
34
}
35
ws.send(JSON.stringify(message))
36
}
37
38
async function subscribe(channel){
39
send("subscribe", channel)
40
}
41
42
async function unsubscribe(channel){
43
send("unsubsribe", channel)
44
}
45
Copied!
1
Please see implementation here: https://github.com/Cryptostats/Cryptostats-Java
Copied!

Live Runnable Example (Node.js)

Live Runnable Example (Python)

Last modified 6mo ago