Subscribes/Unsubscribes to market trade data.
Make sure you're connected to this subscription: wss://ws.web3api.io/spot. Either the pair 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:spot:trades",{"pair":"btc_usd","exchange":"gdax"}]}
Param | Type | Description |
---|---|---|
pair | string | The asset pair. |
exchange | string | The exchange for which to retrieve asset pairs. |
Note: Subscription response will include a field metadata
which includes the names of the columns in the order in which they appeared in the event notification response.
Response
{
"jsonrpc": "2.0",
"method": "subscription",
"params": {
"subscription": "wpw5cmrwsei7unnubx9otjyvnp5lv8jelfnl47i2rck4uib3xkopzzxywhhvsrpv",
"result": [
[
"gdax",
"btc_usd",
1707247429746,
604000,
"600887357",
43191.53,
0.00468966,
true
]
]
}
}
Field | Type | Description |
---|---|---|
exchange | string | The exchange. |
pair | string | The normalized pair name. |
timestamp | number | The time at which the trade took place. |
timestampNanoseconds | number | The nano second part of the timestampMilliseconds , where applicable. |
tradeId | string | The unique id given by an exchange. |
price | number | The quote price of the asset pair. |
volume | number | The number of assets traded in a specific asset pair within a given period of time. |
isBuy | bool | Indicates if it is a buy or sell trade: true for a buy trade and false for a sell. |
Example
const WebSocket = require('ws');
const ws = new WebSocket('wss://ws.web3api.io/spot', {headers: {x-api-key:'<api_key>'}});
ws.on('open', () => {
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'subscribe',
params: ["market:spot:trades", {"pair": "btc_usd", "exchange": "gdax"}],
id: 1,
}));
});
ws.on('message', data => {
console.log(JSON.stringify(JSON.parse(data), null, 2));
});