> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monochrome.exchange/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket: public

> Spot API: websocket: public.

## General WSS information

### Base Address

**wss\://stream.monochrome.exchange/public**

### Request Headers

The request header of the compression extension protocol must be added.

Sec-Websocket-Extensions:permessage-deflate

## Request message format

```javascript subscribe theme={null}
{
    "method": "subscribe", 
    "params": [
        "{topic}@{arg},{arg}", 
        "{topic}@{arg}"
    ], 
    "id": "{id}"    //call back ID
}
```

```javascript unsubscribe theme={null}
{
    "method": "unsubscribe", 
    "params": [
        "{topic}@{arg},{arg}"
    ], 
    "id": "{id}"   //call back ID
}
```

## Response message format

```javascript format theme={null}
{
    "id": "{id}",   //call back ID
    "code": 1,      //result 0=success;1=fail;2=listenKey invalid
    "msg": ""
}
```

```json Response-success theme={null}
{"id":"123", "code": 0, "msg": "success"}   
```

```json response-token invalid theme={null}
{"id":"123", "code": 401, "msg": "token expire"}
```

## Push message format

```javascript format theme={null}
{
    "topic": "trade",             
    "event": "trade@btc_usdt",    //title
    "data": { }                   
}
```

```json Example of transaction record (real-time push message) theme={null}
{
    "topic": "trade", 
    "event": "trade@btc_usdt", 
    "data": {
        "s": "btc_usdt",           //symbol
        "i": 6316559590087222000,  //tradeId
        "t": 1655992403617,        //time
        "oi": 6616559590087222666, //orderId
        "p": "43000",              //price
        "q": "0.21",               //quantity
        "v": "9030"                //quoteQty
        "b": true                  //whether is buyerMaker or not
    }
}
```

## Heartbeat

Each link of the client needs to send a text “ping” periodically, and the server will reply to the text “pong”. If the server does not receive a ping message from the client within 1 minute, it will actively disconnect the link.

## Subscription parameters

**format**

\{topic}@\{arg},\{arg},…

## Orderbook manage

**How to manage a local order book correctly**

1.Open a stream to wss\://stream.monochrome.exchange/public , depth\_update\@btc\_usdt

2.Buffer the events you receive from the stream.

3.Get a depth snapshot from [https://sapi.monochrome.exchange/v4/public/depth?symbol=btc\_usdt\&limit=500](https://sapi.monochrome.exchange/v4/public/depth?symbol=btc_usdt\&limit=500)

4.Drop any event where i is \<= lastUpdateId in the snapshot.

5.The first processed event should have fi \<= lastUpdateId+1 AND i >= lastUpdateId+1.

6.While listening to the stream, each new event’s fi should be equal to the previous event’s i+1.

7.The data in each event is the absolute quantity for a price level.

8.If the quantity is 0, remove the price level.

9.Receiving an event that removes a price level that is not in your local order book can happen and is normal.

Note: Due to depth snapshots having a limit on the number of price levels, a price level outside of the initial snapshot that doesn’t have a quantity change won’t have an update in the Diff.
Depth Stream. Consequently, those price levels will not be visible in the local order book even when applying all updates from the Diff.
Depth Stream correctly and cause the local order book to have some slight differences with the real order book. However,
for most use cases the depth limit of 500 is enough to understand the market and trade effectively.

## Trade record

**request**

format: trade@\{symbol}

eg: trade\@btc\_usdt

rate: real

```json Response theme={null}
{
    "topic": "trade", 
    "event": "trade@btc_usdt", 
    "data": {
        "s": "btc_usdt",           //symbol
        "i": 6316559590087222000,  //tradeId
        "t": 1655992403617,        //time
        "oi": 6616559590087222666, //orderId
        "p": "43000",              //price
        "q": "0.21",               //quantity
        "v": "9030"                //quoteQty
        "b": true                  //whether is buyerMaker or not
    }
}
```

## K-line

**request**

format: kline@\{symbol},\{interval}

interval: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M

eg: kline\@btc\_usdt,5m

rate: 1000ms

```json Response theme={null}
{
    "topic": "kline", 
    "event": "kline@btc_usdt,5m", 
    "data": {
        "s": "btc_usdt",        // symbol
        "t": 1656043200000,     // time
        "i": "5m",              // interval
        "o": "44000",           // open price
        "c": "50000",           // close price
        "h": "52000",           // highest price
        "l": "36000",           // lowest price
        "q": "34.2",            // qty(quantity)
        "v": "230000"           // volume
    }
}
```

## Limited depth

**request**

format: depth@\{symbol},\{levels}

levels: 5, 10, 20, 50

eg: depth\@btc\_usdt,20

rate: 1000ms

```json Response theme={null}
{
    "topic": "depth", 
    "event": "depth@btc_usdt,20", 
    "data": {
        "s": "btc_usdt",        // symbol
        "i": 12345678,          // updateId
        "t": 1657699200000,     // time
        "a": [                  // asks(sell order)
            [                   //[0]price, [1]quantity
                "34000",        //price
                "1.2"           //quantity 
            ], 
            [
                "34001", 
                "2.3"
            ]
        ], 
        "b": [                   // bids(buy order)
            [
                "32000", 
                "0.2"
            ], 
            [
                "31000", 
                "0.5"
            ]
        ]
    }
}
```

## Incremental depth

**request**

format: depth\_update@\{symbol}

eg: depth\_update\@btc\_usdt

rate: 100ms

```json Response theme={null}
{
    "topic": "depth_update", 
    "event": "depth_update@btc_usdt", 
    "data": {
        "s": "btc_usdt",        // symbol
        "fi": 121,              // firstUpdateId = previous lastUpdateId + 1
        "i": 123,               // lastUpdateId
        "a": [                  // asks  sell order
            [                   // [0]price, [1]quantity
                "34000",        //price
                "1.2"           //quantity
            ], 
            [
                "34001", 
                "2.3"
            ]
        ], 
        "b": [                  // bids buy order
            [
                "32000", 
                "0.2"
            ], 
            [
                "31000", 
                "0.5"
            ]
        ]
    }
}
```

## ticker

**request**

format: ticker@\{symbol}

eg: ticker\@btc\_usdt

rate: 1000ms

```json Response theme={null}
{
    "topic": "ticker", 
    "event": "ticker@btc_usdt", 
    "data": {
        "s": "btc_usdt",      // symbol
        "t": 1657586700119,   // time(Last transaction time)
        "cv": "-200",         // priceChangeValue(24 hour price change)
        "cr": "-0.02",        // priceChangeRate 24-hour price change (percentage)
        "o": "30000",         // open price
        "c": "39000",         // close price
        "h": "38000",         // highest price
        "l": "40000",         // lowest price
        "q": "4",             // quantity
        "v": "150000",        // volume
   }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.