Binance TypeScript SDK example: ws-demo-spot.ts
Binance WebSocket Demo websocket demo spot example for the Siebly Binance SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.
What This Example Covers
- Binance WebSocket stream example in TypeScript.
- Uses the Siebly Binance SDK package
binanceinstead of hand-written WebSocket plumbing. - Source path:
Binance/WebSockets/Demo/ws-demo-spot.ts. - Example category: WebSocket Demo.
- Imports SDK symbols including
WebsocketClient. - Calls SDK methods such as
on(),subscribeSpotTrades(),subscribeSpotKline().
How To Use This Example
- Start here for the specific request or stream pattern, then check the matching SDK guide for install, credentials, and operational notes.
- For WebSocket examples, keep reconnect, resubscribe, heartbeat, and event-handler behavior explicit in your service.
- Open the repository source when you need the latest committed version: GitHub source file.
Example Path
Binance/WebSockets/Demo/ws-demo-spot.ts
Source Link
Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Binance/WebSockets/Demo/ws-demo-spot.ts
Related SDK Docs
Example Source
import { WebsocketClient } from 'binance';
const key = process.env.API_KEY_COM || 'APIKEY';
const secret = process.env.API_SECRET_COM || 'APISECRET';
async function start() {
const wsClient = new WebsocketClient({
api_key: key,
api_secret: secret,
beautify: true,
/**
* Demo trading uses real market data with simulated trading.
* Perfect for testing strategies without risk.
*
* For more information:
* https://www.binance.com/en/support/faq/how-to-test-my-functions-on-binance-spot-test-network-ab78f9a1b8824cf0a106b4229c76496d
*/
demoTrading: true,
});
wsClient.on('formattedMessage', (data) => {
console.log('Demo WS data: ', JSON.stringify(data, null, 2));
});
wsClient.on('open', (data) => {
console.log('Demo WS connection opened:', data.wsKey);
});
wsClient.on('response', (data) => {
console.log('Demo WS response: ', JSON.stringify(data, null, 2));
});
wsClient.on('reconnected', (data) => {
console.log('Demo WS reconnected ', data?.wsKey);
});
wsClient.on('exception', (data) => {
console.error('Demo WS error', data);
});
// Subscribe to spot market streams on demo trading
wsClient.subscribeSpotTrades('BTCUSDT');
wsClient.subscribeSpotKline('BTCUSDT', '1m');
}
start();