WebSocket ストリーム

接続タイムアウト

    - サーバーは、60秒間の非アクティブ状態後にWebSocket接続を閉じます(接続キープアライブ)

    - 非アクティブとは、クライアントから送信されたリクエストがない状態と定義されます。

接続キープアライブ

WebSocket接続をアクティブに保つには、

    - 50秒ごとに定期的なリクエストを送信する

    - アプリケーションのロジックで潜在的な切断を適切に処理する

承認

導入

WebSocketリクエストに署名する前に、Cryptomus個人アカウントでAPIキーを作成する必要があります。

APIキーを作成するための詳細な手順はこちらをご覧ください ここ

接続用のワンタイムトークンの取得

WebSocketサーバーに接続する前に、ワンタイム認証トークンを取得する必要があります。

トークンを取得するには、 対応するリクエスト

トークンは5分間、または最初の接続が成功するまで有効です。

WebSocketサーバーへの接続

WebSocketサーバーに接続するには

1. 一度だけ取得 認証トークン

2. 接続を確立するときにクエリパラメータトークンにトークンを指定します

接続URLの例: wss://api-ws.cryptomus.com/ws?token=b0b1cacd26993392ecf072155bd1686c96e380ad12cefa564aa6665c85c77334

トークンは一度しか使用できません。新しい接続ごとに新しいトークンをリクエストする必要があります。

トークンの検証が成功すると接続が確立され、利用可能なチャンネルを購読できるようになります。

WebSocket接続用のワンタイム認証トークンの取得

WebSocket サーバーに接続するためのワンタイム認証トークンを生成できます。トークンは、最初の接続が成功するか、有効期限が切れるまで (5 分) 有効です。

GET
/v2/user-api/exchange/account/web-socket/token
コピー

認証ヘッダー

必要な認証ヘッダーに関する完全な情報は入手可能です ここ

応答

{ "token": "168b34a3a12370740ff1654f0d0c4726ff1bba8c4675edc0d7553f15e5a2a094" }
コピー

考えられるコード

200: 注文は正常に作成されました

404: ユーザーまたはリソースが見つかりません

500: 内部サーバーエラー

深度スケールの記号

リクエスト

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

応答


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

市場リスト

リクエスト

GET
https://api-app.cryptomus.com/v1/exchange/market
コピー

応答


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}
コピー

実装例

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            
コピー

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            
コピー

すべてのエンドポイントはUnixタイムスタンプ形式で時間を返します

リクエストメッセージの構造

名前パラメータータイプ意味
idIntegerリクエストへの応答を処理するには一意である必要があります
methodStringリクエスト名
paramsArrayここでメソッドのパラメータを渡します

パラメータータイプ

Integer

意味

リクエストへの応答を処理するには一意である必要があります

パラメータータイプ

String

意味

リクエスト名

パラメータータイプ

Array

意味

ここでメソッドのパラメータを渡します

無効なJSONが送信された場合、WebSocket接続は終了します。

メッセージとリクエストの種類

- リクエスト (`ping`、`candles_request` など)

- サブスクリプション(`candles_subscribe`、`lastprice_subscribe` など)。同じデータ タイプへの再サブスクリプションはキャンセルされます。

応答メッセージ

名前パラメータータイプ意味
idIntegerリクエスト識別子
dataNullリクエストが失敗した場合。成功した場合、各メソッドのレスポンス構造は以下のとおりです。
errorNull成功した場合。エラーが発生した場合
messageString
codeIntegerエラーコード

パラメータータイプ

Integer

意味

リクエスト識別子

パラメータータイプ

Null

意味

リクエストが失敗した場合。成功した場合、各メソッドのレスポンス構造は以下のとおりです。

パラメータータイプ

Null

意味

成功した場合。エラーが発生した場合

パラメータータイプ

Integer

意味

エラーコード

コード: 1. メッセージ: 無効なメッセージ形式

コード: 2。メッセージ: その他のエラー

応答メッセージの種類

- クエリ結果

- サブスクリプションのステータス(成功/失敗)

- 更新イベント

ピンポン

ピン


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

レスポンスポン


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

サブスクリプションの例

リクエスト


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

応答


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

更新イベント


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

最終価格

サブスクリプション


1{   
2  "id": 0,   
3  "method": "lastprice_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "BTC_USDT",       
7    "ETH_USDT",      
8    ...    
9  ]
10}       
11          
コピー
すべてのイベントを購読する lastprice_subscribe

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

応答


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

イベント


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          
コピー

サブスクリプションのキャンセル

リクエスト

1{    
2  "id": 0,   
3  "method": "lastprice_unsubscribe",   
4  "params": [  
5    "BTC_USDT",       
6    "ETH_USDT",       
7    ...    
8  ]
9}             
10            
コピー
すべてのイベントの登録を解除する lastprice_unsubscribe

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

応答


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

深さ

サブスクリプション

まず市場の規模指数を取得する必要があります 入手方法


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          
コピー
すべてのイベントを購読する depth_subscribe

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

応答


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

イベント


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            
コピー

サブスクリプションのキャンセル

リクエスト

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              
コピー
すべてのイベントの登録を解除する depth_unsubscribe

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

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

ティッカー

サブスクリプション


1{   
2  "id": 0,   
3  "method": "ticker_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "BTC_USDT",       
7    "ETH_USDT",      
8    ...    
9  ]
10}  
11          
コピー
すべてのイベントを購読する ticker_subscribe

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

応答


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

イベント


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          
コピー

サブスクリプションのキャンセル

リクエスト

1{    
2  "id": 0,   
3  "method": "ticker_unsubscribe",   
4  "params": [  
5    "BTC_USDT",      // Market 
6    "ETH_USDT",       
7    ...    
8  ]
9} 
10            
コピー
すべてのイベントの登録を解除する ticker_unsubscribe

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

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

取引

サブスクリプション


1{   
2  "id": 0,   
3  "method": "trade_subscribe",   
4  "params": [   
5    "BTC_USDT",  // Market
6    "BTC_USDT",       
7    "ETH_USDT",      
8    ...    
9  ]
10}  
11          
コピー
すべてのイベントを購読する trade_subscribe

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

応答


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

イベント


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          
コピー

サブスクリプションのキャンセル

リクエスト

1{    
2  "id": 0,   
3  "method": "trade_unsubscribe",   
4  "params": [  
5    "BTC_USDT",    // Market   
6    "ETH_USDT",       
7    ...    
8  ]
9}  
10            
コピー
すべてのイベントの登録を解除する trade_unsubscribe

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

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

注文

サブスクリプション


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

すべてのイベントを購読する order_subscribe


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

応答


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

イベント

注文が作成されました

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            
コピー
注文が更新されました

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            
コピー
注文完了

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            
コピー

更新情報の購読を解除する

特定の市場から

1{    
2  "id": 0,   
3  "method": "order_unsubscribe",   
4  "params": [  
5    "BTC_USDT",   // Market    
6    "ETH_USDT",       
7    ...    
8  ]
9} 
10            
コピー
すべての市場から

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            
コピー
応答

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

残高

サブスクリプション

基本通貨から 市場リスト


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

すべてのイベントを購読する balance_subscribe


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

応答


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

イベント


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          
コピー

イベントの登録を解除する

特定の通貨から

基本通貨から market-list


1{    
2  "id": 0,   
3  "method": "balance_unsubscribe",   
4  "params": [  
5    "BTC",       // BaseCurrency from market list
6    "ETH",       
7    ...    
8  ]
9}     
10            
コピー
すべての通貨から

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            
コピー
応答

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

お得な情報

サブスクリプション


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

すべてのイベントを購読する deal_subscribe


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

応答


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

イベント


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          
コピー

更新情報の購読を解除する

特定の市場から

1{    
2  "id": 0,   
3  "method": "deal_unsubscribe",   
4  "params": [  
5    "BTC_USDT",   // Market
6    "TRX_USDT,       
7    ...    
8  ]
9} 
10            
コピー
すべての市場から

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            
コピー
応答

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