Introduction to Kite Connect API

Kite Connect API is a set of REST-like APIs that provide access to various features of the Zerodha Kite platform. It enables developers to create trading algorithms and client applications with ease. In this blog, we will look at some possible API example codes in JavaScript for the Kite Connect API.

Getting started with Kite Connect API

Before we start coding, we need to first create an account on the Zerodha Kite platform. Once we have created the account, we need to generate API credentials from the Kite Connect dashboard. This is done by following these steps:

  • Log in to the Kite Connect dashboard
  • Click on API Key/Secret section on the left-hand side menu
  • Generate API Key/Secret by clicking the "Generate API Key" button.

Now that we have the API Key/Secret, we are ready to use the Kite Connect API to access various features of the platform.

Access Token

Before we can start making requests to the Kite Connect API, we need to get an access token. This token is obtained by making a request to the /session/token API endpoint. Here's an example code in JavaScript:

var request = require('request');

var options = {
  'method': 'POST',
  'url': 'https://api.kite.trade/session/token',
  'headers': {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  form: {
    'api_key': 'YOUR_API_KEY',
    'request_token': 'YOUR_REQUEST_TOKEN',
    'checksum': 'GENERATED_CHECKSUM'
  }
};

request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Placing orders

Once we have the access token, we can use it to place orders on the Zerodha Kite platform. Here's an example code in JavaScript:

var request = require('request');

var options = {
  'method': 'POST',
  'url': 'https://api.kite.trade/orders/regular',
  'headers': {
    'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    'tradingsymbol': 'RELIANCE',
    'exchange': 'NSE',
    'quantity': '1',
    'transaction_type': 'BUY',
    'order_type': 'LIMIT',
    'product': 'MIS',
    'price': '2000',
    'validity': 'DAY'
  })
};

request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

Conclusion

Kite Connect API is a powerful platform that enables developers to create trading applications with ease. In this blog, we looked at some possible API example codes in JavaScript for the Kite Connect API. These examples should help you get started with creating your own trading algorithms and client applications.

Related APIs