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.
CPPKiteConnect requires C++17 and following dependancies:
- OpenSSL (devel)
- uWebSockets v0.14 (devel) and its dependancies.
- googletest and googlemock are required for running tests.
- Doxygen is required for generating documentation.
- 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)
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.
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.
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.
Option | Description |
---|---|
BUILD_TESTS |
Build tests |
BUILD_EXAMPLES |
Build examples |
BUILD_DOCS |
Build docs |
In project directory,
docker build -t cppkiteconnect-dev .
docker run -it -e EXAMPLE_NUMBER=1 cppkiteconnect-dev
Option | Description |
---|---|
EXAMPLE_NUMBER |
Example to run (e.g., 1 ) |
KITE_API_KEY |
Kite API key |
KITE_ACCESS_TOKEN |
Kite access token |
KITE_API_SECRET |
Kite API secret |
make && make test ARGS='-V'
make docs
#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;
};
#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.