v2.2.0: Integrate WebSocket API for sending orders & commands via WebSocket events
What's Changed
- feat(v2.2.0, #67): Integrate WebSocket API for sending orders & commands via WebSocket events by @tiagosiebler in #84
- feat(): added wsapi client, examples and all types by @JJ-Cro in #90
- feat(): Added examples by @JJ-Cro in #91
- Bumped ws package to newer version
Breaking Changes
- No breaking changes with this release. Much of the architecture was already prepared for this enhancement.
WebSocket API
This release introduces a new WebSocket class called WebsocketAPIClient. This wraps around the newly integrated sendWSAPIRequest command from the WebsocketClient, to send commands & orders via a persisted WebSocket connection.
Below is an example showing how easy it is to use the WebSocket API without any concern for the complexity of managing WebSockets. For more detailed demonstration, take a look at the examples/WebSockets/ws-api-client.ts example:
import { DefaultLogger, WebsocketAPIClient } from 'kucoin-api';
// or, if you prefer `require()`:
// const { DefaultLogger, WebsocketAPIClient } = require('kucoin-api');
const customLogger = {
...DefaultLogger,
// For a more detailed view of the WebsocketClient, enable the `trace` level by uncommenting the below line:
// trace: (...params) => console.log(new Date(), 'trace', ...params),
};
const account = {
key: process.env.API_KEY || 'keyHere',
secret: process.env.API_SECRET || 'secretHere',
passphrase: process.env.API_PASSPHRASE || 'apiPassPhraseHere', // This is NOT your account password
};
const wsClient = new WebsocketAPIClient(
{
apiKey: account.key,
apiSecret: account.secret,
apiPassphrase: account.passphrase,
// If you want your own event handlers instead of the default ones with logs, disable this setting and see the `attachEventHandlers` example below:
// attachEventListeners: false
},
// customLogger, // optional: uncomment this to inject a custom logger
);
// Make WebSocket API calls, very similar to a REST API:
wsClient
.submitNewSpotOrder({
side: 'buy',
symbol: 'BTC-USDT',
type: 'limit',
price: '150000',
size: '0.0001',
})
.then((syncSpotOrderResponse) => {
console.log('Sync spot order response:', syncSpotOrderResponse);
})
.catch((e) => {
console.log('Sync spot order error:', e);
});
wsClient
.submitFuturesOrder({
clientOid: 'futures-test-' + Date.now(),
side: 'buy',
symbol: 'XBTUSDTM',
marginMode: 'CROSS',
type: 'limit',
price: '1000',
qty: '0.01',
leverage: 10,
positionSide: 'LONG', // needed if trading two-way (hedge) position mode
})
.then((futuresOrderResponse) => {
console.log('Futures order response:', futuresOrderResponse);
})
.catch((e) => {
console.log('Futures order error:', e);
});
Full Changelog: v2.1.23...v2.2.0