KiteConnect postback example

Hi traders,

Here is a simple generic example for implementing a postback mechanism. You can host this in a remote server or in your local server itself.
  • Shaha
    Thanks. Will definately try this. You saved my lot of time.
  • urrakesh2053
    Hi @tonystark

    I have already followed above link step by step to setup postback. Everything looks fine but somehow script "" is not throughing out results in its log. log file is totally empty throughout (even if n numbers of orders executed).

    I can see POST/GET lines entries at ngrok online console.

    but I don't know how to take these data into my main python script. I am having tough time in fetching json data into my python script.

    any other reference ??

  • tonystark
    tonystark edited December 2017
    You should give the post back URL as https://yourdomain/post
  • urrakesh2053
    Hi @tonystark ,

    Right now I have given as postback url at my kite app dashboard.
    Are you suggesting that I should use ?

    following is the screenshot of my kite app dashboard

  • tonystark
    tonystark edited December 2017
  • urrakesh2053
    tried that... got following error - - [27/Dec/2017 13:43:45] "POST /post HTTP/1.1" 500 -
    Traceback (most recent call last):
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 33, in reraise
    raise value
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1817, in wsgi_app
    response = self.full_dispatch_request()
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 33, in reraise
    raise value
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
    File "/home/rakesh/anaconda3/lib/python3.4/site-packages/flask/", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "/home/rakesh/wksp1/my_trade/GoogleFin/Kite_new/", line 69, in post
    f.write(request.get_data() + '\n')
    TypeError: can't concat bytes to str
  • tonystark
    Can you try changing the line:

    f.write(request.get_data() + '\n')

  • urrakesh2053
    urrakesh2053 edited December 2017
    Found the resolution. :)

    Following correction is working fine now.


    Thanks for the reply.

    So, Idea is any placed order gets executed, there will be a new post appended in log file. This is working totally fine now. However, i have to keep on polling this log file to detect any change in log file and read last line through my script. I can google it how to do it in python, but do you have any piece of reliable (best way) code to do that. Kindly share that too.

    Thanks a lot again.
  • tonystark
    You can actually integrate that python script into yours. Instead of writing it into a file you can call another function with that request data for further processing. The above gist can be reduced to:
    from flask import Flask, request
    app = Flask(__name__)

    @app.route('/post', methods=['POST'])
    def post():
    return 'done', host='', port=80)
  • jjiteshh
    jjiteshh edited November 2018
    Hey guys,

    I am trying to set up the postback url as mentioned here. I get an error 404 not found and the request is not POST its coming as HEAD.

    I have set up exactly like above using ngrok and flask app. I am able receive response made from other POST requests.

    I am suspecting this might not be working as the I am trying this after market hours. I will try this again tomorrow morning.
  • sujith
    sujith edited November 2018
    The HEAD request is made to validate the URL when a user changes it on the developers console.
  • jjiteshh

    yes as soon as i update the link on the console i get this message.

    But i am not able to receive any messages related to orders. I have tried using postman to send json data and it works from that end.
  • jjiteshh
    never mind,

    I received the data when the order got cancelled. I was expecting a message when the order was placed. But this is okay.

This discussion has been closed.