OKX websocket private handle auth fail JavaScript example for the Siebly OKX SDK, covering exchange REST API and WebSocket integration, setup, and production SDK docs.
We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.
okx-api instead of hand-written WebSocket plumbing.OKX/Websocket/ws-private-handle-auth-fail.ts.WebsocketClient.on(), close(), connectPrivate(), subscribe().import { WebsocketClient } from 'okx-api'; /** * * This simple example demonstrates one way to handle failed authentication, stopping the * websocket client from running into a reconnect-loop when authentication fails (e.g. bad API keys). * * However, keep in mind it might be safer to make a test REST API call (e.g. fetch account balance) before * trying to make a private WS connection to the account. * */ const wsClient = new WebsocketClient({ accounts: [ // For private topics, include one or more accounts in an array. Otherwise only public topics will work { apiKey: 'INCORRECT_API_KEY', apiSecret: 'INCORRECT_API_SECRET', apiPass: 'INCORRECT_API_PASSPHRASE', }, ],}); // Raw data will arrive on the 'update' eventwsClient.on('update', (data) => { // console.log('ws update (raw data received)', JSON.stringify(data, null, 2)); console.log('ws update (raw data received)', JSON.stringify(data));}); wsClient.on('open', (data) => { console.log('connection opened open:', data.wsKey);}); // Replies (e.g. authenticating or subscribing to channels) will arrive on the 'response' eventwsClient.on('response', (data) => { // console.log('ws response: ', JSON.stringify(data, null, 2)); console.log('ws response: ', JSON.stringify(data));}); wsClient.on('reconnect', ({ wsKey }) => { console.log('ws automatically reconnecting.... ', wsKey);});wsClient.on('reconnected', (data) => { console.log('ws has reconnected ', data?.wsKey);}); wsClient.on('exception', (data) => { console.error('ws exception: ', data); const INVALID_API_KEY_ERROR = '60005'; if (data.event === 'error' && data.code === INVALID_API_KEY_ERROR) { console.error('Detected auth failure - closing websocket'); wsClient.close(data.wsKey); }}); // Optional, connect before subscribing:wsClient.connectPrivate(); // This is optional though. The wsclient will automatically open and subscribe if the connection doesn't exist yet. // Subscribe one event at a time:wsClient.subscribe({ channel: 'account',}); We use essential cookies and optional analytics. Read the Privacy Policy.
Essential cookies stay on. Toggle analytics if you want to share anonymous usage insights. You can revisit this anytime via Cookie Settings in the footer.