Subscribes/Unsubscribes to market swaps Tickers data.
Make sure you're connected. Either the instrument or exchange have to be specified - one needs to be specified, but both cannot be empty.
Note that there is a dedicated URL for this subscription: wss://ws.web3api.io/swaps.
Request
{
"jsonrpc" : "2.0",
"id" : 1,
"method" : "subscribe",
"params" : [ "market:swaps:tickers", { "instrument": "BTC-USD-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": {
"subscription": "coh0ye2i18hdso1qvo2u7uskuixwrxgqzj3myilgd9gm3q6cljgp5knavyxtld32",
"result": {
"exchange": "okex",
"instrument": "BTC-USD-SWAP",
"exchangeTimestamp": 1690219386248,
"exchangeTimestampNanoseconds": 0,
"timestamp": 1690219386248,
"bid": 29112.4,
"ask": 29112.5,
"mid": 29112.45,
"last": 29112.5,
"sequence": null,
"lastVolume": 133.0,
"bidVolume": 2819.0,
"askVolume": 144.0
}
}
}
Field | Type | Description |
---|---|---|
exchange | string | The exchange. |
instrument | string | The instrument. |
exchangeTimestamp | number | The exchange provided timestamp. |
exchangeTimestampNanoseconds | number | The exchange provided nanosecond part of the exchangeTimestamp (if available from the exchange). |
timestamp | number | |
bid | number | The bid of the instrument. |
ask | number | The ask of the instrument. |
mid | number | The mid of the instrument. |
last | number | The last of the instrument. |
sequence | number | The sequence number (equal to null if it is not provided by the exchange). |
lastVolume | number | |
bidVolume | number | Best bid volume. |
askVolume | number | Best ask volume. |
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:tickers', {'instrument': 'BTC-USD-SWAP', 'exchange': 'okex'}],
id: 1,
}));
});
ws.on('message', data => {
console.log(JSON.stringify(JSON.parse(data), null, 2));
});