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.

This guide walks you through installing kiteconnect-ts, authenticating with the Kite Connect API, and making your first API call. By the end you will have a working session and know how to stream live market ticks using KiteTicker.

KiteConnect quickstart

1

Install the package

Add kiteconnect-ts to your Node.js project using your preferred package manager.
npm install kiteconnect-ts
2

Initialize KiteConnect

Import the KiteConnect class and create an instance with your API key. You can get your API key from the Kite Connect developer portal.
import { KiteConnect } from 'kiteconnect-ts';

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

Get the login URL

Generate the login URL and redirect your user to it. After a successful login, Zerodha redirects the user back to your registered redirect URL with a request_token in the query parameters.
const loginUrl = kc.getLoginURL();
console.log('Redirect user to:', loginUrl);
// https://kite.zerodha.com/connect/login?api_key=YOUR_API_KEY&v=3
You configure the redirect URL in the Kite Connect developer portal when you create your app. Your server must be listening at that URL to receive the request_token after login.
4

Generate a session

Once your server receives the request_token from the redirect callback, exchange it for an access_token using generateSession(). This call also sets the access token on the kc instance automatically.
try {
  const session = await kc.generateSession(
    'request_token_from_callback',
    'YOUR_API_SECRET'
  );

  console.log('Access token:', session.access_token);
  console.log('User:', session.user_name);
} catch (error) {
  console.error('Session generation failed:', error);
  process.exit(1);
}
Your api_secret must never be exposed to the browser or included in client-side code. Always call generateSession() from your server.
5

Make your first API call

With the session established, call any of the typed API methods. The getProfile() call returns a fully-typed UserProfile object, and getMargins() returns typed UserMargin data for the requested segment.
// Fetch user profile
const profile = await kc.getProfile();
console.log('Logged in as:', profile.user_name);
console.log('Exchanges:', profile.exchanges);

// Fetch equity margins
const margins = await kc.getMargins('equity');
console.log('Available cash:', margins.equity?.available.cash);
console.log('Net margin:', margins.equity?.net);

KiteTicker quickstart

KiteTicker connects to Zerodha’s WebSocket endpoint and streams real-time market ticks. You need a valid access_token before connecting.
1

Initialize KiteTicker

Import KiteTicker and create an instance with your API key and access token.
import { KiteTicker, Tick } from 'kiteconnect-ts';

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

Register event handlers

Register handlers for the ticks and connect events before calling connect(). The connect event fires once the WebSocket connection is established — subscribe to instruments from inside this handler.
ticker.on('ticks', (ticks: Tick[]) => {
  console.log('Received ticks:', ticks);
});

ticker.on('connect', () => {
  // Instrument tokens to subscribe to (e.g. RELIANCE on NSE)
  const instruments = [738561];
  ticker.subscribe(instruments);
  ticker.setMode(ticker.modeFull, instruments);
});

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

Connect

Call connect() to open the WebSocket connection. The client handles the initial handshake and will begin emitting ticks events once subscribed instruments start sending data.
ticker.connect();
KiteTicker enables auto-reconnection by default using an exponential backoff algorithm. You can configure the maximum number of retries and the maximum delay interval with ticker.autoReconnect(true, maxTries, maxDelay). See KiteTicker overview for details.

Next steps

Authentication

Learn how to persist your access token, handle expiry, and implement logout.

Place orders

Place, modify, and cancel equity and derivative orders.

Market data

Fetch live quotes, OHLC, LTP, and historical candle data.

WebSocket events

Handle all ticker events including ticks, reconnect, and order updates.

Build docs developers (and LLMs) love