Class: WebsocketAPIClient
Promise-based Spot and perpetual-futures WebSocket API requests. Connections and authentication are established as needed. Select TestNet with useTestnet: true in constructor options, keeping the normal connection key. Results are in response.data.result.
Constructors
Constructor
new WebsocketAPIClient(options?: WSClientConfigurableOptions & Partial<WSAPIClientConfigurableOptions>, logger?: object): WebsocketAPIClient;Parameters
| Parameter | Type |
|---|---|
options? | WSClientConfigurableOptions & Partial<WSAPIClientConfigurableOptions> |
logger? | { error: (...params: any) => void; info: (...params: any) => void; trace: (..._params: any) => void; } |
logger.error? | (...params: any) => void |
logger.info? | (...params: any) => void |
logger.trace? | (..._params: any) => void |
Returns
WebsocketAPIClient
Methods
cancelFuturesAllOpenOrders()
cancelFuturesAllOpenOrders(params: WSAPIFuturesOrderCancelCPReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrder[], WSAPITopic>>;Cancel all open futures orders
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderCancelCPReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrder[], WSAPITopic>>
cancelFuturesOrder()
cancelFuturesOrder(params: WSAPIFuturesOrderCancelReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>;Cancel a futures order
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderCancelReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>
cancelFuturesOrderById()
cancelFuturesOrderById(params: string[], wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrderCancelIdsRespItem[], WSAPITopic>>;Cancel futures orders by id list
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | string[] |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrderCancelIdsRespItem[], WSAPITopic>>
cancelSpotOrder()
cancelSpotOrder(params: WSAPISpotOrderCancelReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>;Cancel a spot order
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderCancelReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>
cancelSpotOrderById()
cancelSpotOrderById(params: WSAPISpotOrderCancelIdsReq[], wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrderCancelIdsRespItem[], WSAPITopic>>;Cancel all spot orders with the given id list
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderCancelIdsReq[] |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrderCancelIdsRespItem[], WSAPITopic>>
cancelSpotOrderForSymbol()
cancelSpotOrderForSymbol(params: WSAPISpotOrderCancelCPReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrder[], WSAPITopic>>;Cancel a spot order for a given symbol
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderCancelCPReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrder[], WSAPITopic>>
getFuturesOrders()
getFuturesOrders(params: WSAPIFuturesOrderListReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrder[], WSAPITopic>>;Get all futures orders
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderListReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrder[], WSAPITopic>>
getFuturesOrderStatus()
getFuturesOrderStatus(params: WSAPIFuturesOrderStatusReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>;Get futures order status
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderStatusReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>
getSpotOrders()
getSpotOrders(params: WSAPISpotOrderListReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrder[], WSAPITopic>>;Get all spot orders
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderListReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrder[], WSAPITopic>>
getSpotOrderStatus()
getSpotOrderStatus(params: WSAPISpotOrderStatusReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>;Get the status of a spot order
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderStatusReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>
Example
import { WebsocketAPIClient } from 'gateio-api';
import { clientOptions, requiredEnv } from './authenticated-client.js';
async function main() {
const api = new WebsocketAPIClient({
...clientOptions,
attachEventListeners: false,
});
api.getWSClient().on('exception', () => {
console.error('WebSocket API request or connection failed.');
});
try {
const response = await api.getSpotOrderStatus({
currency_pair: 'BTC_USDT',
order_id: requiredEnv('GATEIO_ORDER_ID'),
account: 'spot',
});
console.log('Order state:', response.data.result);
} finally {
api.getWSClient().closeAll();
}
}
main().catch(() => {
console.error(
'Unable to read the order. Check its ID, credentials and environment.',
);
process.exitCode = 1;
});See
getWSClient()
getWSClient(): WebsocketClient;Returns
setTimeOffsetMs()
setTimeOffsetMs(newOffset: number): void;Parameters
| Parameter | Type |
|---|---|
newOffset | number |
Returns
void
submitNewFuturesBatchOrder()
submitNewFuturesBatchOrder(params: WSAPIFuturesOrderPlaceReq[], wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrderBatchPlaceRespItem[], WSAPITopic>>;Submit a batch of futures orders
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderPlaceReq[] |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrderBatchPlaceRespItem[], WSAPITopic>>
submitNewFuturesOrder()
submitNewFuturesOrder(params: WSAPIFuturesOrderPlaceReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>;Submit a futures order.
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderPlaceReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>
submitNewSpotOrder()
submitNewSpotOrder(params: WSAPISpotOrderPlaceReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>;Submit a spot order
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderPlaceReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>
updateFuturesOrder()
updateFuturesOrder(params: WSAPIFuturesOrderAmendReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>;Update a futures order
Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
Parameters
| Parameter | Type |
|---|---|
params | WSAPIFuturesOrderAmendReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPIFuturesOrder, WSAPITopic>>
updateSpotOrder()
updateSpotOrder(params: WSAPISpotOrderAmendReq, wsKey?: keyof WsAPIWsKeyTopicMap): Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>;Update a spot order
Parameters
| Parameter | Type |
|---|---|
params | WSAPISpotOrderAmendReq |
wsKey? | keyof WsAPIWsKeyTopicMap |
Returns
Promise<WSAPIResponse<WSAPISpotOrder, WSAPITopic>>