# Quickstart Guide

## Subscribing to the Endpoint

All subscriptions go through a single endpoint URL :

```markup
wss://pro.cryptostats.dev:8443/?options=:options
```

{% hint style="info" %}
&#x20;This endpoint is SSL encrypted and supported by a high capacity, low-latency C++ websocket implementation to reduce latency whilst maintaining security.
{% endhint %}

You can add your API key to gain access to your subscribed channels:

{% tabs %}
{% tab title="Python" %}

```python
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)

```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
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)

```

{% endtab %}

{% tab title="Java" %}

```
Please see implementation here: https://github.com/Cryptostats/Cryptostats-Java
```

{% endtab %}
{% endtabs %}

Channel subscriptions can be passed as part of the *options* parameters or can be sent separately after connecting to the WebSocket.

```markup
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:

{% tabs %}
{% tab title="Python" %}

```python
# 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()

```

{% endtab %}

{% tab title="Node.js" %}

```javascript
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)
}

```

{% endtab %}

{% tab title="Java" %}

```
Please see implementation here: https://github.com/Cryptostats/Cryptostats-Java
```

{% endtab %}
{% endtabs %}

## Live Runnable Example (Node.js)

{% embed url="<https://runkit.com/cryptostats/cryptostats-pro-quickstart>" %}

## Live Runnable Example (Python)

{% embed url="<https://replit.com/@cryptostats/CryptostatsQuickstart>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cryptostats.dev/quickstart-guide.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
