# Market Data Websocket

### Websocket Endpoints

#### Environments

There are two environments to access our authorized market data feeds:

1. **Live**

<pre><code><strong>wss://market.aquanow.io/ 
</strong></code></pre>

2. **Sandbox**

```
wss://market-staging.aquanow.io/
```

### Websocket Access and Authorization

API Authentication is done using an API key and a secret. User needs to associate the following information in the payload of the subscription message. View the [Authorization page](/aquanow/legacy/integration-guide/authentication.md) for more detail.

#### Subscribe

In order to receive data feed, users sends a *subscribe* message to the server, after successful websocket connection. Users must also indicate the channel they wish to subscribe on in the subscription request.

\
When a subscribe message is received with a pair and channel, the server will respond with a **subscription** message that lists all channels you are subscribed to.

#### There are three channels you can subscribe for:

1. `orderBook` - Orderbook across various marketplaces and exchanges.
2. `ticker` - Aggregated Open, High, Low, Close for a ticker.
3. `gbbo` - Real time best price/bids updates for subscribed ticker

**Note:** that the Subscribe request **MUST** be a JSON string. For example, in Javascript this can be achieved by using the `JSON.stringify` method.

#### Request Parameters

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

|      Name      |  Type  | Description                                                          |
| :------------: | :----: | -------------------------------------------------------------------- |
|    `apiKey`    | String | API key                                                              |
| `apiSignature` | String | Signature created using API secret                                   |
|    `channel`   | String | Channel of subscription                                              |
|     `type`     | String | Type of action; subscribe or unsubscribe                             |
|     `nonce`    | String | Numeric string timestamp in milliseconds of when the request is sent |
|     `pair`     | String | Symbol/currency pair; ex. – pair: “BTC-CAD”                          |
|     `depth`    | Number | Depth of an orderbook; ex. – depth: 5                                |
|   `precision`  | Number | <p>Precision level;<br>ex. - precision: 2</p>                        |
|  {% endtab %}  |        |                                                                      |

{% tab title="ticker" %}

|      Name      |  Type  | Description                                                          |
| :------------: | :----: | -------------------------------------------------------------------- |
|    `apiKey`    | String | API key                                                              |
| `apiSignature` | String | Signature created using API secret                                   |
|    `channel`   | String | Channel of subscription                                              |
|     `type`     | String | Type of action; subscribe or unsubscribe                             |
|     `nonce`    | String | Numeric string timestamp in milliseconds of when the request is sent |
|     `pair`     | String | Symbol/currency pair; ex. – pair: “BTC-CAD”                          |
|  {% endtab %}  |        |                                                                      |

{% tab title="gbbo" %}

|      Name      |  Type  | Description                                                          |
| :------------: | :----: | -------------------------------------------------------------------- |
|    `apiKey`    | String | API key                                                              |
| `apiSignature` | String | Signature created using API secret                                   |
|    `channel`   | String | Channel of subscription                                              |
|     `type`     | String | Type of action; subscribe or unsubscribe                             |
|     `nonce`    | String | Numeric string timestamp in milliseconds of when the request is sent |
|     `pair`     | String | Symbol/currency pair; ex. – pair: “BTC-CAD”                          |
|   `precision`  | Number | <p>Precision level;<br>ex. - precision: 2</p>                        |
|  {% endtab %}  |        |                                                                      |
|  {% endtabs %} |        |                                                                      |

#### Unsubscribe

You can unsubscribe from a channel at any time.

Example Request

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

```javascript
JSON.stringify({
  "type": "unsubscribe",      // Message Type
  "channel": 'orderBook',     // Subscription Channel
  "pair": 'BTC-USD'           // Symbol
})
```

{% endtab %}
{% endtabs %}

### Example Usage

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

```javascript
const WebSocket = require("ws"); 
const crypto = require("crypto"); 
  
const ws = new WebSocket(`wss://market.aquanow.io`); 
  
const apiKey = ""; 
const secret = ""; 
  
function subscribeToMarketData(credentials) { 
   const { apiKey, secret } = credentials; 
   const timestamp = Date.now(); 
   const payload = { 
     type: "subscribe", 
     channel: "orderBook", 
     pair: "BTC-USD", 
     nonce: timestamp, 
     depth: 5 
   }; 
   const apiSignature = crypto 
     .createHmac("sha384", secret) 
     .update(JSON.stringify(payload)) 
     .digest("hex"); 
   
   const subsOption = JSON.stringify({ 
     ...payload, 
     apiKey, 
     apiSignature 
   }); 
   
   ws.send(subsOption); 
 } 
   
 ws.on("open", () => { 
   console.log(`WS tunnel opened - trade`); 
   subscribeToMarketData({ apiKey, secret }); 
 }); 
   
 ws.on("message", data => { 
   console.log("data", data); 
 }); 

```

{% endtab %}

{% tab title="ticker" %}

```javascript
const WebSocket = require("ws"); 
const crypto = require("crypto"); 
  
const ws = new WebSocket(`wss://market.aquanow.io`); 
  
const apiKey = ""; 
const secret = ""; 
  
function subscribeToMarketData(credentials) { 
   const { apiKey, secret } = credentials; 
   const timestamp = Date.now(); 
   const payload = { 
     type: "subscribe", 
     channel: "ticker", 
     pair: "BTC-USD", 
     nonce: timestamp 
   }; 
   const apiSignature = crypto 
     .createHmac("sha384", secret) 
     .update(JSON.stringify(payload)) 
     .digest("hex"); 
   
   const subsOption = JSON.stringify({ 
     ...payload, 
     apiKey, 
     apiSignature 
   }); 
   
   ws.send(subsOption); 
 } 
   
 ws.on("open", () => { 
   console.log(`WS tunnel opened - trade`); 
   subscribeToMarketData({ apiKey, secret }); 
 }); 
   
 ws.on("message", data => { 
   console.log("data", data); 
 }); java
```

{% endtab %}

{% tab title="gbbo" %}

```javascript
const WebSocket = require("ws"); 
const crypto = require("crypto"); 
  
const ws = new WebSocket(`wss://market.aquanow.io`); 
  
const apiKey = ""; 
const secret = ""; 
  
function subscribeToMarketData(credentials) { 
   const { apiKey, secret } = credentials; 
   const timestamp = Date.now(); 
   const payload = { 
     type: "subscribe", 
     channel: "gbbo", 
     pair: "BTC-USD", 
     nonce: timestamp,
     precision: 2
   }; 
   const apiSignature = crypto 
     .createHmac("sha384", secret) 
     .update(JSON.stringify(payload)) 
     .digest("hex"); 
   
   const subsOption = JSON.stringify({ 
     ...payload, 
     apiKey, 
     apiSignature 
   }); 
   
   ws.send(subsOption); 
 } 
   
 ws.on("open", () => { 
   console.log(`WS tunnel opened - trade`); 
   subscribeToMarketData({ apiKey, secret }); 
 }); 
   
 ws.on("message", data => { 
   console.log("data", data); 
 }); javaa
```

{% endtab %}
{% endtabs %}

#### Example Responses

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

```json
{
  dateType: 'aggOB',
  lastUpdated: 1544004380004,
  includeFees: 1,
  symbol: 'BTC-USD',
  bids:
  [
    {
      "quote": "6000",            // Price
      "quantity": 2.13,           // Quote Size
      "cumulativeQuantity": 2.13  // Cumulative Sum of Quantity 
    },
    ...
  ],
  asks:
  [
    {
      "quote": "6001",            // Price
      "quantity": 1.36,           // Quote Size
      "cumulativeQuantity": 1.36  // Cumulative Sum of Quantity
    },
    ...
  ]
}
```

{% endtab %}

{% tab title="ticker" %}

```json
{   
    symbol:"BTC-USD", 
    open:9078.632669565, 
    close:9078.632669415001, 
    high:9078.632669565, 
    low:9078.632669415001, 
    itemDateTime:1560892856307
}
```

{% endtab %}

{% tab title="gbbo" %}

```json
{
    "dataType":"aggBBO",
    "lastUpdated":1561505760000,
    "symbol":"ETH-USD",
    "includeFees":1,
    "precisions":[1,2],
    "bestBid":315.47,
    "bestAsk":315.31,
    "spread":"-0.16"
}
```

{% endtab %}
{% endtabs %}


---

# 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.aquanow.io/aquanow/legacy/websockets/authenticated-websockets/market-data-websocket.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.
