Created
May 6, 2025 11:08
-
-
Save tiagosiebler/03293c25149d2a0aa929cd8381403d83 to your computer and use it in GitHub Desktop.
Connect to the binance WebSocket API User Data stream on testnet in Node.js/JavaScript/TypeScript & Ed25519 keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
DefaultLogger, | |
WebsocketAPIClient, | |
WebsocketClient, | |
WS_KEY_MAP, | |
} from 'binance'; | |
/** | |
* The WS API only works with an Ed25519 API key. | |
* | |
* Check the rest-private-ed25519.md in this folder for more guidance | |
* on preparing this Ed25519 API key. | |
*/ | |
const publicKey = `-----BEGIN PUBLIC KEY----- | |
MCowBQYDK2VwAyEAsjbJ9Dl6WdFVWbxXAELxughpr6O3vwIVvZFt5jEhpSw= | |
-----END PUBLIC KEY----- | |
`; | |
const privateKey = `-----BEGIN PRIVATE KEY----- | |
MC4CAQAwBQYDK2VwBCIEINvd2zZafHhXEC/dENjhXtaXmEzOSDUh1m0aEIqhSW2i | |
-----END PRIVATE KEY----- | |
`; | |
const key = 'Q7fzVnhODCM7NUV8Fjg5cdqeCcDaLkYizVmdIn4XtXYsHBnzkRKGSocEf1kK6SST'; | |
const secret = privateKey; | |
function attachEventHandlers<TWSClient extends WebsocketClient>( | |
wsClient: TWSClient, | |
): void { | |
/** | |
* General event handlers for monitoring the WebsocketClient | |
*/ | |
wsClient.on('message', (data) => { | |
// console.log('raw message received ', JSON.stringify(data)); | |
}); | |
wsClient.on('formattedMessage', (data) => { | |
console.log('formattedMessage: ', data); | |
}); | |
wsClient.on('response', (data) => { | |
// console.log('ws response: ', JSON.stringify(data)); | |
}); | |
wsClient.on('open', (data) => { | |
console.log('ws connected', data.wsKey); | |
}); | |
wsClient.on('reconnecting', ({ wsKey }) => { | |
console.log('ws automatically reconnecting.... ', wsKey); | |
}); | |
wsClient.on('reconnected', (data) => { | |
console.log('ws has reconnected ', data?.wsKey); | |
}); | |
wsClient.on('authenticated', (data) => { | |
console.log('ws has authenticated ', data?.wsKey); | |
}); | |
wsClient.on('exception', (data) => { | |
console.error('ws exception: ', JSON.stringify(data)); | |
}); | |
} | |
async function main() { | |
const customLogger = { | |
...DefaultLogger, | |
trace: (msg, data, ...others) => { | |
if (msg.includes('resolveEmittableEvents')) { | |
if (data?.type === 'message') { | |
console.log(msg, data.data); | |
} else { | |
console.log(msg, data); | |
} | |
return; | |
} | |
console.log('trace: ', msg, data, ...others); | |
}, | |
}; | |
const wsClient = new WebsocketAPIClient( | |
{ | |
api_key: key, | |
api_secret: secret, | |
beautify: true, | |
// Enforce testnet ws connections, regardless of supplied wsKey: | |
testnet: true, | |
attachEventListeners: false, | |
}, | |
customLogger, | |
); | |
// Attach your own event handlers to process incoming events | |
// You may want to disable the default ones to avoid unnecessary logs (via attachEventListeners:false, above) | |
attachEventHandlers(wsClient.getWSClient()); | |
try { | |
const response = await wsClient.subscribeUserDataStream( | |
WS_KEY_MAP.mainWSAPI, // The `mainWSAPI` wsKey will connect to the "spot" Websocket API on Binance. | |
); | |
console.log('subscribeUserDataStream response: ', response); | |
} catch (e) { | |
console.log('subscribeUserDataStream error: ', e); | |
} | |
} | |
// Start executing the example workflow | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment