OKX JavaScript SDK example: ws-private-handle-auth-fail.ts

OKX websocket private handle auth fail JavaScript example. Read the source code, review its SDK calls, and browse related exchange integration examples.

This example uses okx-api, the JavaScript and TypeScript SDK for OKX. Install it with npm install okx-api.

We use environment variables in our examples for API keys. It is your responsibility to manage and secure your keys appropriately.

examples/OKX/Websocket/ws-private-handle-auth-fail.ts

What this example covers

  • OKX WebSocket stream JavaScript example.
  • Uses the Siebly OKX SDK package okx-api instead of hand-written WebSocket plumbing.
  • Source path: OKX/Websocket/ws-private-handle-auth-fail.ts.
  • 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.
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',}); 

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.