Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/anurag-roy/kiteconnect-ts/llms.txt

Use this file to discover all available pages before exploring further.

Every KiteConnect API method returns a Promise that can reject. When a request fails, the rejection value is a plain object — not a native Error instance — with a consistent shape that you can inspect to determine what went wrong and how to recover.

Error object structure

A rejected promise carries an object with three fields:
{
  message: string;      // Human-readable description
  error_type: string;   // Machine-readable error category
  data: null | any;     // Additional data (usually null on errors)
}
You can destructure or check error_type in your catch block to branch on specific failure modes.

Common error types

TokenException

The access token has expired or is invalid. This is the most common error in production. Use setSessionExpiryHook to handle it automatically.

DataException

The API returned an unrecognised content type. This usually means the upstream service returned an unexpected response format.

NetworkException

Either no response was received from the server, or the HTTP response had a non-2xx status code without a structured error body.

GeneralException

A catch-all for any other error that does not fit the categories above.

Handling errors in code

You can use either async/await with try/catch or the Promise .catch() method. Both are equivalent.
import { KiteConnect } from 'kiteconnect-ts';

const kc = new KiteConnect({ api_key: 'YOUR_API_KEY' });

try {
  const { access_token } = await kc.generateSession(
    'request_token',
    'YOUR_API_SECRET'
  );
  console.log('Access token:', access_token);
} catch (error) {
  console.error('Error while generating session', error);
  process.exit(1);
}

Automatic session expiry handling

Rather than checking for TokenException manually in every call, register a single callback with setSessionExpiryHook. The client calls it automatically whenever a TokenException is received.
import { KiteConnect } from 'kiteconnect-ts';

const kc = new KiteConnect({ api_key: 'YOUR_API_KEY' });

// Register the hook once after initialisation
kc.setSessionExpiryHook(() => {
  console.log('Session expired — redirecting to login');
  // Clear stored tokens, redirect to login flow, etc.
});

// All subsequent API calls will trigger the hook automatically
// on TokenException, without any extra try/catch needed
const margins = await kc.getMargins('equity');
The session expiry hook is called in addition to the promise rejection. You still need to handle the rejected promise in your call site — the hook is a side-effect, not a recovery mechanism.

KiteTicker error events

The KiteTicker WebSocket client surfaces errors through events rather than rejected promises.
import { KiteTicker } from 'kiteconnect-ts';

const ticker = new KiteTicker({
  api_key: 'YOUR_API_KEY',
  access_token: 'YOUR_ACCESS_TOKEN',
});

ticker.on('error', (error: Error) => {
  console.error('Ticker error:', error);
});

ticker.on('disconnect', (error: Error) => {
  console.log('Ticker disconnected:', error.message);
});

ticker.on('reconnect', (reconnect_count: number, reconnect_interval: number) => {
  console.log(
    `Reconnecting: attempt ${reconnect_count}, next in ${reconnect_interval}s`
  );
});

ticker.on('noreconnect', () => {
  console.log('Max reconnection attempts reached');
});

ticker.connect();
The default noreconnect handler in kiteconnectjs calls process.exit(1). kiteconnect-ts preserves this behaviour for compatibility. Override the noreconnect event with your own handler if you need to perform cleanup before the process exits.

Build docs developers (and LLMs) love