Subscribes/Unsubscribes to market swaps Trades data.
Make sure you're connected to this subscription: wss://ws.web3api.io/swaps. Either the instrument or exchange have to be specified - one needs to be specified, but both cannot be empty.
Request
{
"jsonrpc": "2.0",
"id": 1,
"method": "subscribe",
"params": [
"market:swaps:trades",
{
"instrument": "BTC-USDT-SWAP",
"exchange": "okex"
}
]
}
Param | Type | Description |
---|---|---|
instrument | string | The asset instrument. |
exchange | string | The exchange for which to retrieve asset instruments. |
Response
{
"jsonrpc": "2.0",
"method": "subscription",
"params": {
"result": {
"exchange": "okex",
"instrument": "BTC-USDT-SWAP",
"exchangeTimestamp": 1686603878414,
"exchangeTimestampNanoseconds": 0,
"isBuySide": false,
"quoteSize": null,
"price": 25886.7,
"size": 52,
"tradeId": 571629883
},
"subscription": "843d9baca24d3563b62295de0c47a992e25589bc1717903eac3b40c640a20184"
}
}
Field | Type | Description |
---|---|---|
exchange | string | The exchange. |
instrument | string | |
exchangeTimestamp | number | Timestamp that the exchange returned. |
exchangeTimestampNanoseconds | number | Nanoseconds part of exchangeTimestamp . |
isBuySide | boolean | null | true if the trade is a buy, false otherwise. |
quoteSize | number | null | |
price | number | null | The price at which the instrument was traded. |
size | number | null | The total amount of the instrument that was traded. |
tradeId | number | null | The exchange provided id of the trade. |
Example
const WebSocket = require('ws');
const ws = new WebSocket('wss://ws.web3api.io/swaps', {headers: {x-api-key:'<api_key>'}});
ws.on('open', () => {
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'subscribe',
params: ['market:swaps:trades', {'instrument': 'BTC-USDT-SWAP', 'exchange': 'okex'}],
id: 1,
}));
});
ws.on('message', data => {
console.log(JSON.stringify(JSON.parse(data), null, 2));
});