Skip to content

How do I get Binance futures liquidations in Node.js? ​

Subscribe to the forceOrder stream on the Binance USD-M futures WebSocket. With the binance SDK, subscribeAllLiquidationOrders('usdm') streams liquidations for every symbol and subscribeSymbolLiquidationOrders('BTCUSDT', 'usdm') follows one symbol. The old public REST route for liquidation history, /fapi/v1/allForceOrders, now returns 404, so collecting the stream is how you build a history.

Stream every liquidation ​

This example subscribes to all USD-M liquidations, prints each one and closes after 60 seconds. It needs no API key:

ts
import { WebsocketClient, isWsFormattedForceOrder } from 'binance';

const ws = new WebsocketClient({ beautify: true });
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
function stop() {
  if (stopped) return;
  stopped = true;
  clearTimeout(timer);
  ws.closeAll();
}
process.once('SIGINT', stop);
process.once('SIGTERM', stop);
ws.on('formattedMessage', (message) => {
  if (!isWsFormattedForceOrder(message)) return;
  const { symbol, side, quantity, price } = message.liquidationOrder;
  console.log(`${symbol} ${side} liquidation: ${quantity} at ${price}`);
});
ws.on('exception', () => {
  console.error('WebSocket exception. Check the topic, environment and connection.');
});
async function main() {
  await ws.subscribeAllLiquidationOrders('usdm');
  if (!stopped) timer = setTimeout(stop, 60000);
}
main().catch(() => {
  console.error('Subscription failed.');
  process.exitCode = 1;
  stop();
});

A line of output looks like MANTRAUSDT BUY liquidation: 2517 at 0.002176. The beautify option gives readable field names and numeric values. Without it, the raw event keeps Binance's short field names under the message event.

What the stream sends ​

  • Binance sends a snapshot, not every fill. For each symbol it pushes the largest liquidation order in each 1000 millisecond window, and nothing when no liquidation happens.
  • Liquidations are quiet on some symbols and busy on others. Leave the stream running for a while before you judge it.
  • Binance serves USD-M liquidations on the /market route of wss://fstream.binance.com. The SDK chooses that route for you, which matters because the legacy /ws path does not deliver market streams any more. See troubleshooting.
  • Use 'coinm' instead of 'usdm' for COIN-M futures.

Follow one symbol ​

ts
await ws.subscribeSymbolLiquidationOrders('BTCUSDT', 'usdm');

Add this to the example in place of the all-symbol call. The SDK lower-cases the symbol for the btcusdt@forceOrder topic.

Read your own liquidations ​

Your own account's liquidation and auto-deleveraging orders are a private REST query. With a futures-enabled key:

ts
import { USDMClient } from 'binance';

const futures = new USDMClient({
  api_key: process.env.BINANCE_API_KEY,
  api_secret: process.env.BINANCE_API_SECRET,
});

const orders = await futures.getForceOrders({ symbol: 'BTCUSDT', autoCloseType: 'LIQUIDATION' });
console.log(orders.length, 'liquidation orders on this account');
console.table(
  orders.map((order) => ({
    symbol: order.symbol,
    side: order.side,
    price: order.avgPrice,
    quantity: order.executedQty,
    total: order.cumQuote,
    time: new Date(order.time).toISOString(),
  })),
);

Each row shows the symbol, side, average fill price, filled quantity, total quote notional and time.

Store a history ​

Because the public history route is gone, write each event to a file or database as it arrives. Keep the event time, symbol, side, price and quantity. The SDK reconnects after a dropped connection, and the WebSocket streams guide explains how to handle a gap in the data.

Built by Siebly. Open source SDKs for exchange APIs.