OKX TypeScript SDK example: ws-private-handle-auth-fail.ts
OKX WebSocket websocket private handle auth fail example for the Siebly OKX SDK, with TypeScript source for exchange REST API and WebSocket integration, setup, and production SDK docs.
What This Example Covers
- OKX WebSocket stream example in TypeScript.
- Uses the Siebly OKX SDK package
okx-apiinstead of hand-written WebSocket plumbing. - Source path:
OKX/Websocket/ws-private-handle-auth-fail.ts. - Example category: WebSocket.
- Imports SDK symbols including
WebsocketClient. - Calls SDK methods such as
on(),close(),connectPrivate(),subscribe().
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
OKX/Websocket/ws-private-handle-auth-fail.ts
Source Link
Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/OKX/Websocket/ws-private-handle-auth-fail.ts
Related SDK Docs
Example Source
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' event
wsClient.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' event
wsClient.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',
});