Does Binance have a WebSocket API?
Yes. Binance has two WebSocket interfaces. WebSocket Streams push live market and account data to you. The WebSocket API sends request and response calls, such as placing and querying orders, over one persistent connection. The binance SDK covers both, with WebsocketClient for streams and WebsocketAPIClient for the WebSocket API.
Which one to use
| WebSocket Streams | WebSocket API | |
|---|---|---|
| Direction | Binance pushes updates to you | You send a request and receive one response |
| Typical use | Live prices, order books, trades, account updates | Prices, order placement and account queries without a new HTTP request each time |
| SDK client | WebsocketClient | WebsocketAPIClient |
| Guide | WebSocket streams | WebSocket API requests |
Use streams for anything continuous. Use the WebSocket API when you want REST-style calls on a connection that is already open.
Subscribe to live prices
Streams need no API key for public data. This example prints BTCUSDT Spot ticker updates for 30 seconds and then closes the connection:
import { WebsocketClient } from 'binance';
const ws = new WebsocketClient({});
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('message', (message) => console.log(message));
ws.on('exception', () => {
console.error('WebSocket exception. Check the topic, environment and connection.');
});
async function main() {
await ws.subscribeSpotSymbol24hrTicker('btcusdt');
if (!stopped) timer = setTimeout(stop, 30000);
}
main().catch(() => {
console.error('Subscription failed.');
process.exitCode = 1;
stop();
});Make a WebSocket API request
WebsocketAPIClient has one method for each supported WebSocket API operation. It opens and looks after the connection, so a call reads like a REST call. This public price request needs no key:
import { WebsocketAPIClient } from 'binance';
const wsApi = new WebsocketAPIClient({});
async function main() {
const response = await wsApi.getSpotSymbolPriceTicker({ symbol: 'BTCUSDT' });
const ticker = Array.isArray(response.result) ? response.result[0] : response.result;
console.log('BTCUSDT last price over the WebSocket API:', ticker.price);
}
main()
.catch(() => {
console.error('WebSocket API request failed. Check the connection and try again.');
process.exitCode = 1;
})
.finally(() => wsApi.getWSClient().closeAll());For account calls, pass api_key and api_secret to the client. Binance supports HMAC, RSA and Ed25519 keys on the WebSocket API. An Ed25519 key lets the connection log on once and skip signing each request. See API key setup.
Connection details
- Binance serves the Spot WebSocket API at
wss://ws-api.binance.com:443/ws-api/v3and the Spot Testnet atwss://ws-api.testnet.binance.vision/ws-api/v3. The SDK picks the address for you. - Binance closes each connection after 24 hours. The SDK reconnects and restores your subscriptions. See recover after a disconnect.
- A WebSocket API response confirms one request. Follow orders and fills with the private account stream.