Here are the steps to go about:
------------------------------------------Part 1 Begins------------------------------------------
1. You will need to signup for a Kit Connect Developers account.
https://developers.kite.trade/signup
2. Once signup, you will need to deposit Rs.2000/- to your developer account. Only when you deposit it, you would be able to create your auto trading application.
3. Whie signing up, provide your Zerodha Trading Account (If you dont have zerodha account, download the forms from
Here. and send the courier to Zerodha banglore office.)
4. Once developer account is created, you will need to create an app for your autotrading. (This option is available once you open a developer account)
5. To create an app, select "Publisher + Kite Connect" app.
6. You will need your own https web url (I think you might not need this if you are placing orders from your local machine. in that case enter "127.0.0.1" in redirect url field.)
7. Once an app is created, you will get your api_key and api_secret. This two would be used when you create your login session for auto trading.
----------------------------------------------Part 1 Complete-----------------------------------------------
-----------------------------------------------Part 2 Begins ------------------------------------------------------------
1. Flow of login
a. You will first login to your account using your api_key using the below url
https://kite.trade/connect/login?api_key=xxx
ex:
https://kite.zerodha.com/connect/login?api_key=uhogbq3fio9viw33
b. This will redirect you to the "redirect url" you mentioned when you created your App.
c. Now, in the redirect url, you would receive "request token".
d. Use this "request token " along with your "api_key" and "api_secret" to generate "checksum".
e. This checksum is then used to send another http request which will give you access_token.
2. The "api_key" got from while creating the App and the "access_token" from the above http url is used in all subsequent request to place orders.
3. The php file that does this is attached in this post with name "login-success.php". This file needs to be uploaded to your webserver address which you have specified in your app creation.
4. I will attach all the images of my app creation to give you better idea and help.
-----------------------------------------------Part 2 Ends-------------------------------------------------------
Contents of login-success.php file
PHP:
<?php
function customError($errno, $errstr) {
echo "There has been some error. Please clear browser cache and relogin.<br/>$errstr";
}
//set error handler
set_error_handler("customError");
echo "The request token received is : ".$_GET["request_token"];
$api_key = "your api_key";
$secret_key = "your secret key";
// Incoming from the redirect.
$request_token = $_GET["request_token"];
$checksum = hash("sha256", $api_key . $request_token . $secret_key);
echo "<br/>".$checksum;
$url = 'https://api.kite.trade/session/token';
$data = array('api_key' => $api_key, 'request_token' => $request_token, 'checksum' => $checksum);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result == FALSE) {
echo "<h1 style='color:red;'>Error while logging in.</h1>";
}else{
echo "<h1>You are logged in succesfully.</h1>";
}
//var_dump($result);
$out = json_decode($result, true);
echo $out['data']['user_name'];
echo $out['data']['access_token'];
?>
<body>
<input type="hidden" id="zerodha_token" value="<?php echo $out['data']['access_token']; ?>">
<input type="hidden" id="zerodha_user" value="<?php echo $out['data']['user_name']; ?>">
</body>
Various images to show how to register for an app in Kite Connect Developer Console.
---------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Here is the php code to place order directly to your Zerodha account once you know your api_key and access_token.
PHP:
<?php
$api_key = "uhogbq3fio9viw33";
$access_token = $_GET["access_token"];
$tradingSymbol = $_GET["tradingsymbol"];
$exchange = $_GET["exchange"];
$transactionType = $_GET["transactionType"];
$orderType = $_GET["orderType"];
$price = $_GET["price"];
$quantity = $_GET["quantity"];
$triggerPrice = $_GET["triggerPrice"];
//echo $access_token."-".$tradingSymbol."-".$exchange."-".$transactionType."-".$orderType."-".$price."-".$quantity;
$url = 'https://api.kite.trade/orders/regular';
$data = array('api_key' => $api_key, 'access_token' => $access_token, 'tradingsymbol' => $tradingSymbol, 'exchange' => $exchange, 'transaction_type' => $transactionType, 'order_type' => $orderType, 'quantity' => $quantity, 'price' => $price, 'trigger_price' => $triggerPrice, 'product' => 'MIS', 'validity' => 'DAY');
// use key 'http' even if you send the request to https://...
$content = http_build_query($data);
//var_dump($content);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => $content,
),
);
$context = stream_context_create($options);
$result = file_GET_contents($url, false, $context);
if ($result === FALSE) { echo "Error"; }
echo $result;
?>