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.
- NodeJS v8.0.0+
Install via npm
npm install kiteconnect@latest
Or via Yarn
yarn add kiteconnect
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
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.
});
}
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
});
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);
}
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 paramsreconnection interval set
andcurrent 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 as20
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);
}
npm run test
$ npm install -g jsdoc
$ jsdoc -r ./lib -d ./docs
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 theaccess_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.
react's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.