Cryptostats
  • What is Cryptostats?
  • Quickstart Guide
  • Streaming API
    • Normalized Data
    • Combined Orderbook
    • Arbitrage
    • Alerts
    • Streaming Statistics
    • Orderbook Dynamics
    • Liquidity
    • Optimal Execution
    • Options
    • Indices
    • Credit
    • Betas
    • Exchanges & Latency
  • REST API
    • Coming soon
  • Support
  • FAQ
  • Subscribe Now
  • Terms and Conditions
  • Privacy Policy
Powered by GitBook
On this page
  • Subscribing to the Endpoint
  • Full Implementation
  • Live Runnable Example (Node.js)
  • Live Runnable Example (Python)

Was this helpful?

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

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:

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)
const options = {
    api_key: [YOUR_API_KEY],
    action: 'subscribe',
    channels: [YOUR CHANNELS]
}

const options_URL = encodeURIComponent(JSON.stringify(options))
const url = `wss://pro.cryptostats.dev:8443/?options=${options_URL}`
const ws = new WebSocket(url)
Please see implementation here: https://github.com/Cryptostats/Cryptostats-Java

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()
const WebSocket = require('ws');

function urlencode(options) {
    return encodeURIComponent(JSON.stringify(options))
}

const subscription = {
    'action': 'subscribe',
    'channel': "deribit.BTC-PERPETUAL.trade",
}

const url = `wss://pro.cryptostats.dev:8443?options=${urlencode(subscription)}`
const ws = new WebSocket(url)

ws.on('open', function open() {
    console.log("open")
});

ws.on('close', function incoming(data) {
    console.log("Close")
    console.log(data);
});


ws.on('message', function incoming(data) {
    console.log(data);
});

// Helper Functions for subscribing to new channels
async function send(action, channel){
    const message = {
        action: action,
        channel: [channel]
    }
    ws.send(JSON.stringify(message))
}

async function subscribe(channel){
    send("subscribe", channel)
}

async function unsubscribe(channel){
    send("unsubsribe", channel)
}
Please see implementation here: https://github.com/Cryptostats/Cryptostats-Java

Live Runnable Example (Node.js)

Live Runnable Example (Python)

PreviousWhat is Cryptostats?NextNormalized Data

Last updated 4 years ago

Was this helpful?