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.

kiteconnect-ts is designed for server-side JavaScript environments. You cannot use it in Angular, React, Vue, or any other browser-based front-end. Attempting to call any Kite API endpoint directly from a browser will result in a CORS error.
This is a restriction imposed by Zerodha, not a limitation of kiteconnect-ts. Zerodha intentionally does not allow direct browser access to the Kite Connect HTTP APIs. See the Zerodha forum thread for the official explanation.

Where kiteconnect-ts works

kiteconnect-ts runs anywhere Node.js is available:
  • Node.js scripts and long-running servers — the primary use case
  • Serverless functions — AWS Lambda, Vercel Functions, Netlify Functions, Cloudflare Workers (Node.js-compatible runtime)
  • Meta-frameworks with server-side rendering — Next.js, Nuxt.js, SvelteKit, Remix, when used in server-only code paths

Using with Next.js API routes

You can use kiteconnect-ts inside Next.js API routes or server components. The library must never be imported from client-side code.
// pages/api/instruments.ts  (Next.js Pages Router)
import type { NextApiRequest, NextApiResponse } from 'next';
import { KiteConnect, Instrument } from 'kiteconnect-ts';

const kc = new KiteConnect({
  api_key: process.env.KITE_API_KEY!,
  access_token: process.env.KITE_ACCESS_TOKEN,
});

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Instrument[]>
) {
  const instruments = await kc.getInstruments(['NSE']);
  res.status(200).json(instruments);
}
Store your api_key and access_token in environment variables and never expose them to the client bundle. Next.js only includes variables prefixed with NEXT_PUBLIC_ in the browser build.

Connecting to Kite WebSocket from a browser

While the REST API is blocked by CORS, the Kite WebSocket endpoint (wss://ws.kite.trade) can be connected to directly from a browser using the native WebSocket API. You will need to write your own binary parser, since KiteTicker (which handles parsing internally) also relies on Node.js APIs. Here is a complete minimal tick parser that reads token, firstBid, and firstAsk from each packet. Adapt it to extract whichever fields you need using the Kite WebSocket message structure docs.
// Tick structure reference: https://kite.trade/docs/connect/v3/websocket/#message-structure
const parseBinary = (dataView: DataView) => {
  const numberOfPackets = dataView.getInt16(0);
  let index = 4;
  const ticks: { token: number; firstBid: number; firstAsk: number }[] = [];

  for (let i = 0; i < numberOfPackets; i++) {
    const size = dataView.getInt16(index - 2);

    // Parse whatever you need
    ticks.push({
      token: dataView.getInt32(index),
      firstBid: dataView.getInt32(index + 68) / 100,
      firstAsk: dataView.getInt32(index + 128) / 100,
    });

    index = index + 2 + size;
  }

  return ticks;
};

const API_KEY = 'INSERT_API_KEY_HERE';
const ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE';

const ws = new WebSocket(
  `wss://ws.kite.trade?api_key=${API_KEY}&access_token=${ACCESS_TOKEN}`
);

ws.onopen = (_event) => {
  console.log('Connected to Zerodha Kite Socket!');

  const setModeMessage = { a: 'mode', v: ['full', [61512711]] };
  ws.send(JSON.stringify(setModeMessage));
};

ws.onerror = (error) => {
  console.log('Some error occurred', error);
};

ws.onmessage = async (message) => {
  if (message.data instanceof Blob && message.data.size > 2) {
    const arrayBuffer = await message.data.arrayBuffer();
    const dataView = new DataView(arrayBuffer);
    const ticks = parseBinary(dataView);
    console.log(ticks);
  }
};
Never expose your access_token in client-side code that is shipped to end users. Obtain the token server-side and pass only what is necessary to the browser, or proxy the WebSocket connection through your own server.

Build docs developers (and LLMs) love