WebSocket Streams

Connection Timeout

    - The server closes the WebSocket connection after 60 seconds of inactivity (Connection Keep-Alive)

    - Inactivity is defined as the absence of requests sent by the client

Connection Keep-Alive

To keep the WebSocket connection active, you need to

    - Send periodic requests every 50 seconds

    - Properly handle potential disconnections in your application's logic

Authorization

Introduction

Before signing any WebSocket requests, you must create an API key in your Cryptomus personal account

Detailed instructions for creating an API key are available here

Obtaining a One-Time Token for Connection

Before connecting to the WebSocket server, you need to obtain a one-time authentication token

You can obtain the token by sending the corresponding request

The token is valid for 5 minutes or until the first successful connection

Connecting to the WebSocket Server

To connect to the WebSocket server

1. Obtain a one-time authentication token

2. Specify the token in the query parameter token when establishing the connection

Connection URL example: wss://api-ws.cryptomus.com/ws?token=b0b1cacd26993392ecf072155bd1686c96e380ad12cefa564aa6665c85c77334

The token can only be used once. For each new connection, you need to request a new token

After successful token verification, the connection will be established, and you can subscribe to available channels

Obtaining a One-Time Authentication Token for WebSocket Connection

Allows generating a one-time authentication token for connecting to the WebSocket server. The token is valid until the first successful connection or until it expires (5 minutes).

GET
/v2/user-api/exchange/account/web-socket/token
Copy

Authorization Headers

Full information about the required authorization headers is available here

Response

{ "token": "168b34a3a12370740ff1654f0d0c4726ff1bba8c4675edc0d7553f15e5a2a094" }
Copy

Possible codes

200: The order was successfully created

404: User or resource not found

500: Internal server error

Symbols of the depth scale

Request

GET
https://exchange-ws.cryptomus.com/api/v2/symbol-scales?symbol={symbol}
Copy

Response


1{
2  "status": "success",
3  "message": "success",
4  "data": [
5    {
6      "scale": "0.01",
7      "index": 0 // index
8    },
9    ...
10  ]
11}
Copy

Market list

Request

GET
https://api-app.cryptomus.com/v1/exchange/market
Copy

Response


1{
2  "result": [
3    {
4      "id": "01HSBPVS17CT2GN2FV34K3AMP9",
5      "symbol": "BTC_USDT",
6      "baseCurrency": "BTC",
7      "quoteCurrency": "USDT",
8      "baseMinSize": "0.0000100000000000",
9      "quoteMinSize": "0.1000000000000000",
10      "baseMaxSize": "10000000000.0000000000000000",
11      "quoteMaxSize": "99999999.0000000000000000",
12      "basePrec": "8",
13      "quotePrec": "6",
14      "baseCurrencyFullName": "Bitcoin",
15      "quoteCurrencyFullName": "Tether USD"
16    },
17    ...
18  ]
19}
Copy

Example Implementation

Node.js


1const WebSocket = require('ws');
2
3class CryptomusWebSocket {
4    constructor(token) {
5        this.token = token;
6        this.socket = new WebSocket('wss://api-ws.cryptomus.com/ws?token=${this.token}');
7        this.setupConnection();
8    }
9
10    setupConnection() {
11        this.socket.on('open', () => {
12            console.log('Connected to Cryptomus WebSocket');
13            this.startPing();
14        });
15
16        this.socket.on('message', (data) => {
17            console.log('Received:', data.toString());
18        });
19    }
20
21    ping() {
22        if (this.socket.readyState === WebSocket.OPEN) {
23            this.socket.send(JSON.stringify({
24                id: 0,
25                method: "ping",
26                params: []
27            }));
28        }
29    }
30
31    startPing() {
32        setInterval(() => {
33            this.ping();
34        }, 50 * 1000); // Every 50 seconds
35    }
36}
37
38// Usage
39const token = "your_auth_token_here";
40const client = new CryptomusWebSocket(token);
41            
Copy

Python


1import asyncio
2import websockets
3import json
4
5class CryptomusWebSocket:
6    def __init__(self, token):
7        self.token = token
8        self.socket = None
9    
10    async def connect(self):
11        self.socket = await websockets.connect(f"wss://api-ws.cryptomus.com/ws?token={self.token}")
12        print('Connected to Cryptomus WebSocket')
13    
14    async def ping(self):
15        await self.socket.send(json.dumps({
16            "id": 0,
17            "method": "ping",
18            "params": []
19        }))
20    
21    async def start_ping(self):
22        while True:
23            await asyncio.sleep(50)  # Every 50 seconds
24            await self.ping()
25    
26    async def listen(self):
27        async for message in self.socket:
28            print(f"Received: {message}")
29    
30    async def run(self):
31        await self.connect()
32        
33        # Start ping task
34        ping_task = asyncio.create_task(self.start_ping())
35        
36        # Listen for messages
37        await self.listen()
38
39# Usage
40token = "your_auth_token_here"
41client = CryptomusWebSocket(token)
42asyncio.run(client.run())
43            
Copy

All endpoints return time in the Unix timestamp format

Request Message Structure

NameParameter typeDefinition
idIntegerShould be unique to process the response to your request
methodStringThe request name
paramsArrayPass the parameters for the method here

Parameter type

Integer

Definition

Should be unique to process the response to your request

Parameter type

String

Definition

The request name

Parameter type

Array

Definition

Pass the parameters for the method here

The WebSocket connection will be terminated if invalid JSON is sent

Message and request types

- Request (`ping`, `candles_request`, etc.)

- Subscription (`candles_subscribe`, `lastprice_subscribe`, etc.). Resubscribing to the same data type will be canceled.

Response Message

NameParameter typeDefinition
idIntegerRequest identifier
dataNullif the request failed. If successful, the response structure for each method is shown below
errorNullif successful. If there's an error
messageString
codeIntegerError code

Parameter type

Integer

Definition

Request identifier

Parameter type

Null

Definition

if the request failed. If successful, the response structure for each method is shown below

Parameter type

Null

Definition

if successful. If there's an error

Parameter type

Integer

Definition

Error code

Code: 1. Message: Invalid message format

Code: 2. Message: Other errors

Response Message Types

- Query result

- Subscription status (success/failure)

- Update events

Ping/Pong

Ping


1{    
2  "id": 0,   
3  "method": "ping",   
4  "params": []
5} 
6          
Copy

Response pong


1{
2  "id": 0,
3  "method": "pong",
4  "data": null,
5  "error": null
6}         
7          
Copy

Examples subscriptions

Request


1{    
2  "id": 0,    
3  "method": "trade_subscribe",    
4  "params": [  
5      "BTC_USDT"    
6  ]
7}            
8          
Copy

Response


1{    
2  "id": 0,    
3  "method": "trade_subscribe",  
4  "data": {       
5	  "status": "success"    
6  },    
7  "error": null  
8} 
9          
Copy

Update Event


1{    
2  "id": 0,    
3  "method": "trade_update",    
4  "data": [], // See structure for each method below  
5  "error": null
6}  
7          
Copy

Last price

Subscription


1{   
2  "id": 0,   
3  "method": "lastprice_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "BTC_USDT",       
7    "ETH_USDT",      
8    ...    
9  ]
10}       
11          
Copy
Subscribe to all events lastprice_subscribe

1{    
2  "id": 0,   
3  "method": "lastprice_subscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7}
8            
Copy

Response


1{    
2  "id": 0,    
3  "method": "lastprice_subscribe",  
4  "data": {       
5    "status": "success"    
6  },    
7  "error": null  
8}   
9          
Copy

Event


1{
2  "id": 0,
3  "method": "lastprice_update",
4  "data": {
5    "symbol": "BTC_USDT",
6    "timestamp": 1750953362,
7    "price": "107152.55"
8  },
9  "error": null
10}
11          
Copy

Subscription Cancellation

Request

1{    
2  "id": 0,   
3  "method": "lastprice_unsubscribe",   
4  "params": [  
5    "BTC_USDT",       
6    "ETH_USDT",       
7    ...    
8  ]
9}             
10            
Copy
Unsubscribe from all events lastprice_unsubscribe

1{    
2  "id": 0,   
3  "method": "lastprice_unsubscribe",   
4  "params": [  
5    "all" // This also works for other events   
6  ]
7}  
8            
Copy

Response


1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null  
7}
Copy

Depth

Subscription

First you need to get the scale index of the market how to get


1{   
2  "id": 0,   
3  "method": "depth_subscribe",   
4  "params": [   
5    "BTC_USDT:0",  // market:scale_index 
6    "BTC_USDT:1",       
7    "ETH_USDT:1",       
8    ...    
9  ]
10}  
11          
Copy
Subscribe to all events depth_subscribe

1{    
2  "id": 0,   
3  "method": "depth_subscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7}  
8            
Copy

Response


1{    
2  "id": 0,    
3  "method": "depth_subscribe",  
4  "data": {       
5    "status": "success"    
6  },    
7  "error": null  
8} 
9            
Copy

Event


1{
2  "id": 0,
3  "method": "depth_update",
4  "data": {
5    "symbol": "BTC_USDT",
6    "timestamp": 1750952911,
7    "full_reload": false, // If false, it is a partial update, if true, it is a fully updated orderbook.
8    "scale_index": 0,
9    "asks": [
10      [
11        "107043.93", // price
12        "0.304313" // value
13      ]
14    ],
15    "bids": [
16      [
17        "106976.11",
18        "0" // for partial update - finished orders will be 0
19      ]
20    ]
21  },
22  "error": null
23} 
24            
Copy

Subscription Cancellation

Request

1{    
2  "id": 0,   
3  "method": "depth_unsubscribe",   
4  "params": [  
5    "BTC_USDT:0", // market:scale_index     
6    "ETH_USDT:1",      
7    ...    
8  ]
9}  
10              
Copy
Unsubscribe from all events depth_unsubscribe

1{    
2  "id": 0,   
3  "method": "depth_unsubscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7}  
8              
Copy
Response

1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null  
7}  
8              
Copy

Ticker

Subscription


1{   
2  "id": 0,   
3  "method": "ticker_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "BTC_USDT",       
7    "ETH_USDT",      
8    ...    
9  ]
10}  
11          
Copy
Subscribe to all events ticker_subscribe

1{    
2  "id": 0,   
3  "method": "ticker_subscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7} 
8          
Copy

Response


1{    
2  "id": 0,    
3  "method": "ticker_subscribe",  
4  "data": {       
5    "status": "success"    
6  },    
7  "error": null  
8}  
9          
Copy

Event


1{
2  "id": 0,
3  "method": "ticker_update",
4  "data": {
5    "symbol": "BTC_USDT",
6    "timestamp": 1750953144,
7    "price": "107090.35",
8    "open": "107042.21",
9    "high": "108248.15",
10    "low": "106573.19",
11    "volume": "1531.027982",
12    "quote_volume": "164564314.80174687",
13    "price_change": "0.04"
14  },
15  "error": null
16} 
17          
Copy

Subscription Cancellation

Request

1{    
2  "id": 0,   
3  "method": "ticker_unsubscribe",   
4  "params": [  
5    "BTC_USDT",      // Market 
6    "ETH_USDT",       
7    ...    
8  ]
9} 
10            
Copy
Unsubscribe from all events ticker_unsubscribe

1{    
2  "id": 0,   
3  "method": "ticker_unsubscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7} 
8            
Copy
Response

1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null  
7}  
8            
Copy

Trades

Subscription


1{   
2  "id": 0,   
3  "method": "trade_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "BTC_USDT",       
7    "ETH_USDT",      
8    ...    
9  ]
10}  
11          
Copy
Subscribe to all events trade_subscribe

1{    
2  "id": 0,   
3  "method": "trade_subscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7}  
8          
Copy

Response


1{    
2  "id": 0,    
3  "method": "trade_subscribe",  
4  "data": {       
5    "status": "success"    
6  },    
7  "error": null  
8}  
9          
Copy

Event


1{
2  "id": 0,
3  "method": "trade_update",
4  "data": {
5    "symbol": "BTC_USDT",
6    "timestamp": 1750953177,
7    "trades": [
8      {
9        "price": 107100.01,
10        "quantity": 0.000254,
11        "timestamp": 1750953177,
12        "direction": "buy"
13      },
14      ...
15    ]
16  },
17  "error": null
18}
19          
Copy

Subscription Cancellation

Request

1{    
2  "id": 0,   
3  "method": "trade_unsubscribe",   
4  "params": [  
5    "BTC_USDT",    // Market   
6    "ETH_USDT",       
7    ...    
8  ]
9}  
10            
Copy
Unsubscribe from all events trade_unsubscribe

1{    
2  "id": 0,   
3  "method": "trade_unsubscribe",   
4  "params": [  
5    "all" // This also works for other events    
6  ]
7}  
8            
Copy
Response

1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null  
7}  
8            
Copy

Orders

Subscription


1{   
2  "id": 0,   
3  "method": "order_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "TRX_USDT",       
7    "ETH_USDT",       
8    ...    
9  ]
10}  
11          
Copy

Subscribe to all events order_subscribe


1{    
2  "id": 0,  
3  "method": "order_subscribe",    
4  "params": [      
5    "all" // This also works for other events    
6  ]
7}  
8          
Copy

Response


1{    
2  "id": 0,    
3  "method": "order_subscribe",  
4  "data": {      
5    "status": "success"    
6  },    
7  "error": null 
8} 
9          
Copy

Events

Order created

1{
2  "id": "1",
3  "method": "order_update",
4  "data": {
5    "type": "created",
6    "info": {
7      "id": "01JYET5DQ772MPYHHE417FQF1J",
8      "symbol": "TRX_USDT",
9      "orderType": "limit",
10      "direction": "sell",
11      "price": "0.2200000000000000",
12      "quantity": "50.0000000000000000",
13      "value": "11.0000000000000000",
14      "filledQuantity": "0.0000000000000000",
15      "filledValue": "0.0000000000000000",
16      "clientOid": null,
17      "createTs": 1750696376
18    }
19  },    
20  "error": null 
21}
22            
Copy
Order updated

1{
2  "id": "3",
3  "method": "order_update",
4  "data": {
5    "type": "updated",
6    "info": {
7      "id": "01JYET5DQ772MPYHHE417FQF1J",
8      "symbol": "TRX_USDT",
9      "orderType": "limit",
10      "direction": "sell",
11      "price": "0.2200000000000000",
12      "quantity": "50.0000000000000000",
13      "value": "11.0000000000000000",
14      "filledQuantity": "20.0000000000000000",
15      "filledValue": "4.4000000000000000",
16      "clientOid": null,
17      "createTs": 1750696376,
18      "updateTs": 1750696487,
19    }
20  },    
21  "error": null 
22}
23            
Copy
Order finished

1{
2  "id": "3",
3  "method": "order_update",
4  "data": {
5    "type": "finished",
6    "info": {
7      "id": "01JYET5DQ772MPYHHE417FQF1J",
8      "symbol": "TRX_USDT",
9      "orderType": "limit",
10      "direction": "sell",
11      "price": "0.2200000000000000",
12      "quantity": "50.0000000000000000",
13      "value": "11.0000000000000000",
14      "filledQuantity": "50.0000000000000000",
15      "filledValue": "11.0000000000000000",
16      "clientOid": null,
17      "createTs": 1750696376,
18      "finishTs": 1750696417,
19      "state": "completed""internalState": "filled"
20    }
21  },    
22  "error": null 
23}
24            
Copy

Unsubscribe from updates

From specific markets

1{    
2  "id": 0,   
3  "method": "order_unsubscribe",   
4  "params": [  
5    "BTC_USDT",   // Market    
6    "ETH_USDT",       
7    ...    
8  ]
9} 
10            
Copy
From all markets

1{    
2  "id": 0,   
3  "method": "order_unsubscribe",   
4  "params": [  
5    "all" // Optional, will also work if an empty array is passed in    
6  ]
7}  
8            
Copy
Response

1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null   
7} 
8            
Copy

Balances

Subscription

BaseCurrency from market-list


1{   
2  "id": 0,   
3  "method": "balance_subscribe",   
4  "params": [   
5    "BTC",  // BaseCurrency from market list
6    "TRX,      
7    "ETH",       
8    ...    
9  ]
10}  
11          
Copy

Subscribe to all events balance_subscribe


1{    
2  "id": 0,  
3  "method": "balance_subscribe",    
4  "params": [       
5    "all" // This also works for other events    
6  ]
7}  
8          
Copy

Response


1{    
2  "id": 0,    
3  "method": "balance_subscribe",  
4  "data": {      
5    "status": "success"    
6  },    
7  "error": null   
8}  
9          
Copy

Events


1{
2  "id": "4",
3  "method": "balance_update",
4  "data": {
5    "info": {
6      "walletId": "01J7E836F6K5KCX5DP2W0F6FAG",
7      "currencyCode": "USDT",
8      "amount": "50.0000000000000000",
9      "oldBalance": "40000.0000000000000000",
10      "newBalance": "39950.00000000"
11    }
12  },    
13  "error": null 
14}
15          
Copy

Unsubscribe from events

From specific currencies

BaseCurrency from market-list


1{    
2  "id": 0,   
3  "method": "balance_unsubscribe",   
4  "params": [  
5    "BTC",       // BaseCurrency from market list
6    "ETH",       
7    ...    
8  ]
9}     
10            
Copy
From all currencies

1{    
2  "id": 0,   
3  "method": "balance_unsubscribe",   
4  "params": [  
5    "all" // Optional, will also work if an empty array is passed in    
6  ]
7}   
8            
Copy
Response

1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null   
7}  
8            
Copy

Deals

Subscription


1{   
2  "id": 0,   
3  "method": "deal_subscribe",   
4  "params": [   
5    "BTC_USDT",   // Market
6    "TRX_USDT,       
7    "ETH_USDT",       
8    ...   
9  ]
10} 
11          
Copy

Subscribe to all events deal_subscribe


1{    
2  "id": 0,  
3  "method": "deal_subscribe",    
4  "params": [       
5    "all" // This also works for other events    
6  ]
7}  
8          
Copy

Response


1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null  
7}
8          
Copy

Events


1{
2  "id": "4",
3  "method": "deal_update",
4  "data": {
5    "info": {
6      "dealId": "01JYH50V5VWPP3QTYGM6CPZ0AR",
7      "symbol": "TRX_USDT",
8      "dealState": "completed",
9      "transactionId": "01JYH50V5YM8M3943KJ9HY2VXM",
10      "filledPrice": "0.2726960000000000",
11      "filledQuantity": "100.0000000000000000",
12      "filledValue": "27.2696000000000000",
13      "fee": "1.0907840000000000",
14      "feeCurrency": "USDT",
15      "tradeRole": "taker",
16      "committedAt": 1750774869
17    }
18  },    
19  "error": null 
20}
21          
Copy

Unsubscribe from updates

From specific markets

1{    
2  "id": 0,   
3  "method": "deal_unsubscribe",   
4  "params": [  
5    "BTC_USDT",   // Market
6    "TRX_USDT,       
7    ...    
8  ]
9} 
10            
Copy
From all markets

1{    
2  "id": 0,   
3  "method": "deal_unsubscribe",   
4  "params": [  
5    "all" // Optional, will also work if an empty array is passed in    
6  ]
7} 
8            
Copy
Response

1{    
2  "id": 0,    
3  "data": {      
4    "status": "success"    
5  },    
6  "error": null   
7} 
8            
Copy