• Stars
    star
    314
  • Rank 133,353 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The official typescript client library for the Kite Connect trading APIs

The Kite Connect API Javascript client - v4

The official Javascript node client for communicating with the Kite Connect API.

Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection.

Zerodha Technology (c) 2018. Licensed under the MIT License.

Documentation

Requirements

  • NodeJS v8.0.0+

Installation

Install via npm

npm install kiteconnect@latest

Or via yarn

yarn add kiteconnect

Breaking changes - v4

v4 is a breaking major release with multiple internal modification to improve user experience.

Below are the breaking changes:

  • Upgrade deps and set minimum nodejs version to 8.0.0+
  • Return promise instead of throwing error on generateSession and renewAccessToken
  • Handle gtt payload validation and throw proper error
  • Change ticker response attributes naming as per kite connect doc

Getting started with API

var KiteConnect = require("kiteconnect").KiteConnect;

var kc = new KiteConnect({
  api_key: "your_api_key",
});

kc.generateSession("request_token", "api_secret")
  .then(function (response) {
    init();
  })
  .catch(function (err) {
    console.log(err);
  });

function init() {
  // Fetch equity margins.
  // You can have other api calls here.
  kc.getMargins()
    .then(function (response) {
      // You got user's margin details.
    })
    .catch(function (err) {
      // Something went wrong.
    });
}

API promises

All API calls returns a promise which you can use to call methods like .then(...) and .catch(...).

kiteConnectApiCall
  .then(function (v) {
    // On success
  })
  .catch(function (e) {
    // On rejected
  });

Getting started WebSocket client

var KiteTicker = require("kiteconnect").KiteTicker;
var ticker = new KiteTicker({
  api_key: "api_key",
  access_token: "access_token",
});

ticker.connect();
ticker.on("ticks", onTicks);
ticker.on("connect", subscribe);

function onTicks(ticks) {
  console.log("Ticks", ticks);
}

function subscribe() {
  var items = [738561];
  ticker.subscribe(items);
  ticker.setMode(ticker.modeFull, items);
}

Auto re-connect WebSocket client

Optionally you can enable client side auto re-connection to automatically reconnect if the connection is dropped. It is very useful at times when client side network is unreliable and patchy.

All you need to do is enable auto re-connection with preferred interval and time. For example

// Enable auto reconnect with 5 second interval and retry for maximum of 20 times.
ticker.autoReconnect(true, 20, 5);

// You can also set re-connection times to -1 for infinite re-connections
ticker.autoReconnect(true, -1, 5);
  • Event reconnecting is called when auto re-connection is triggered and event callback carries two additional params reconnection interval set and current re-connection count.

  • Event noreconnect is called when number of auto re-connections exceeds the maximum re-connection count set. For example if maximum re-connection count is set as 20 then after 20th re-connection this event will be triggered. Also note that the current process is exited when this event is triggered.

  • Event connect will be triggered again when re-connection succeeds.

Here is an example demonstrating auto reconnection.

var KiteTicker = require("kiteconnect").KiteTicker;
var ticker = new KiteTicker({
  api_key: "api_key",
  access_token: "access_token",
});

// set autoreconnect with 10 maximum reconnections and 5 second interval
ticker.autoReconnect(true, 10, 5);
ticker.connect();
ticker.on("ticks", onTicks);
ticker.on("connect", subscribe);

ticker.on("noreconnect", function () {
  console.log("noreconnect");
});

ticker.on("reconnecting", function (reconnect_interval, reconnections) {
  console.log(
    "Reconnecting: attempt - ",
    reconnections,
    " innterval - ",
    reconnect_interval
  );
});

function onTicks(ticks) {
  console.log("Ticks", ticks);
}

function subscribe() {
  var items = [738561];
  ticker.subscribe(items);
  ticker.setMode(ticker.modeFull, items);
}

Run unit tests

npm run test

Generate documentation

$ npm install -g jsdoc
$ jsdoc -r ./lib -d ./docs

Changelog

Check CHANGELOG.md

A typical web application

In a typical web application where a new instance of views, controllers etc. are created per incoming HTTP request, you will need to initialise a new instance of Kite client per request as well. This is because each individual instance represents a single user that's authenticated, unlike an admin API where you may use one instance to manage many users.

Hence, in your web application, typically:

  • You will initialise an instance of the Kite client
  • Redirect the user to the login_url()
  • At the redirect url endpoint, obtain the request_token from the query parameters
  • Initialise a new instance of Kite client, use request_access_token() to obtain the access_token along with authenticated user data
  • Store this response in a session and use the stored access_token and initialise instances of Kite client for subsequent API calls.

More Repositories

1

pykiteconnect

The official Python client library for the Kite Connect trading APIs
Python
981
star
2

dungbeetle

A distributed job server built specifically for queuing and executing heavy SQL read jobs asynchronously. Separate out reporting layer from apps. MySQL, Postgres, ClickHouse.
Go
506
star
3

javakiteconnect

The official Java client for communicating with Kite Connect API.
Java
203
star
4

gokiteconnect

Official Go client for Kite Connect API's
Go
176
star
5

nomad-cluster-setup

Terraform modules for creating Nomad servers and clients nodes on AWS.
HCL
144
star
6

frappe-attachments-s3

A frappe app to upload file attachments in doctypes to s3.
Python
121
star
7

logf

Extremely fast, light weight, zero alloc logfmt logging library for Go.
Go
91
star
8

gchatgpt

Google Chat bot for OpenAI ChatGPT
Go
81
star
9

fastglue

Fastglue is an opinionated, bare bones wrapper that glues together fasthttp and fasthttprouter to act as a micro HTTP framework.
Go
80
star
10

dotnetkiteconnect

.NET library for Kite connect
C#
78
star
11

kaf-relay

Replicate and sync Kafka topics between clusters in realtime. Supports topic re-mapping, healthchecks, and hot failovers for high availability.
Go
67
star
12

kite-connect-python-example

Kite connect Python client example
Python
62
star
13

simplesessions

simplesessions is a Go session management library that is completely agnostic of HTTP libraries and frameworks, backend stores, and even cookie jars.
Go
62
star
14

rbiparser

A utility for downloading, parsing and sanitizing bank database (IFSC, MICR, address etc.) Excel sheets from the RBI website.
Python
54
star
15

zerodhatech.github.io

The zerodha.tech blog
HTML
48
star
16

cppkiteconnect

C++ Kite Connect API library / SDK
C++
47
star
17

kiteconnect-rs

The official Rust client library for the Kite Connect trading APIs
Rust
45
star
18

jpdfsigner

A HTTP server and a CLI for digitally signing PDFs.
Java
43
star
19

phpkiteconnect

The official PHP client library for the Kite Connect trading APIs
PHP
43
star
20

fastcache

fastcache is an HTTP response caching package that plugs into fastglue that simplifies "dumb" caching of API endpoints.
Go
34
star
21

py-frappe-client

Frappe client for humans
Python
31
star
22

pdf_text_overlay

pdf_text_overlay is a python library to write text on top of pdf.
Python
27
star
23

vendor-payments

A frappe app that has workflows and reports to make payments to vendors by a company and track them
Python
26
star
24

kiteconnect-mocks

Mock responses for kiteconnect
25
star
25

mii-lama

A tool for posting metrics from node-exporter to LAMA (Indian stock market regulatory framework) API gateways
Go
19
star
26

rms-consolidated-scrips-status

A utility that parse Zerodha Consolidated google spreadsheets and render category-wise scrip details(margins, multiplier, etc)
Python
19
star
27

fastglue-csrf

CSRF middleware for https://github.com/zerodha/fastglue
Go
15
star
28

nithinkamath.me

HTML
13
star
29

fastglue-metrics

Prometheus Metrics exposed for Fastglue HTTP Handlers.
Go
12
star
30

subscription_coupons

Subscription discount coupon code manager
Python
11
star
31

osticket-autoassign

Osticket plugin to assign tickets automatically to random agents based on teams, department, and activity
PHP
11
star
32

osticket-archive

A utility to archive all closed tickets beyond a certain age to disk (including attachments) and delete them from the database. The tickets are archived as JSON files.
PHP
8
star
33

flask-kiteconnect

Flask extension for kiteconnect API
Python
7
star
34

python-wheels

Python wheels used in other Python projects
5
star
35

kite-discourse-sso

Discourse SSO in Go for Kite Connect. Serves as a template for implementing other Discourse integrations.
Go
5
star
36

fastglue-adapter

net/http adapter for fastglue
Go
3
star
37

pdfrender

Python
2
star