CPPKiteConnect
CPPKiteConnect Documentation

Overview

The official C++ client for communicating with Kite Connect API.

CPPKiteConnect is a header-only library that wraps around Zerodha's KiteConnect REST API and WebSockets API. It saves you the hassle of directly communicating with the APIs and provides an easy to use, native and modern C++ interface.

Dependencies

CPPKiteConnect requires C++17 and following dependancies:

Getting dependencies

Linux

  • On Fedora 32: sudo dnf install openssl-devel zlib-devel + (uWS v0.14) + (gtest-devel gmock-devel for running tests)
  • On Ubuntu: sudo apt install libssl-dev zlib1g-dev + (uWS v0.14) + (googletest, googlemock for running tests)

Others & uWS v0.14

Use package managers provided by your OS. Unless your package manager provides v0.14 of uWS, you'll have to build and install it manually.

You can also download source of the required dependencies by running cmake . in deps directory. This will place files in the same directory.

Building & Installation

Clone the repository and fetch the submodules

git clone https://github.com/zerodha/cppkiteconnect.git
submodule update --init --recursive

CPPKiteConnect is a header-only library. Copy the include folder to system or project's include path.

Build

mkdir build && cd build
cmake .. -DBUILD_TESTS=On <other-options>
make

If cmake cannot find your uWS library, try providing it manually to cmake like cmake .. -DUWS_LIB=/path/to/uWS.so. Note that this will build the library but some tests might not be run.

Build options

Option Description
BUILD_TESTS Build tests
BUILD_EXAMPLES Build examples
BUILD_DOCS Build docs

Run tests

make && make test ARGS='-V'

Generate docs

make docs

Examples

REST API

#include <cstdlib>
#include <iostream>
#include "kitepp.hpp"
namespace kc = kiteconnect;
int main() {
try {
kc::kite Kite(std::getenv("KITE_API_KEY"));
std::string apiSecret = std::getenv("KITE_API_SECRET");
std::cout << "login URL: " << Kite.loginURL() << '\n';
std::cout << "login with this URL and obtain the request token\n";
std::string reqToken;
std::cout << "enter obtained request token: ";
std::cin >> reqToken;
std::string accessToken =
Kite.generateSession(reqToken, apiSecret).tokens.accessToken;
Kite.setAccessToken(accessToken);
std::cout << "access token is " << Kite.getAccessToken() << '\n';
kc::userProfile profile = Kite.profile();
std::cout << "name: " << profile.userName << "\n";
std::cout << "email: " << profile.email << "\n";
} catch (kc::kiteppException& e) {
std::cerr << e.what() << ", " << e.code() << ", " << e.message() << '\n';
} catch (kc::libException& e) {
std::cerr << e.what() << '\n';
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
};
return 0;
};

Ticker

#include <iostream>
#include "kitepp.hpp"
namespace kc = kiteconnect;
void onConnect(kc::ticker* ws) {
std::cout << "Connected.. Subscribing now..\n";
ws->setMode("full", { 408065, 2953217 });
};
void onTicks(kc::ticker* ws, const std::vector<kc::tick>& ticks) {
for (const auto& i : ticks) {
std::cout << "instrument token: " << i.instrumentToken
<< " last price: " << i.lastPrice << "\n";
};
};
void onError(kc::ticker* ws, int code, const std::string& message) {
std::cout << "Error! Code: " << code << " message: " << message << "\n";
};
void onConnectError(kc::ticker* ws) { std::cout << "Couldn't connect..\n"; };
void onClose(kc::ticker* ws, int code, const std::string& message) {
std::cout << "Closed the connection.. code: " << code
<< " message: " << message << "\n";
};
int main(int argc, char const* argv[]) {
kc::ticker Ticker(std::getenv("KITE_API_KEY"), 5, true, 5);
Ticker.setAccessToken(std::getenv("KITE_ACCESS_TOKEN"));
Ticker.onConnect = onConnect;
Ticker.onTicks = onTicks;
Ticker.onError = onError;
Ticker.onConnectError = onConnectError;
Ticker.onClose = onClose;
Ticker.connect();
Ticker.run();
Ticker.stop();
return 0;
};

More examples can be found in the examples directory.

Documentation

License

MIT

kiteconnect::libException
This exception is thrown when an error occures at the library level.
Definition: exceptions.hpp:206
kiteconnect::kiteppException
CPPKiteConnect saves you the hassle of detecting API errors by manually checking HTTP codes or JSON e...
Definition: exceptions.hpp:50
kiteconnect::kiteppException::code
int code() const noexcept
Get HTTP code sent by the REST API.
Definition: exceptions.hpp:68
kiteconnect::ticker
ticker wraps around the websocket API provided by KiteConnect and provides a native interface.
Definition: ws.hpp:66
kiteconnect::ticker::setMode
void setMode(const string &mode, const std::vector< int > &instrumentTokens)
Set the subscription mode for a list of instrument tokens.
Definition: internal.hpp:132
kiteconnect::kiteppException::message
const char * message() const noexcept
Get error message sent by the REST API.
Definition: exceptions.hpp:75
kiteconnect::kiteppException::what
const char * what() const noexcept override=0
Provides short descripion of the error.
kiteconnect::kite
kite represents a KiteConnect session. It wraps around the KiteConnect REST API and provides a native...
Definition: kite.hpp:48
kiteconnect::libException::what
const char * what()
Provides short description of the error.
Definition: exceptions.hpp:216
kiteconnect::userProfile
Represents an user's profile.
Definition: user.hpp:42