In this article we will explain how can you connect to Zerodha KiteConnect API, with step by step explanation easy to understand for beginners.
In order to understand this article, you need to have already opened a Zerodha account and also opened an account with Kite. Trade. And also you need to have your API key and access token from your kite. trade account ready in order to proceed. If you don’t know how to open a Zerodha kite connect account you can check this article. and in order to open an account with Zerodha click here
Requirements to connect to Zerodha API
you need the following thing ready
API key from your kite. trade account
API secret from your kite. trade account
Access to both live and historical data.
Zerodha Broker account
Installed any python IDE on your computer as the code we explain is python.
If you have all the above things ready we can proceed further. Connecting to kite connect API generally involves multiple steps.
The first step is to install the KiteConnect module on your system. To install the KiteConnect module open your command prompt and if you are using Anaconda, you can open anaconda prompt. Use the below code to install kite connect module
pip install --upgrade kiteconnect
The next step is to connect to import all the necessary modules into the python code. As we are only explaining how to connect to Zerodha API, we need only one module that is the KiteConnect module. Open your Python IDE and create a new python file. Pythons files will have .py as extension. Use the below code to import Kiteconnect module
from kiteconnect import KiteConnect
In the above code, keep an eye on the Upper case and Lowercase. Python is case sensitive and if you type lowercase in place of uppercase it will throw an error.
Save this file and run the code. See if you are getting any error. If you get any error, it means that your kite connect installation is not successful. Please check Installation of your kite connect module again. If your code runs without any error, then you can proceed to the next step.
After importing the module, now we need to connect to the kite connect using our API key and API secret. create variables and give your api key and access token as shown below.
create a kite object which uses your API key and API secret to connect to your kite. trade account. Use the below code to create a variable named kite using your API key and API secret.
kite = KiteConnect(api_key, api_secret)
Now you can use kite variable to connect to kiteconnect.
( This step should be executed only once in every 24 hours)
Now we need to get a Request token using your Kite API key and API secret. To get a request token, you need to approve your Zerodha broker account to connect with the kite. trade app. In order to do that you need to use the below code to generate a login URL
now run the file and you will get an output as shown below
(This step should be executed only once in every 24 hour)
Now copy this URL to your browser and log in with your Zerodha broker account. Approver access to your Zerodha broker account. Once you approve the access, you will get the page as shown in the below image.
The request token will be available on the browser URL. The request token is highlighted in Red box in the above image. Copy the request token from after the =’ site to till ‘&’ sign.
Now we have the request token. Using this request token, we need to generate an ‘Access token”. First, assign the copied request token to a variable named ‘request_token’/ After that we need the request token and API secret for generating the Access token. For generating the access token, we will use the below code.
request_token = xxxxxxxxxx
data = kite.generate_session(request_token, api_secret)
Save the file and run the code. You will get the ‘Access token’ as output when you execute the code.
Copy the Access token and assign it to a variable named ‘access_token’
access_token = xxxxxxxxxx
Now you have the access token. Now you can connect to the Kite trade account and get the data about your account and also get live and historical data feeds. For that to happen, you need to set the access token using the ‘kite’ object we created.
Now add the above code and remove the code that is mentioned in the step 5 and step 7. After removing the code run the code. Your code should work fine without any errors.
Now you have connected to the kite api. you can check by using the below simple commands to get details about your broker account.
kite.order() #this code will get all the orders that you have placed today
kite.ltp(424961) #this code will get the last traded price of ITC as 424961 token represents ITC.
Step 5 and 7 must be executed only once every 24 hours.
The access token that you have generated in step 7 will expire every 24 hours. Access token usually expires daily morning at 7 am. You need to generate an access token every day at 7 am using steps 5 and 7.
Python is case sensitive. Hence keep in mind while writing the code.
You have successfully connected to kite connect api and you can perform the operations using different commands.