Zerodha trading API - Setting up two factor authentication with jugaad-trader and pyotp

Zerodha’s two factor authentication makes automatic login difficult

Since Zerodha has made the two factor authentication using totp mandetory, it has become difficult to login without manual intervention. Loging in using jtrader zerodha startsession still works but the session will last only for 24 hours and we still have to login daily.

For a fully auatomated system this can become challenging. But dont worry, we have just the solution for you.

We will use pyotp library to automatically generate time based OTPs eliminating the need of manual intervention. We will take you through the step by step process on how to setup this.

Re-do the steps to enable two factor authentication, you missed a step

In order to generate the TOTP from Python, we need the secret key that Zerodha creates when we enable the two factor authentication for the first time. In case you have not copied this secret key, you will have to disable the two factor authentication and enable it again.

You can follow this instruction to enable two factor authentication again.

Make sure you copy and securely store the secret key as shown below, we will later use that in our code. You can still use the external authenticator as before for your day to day login.

Let’s code

First create a file named creds.ini and store your username, password and secret key copied from previous step. We are already committing a sin of storing our passwords in plain text 😛, not mixing the code and credentials is the least we can do. In production, you should use a more robust method.

[DEFAULT]
user_id = Your Zerodha user id
password = Your Zerodha password
totp_secret = Secret key from previous step

Let’s install required packages-

!pip install jugaad-trader pyotp

Let’s go thru the code, starting with import

import configparser
import pyotp
from jugaad_trader import Zerodha

Let’s read the credentials from the config file

config = configparser.ConfigParser()
config.read('creds.ini')
user_id = config['DEFAULT']['user_id']
password = config['DEFAULT']['password']
totp_secret = config['DEFAULT']['totp_secret']

Zerodha uses time based OTPs, so we will use pytop.TOTP class, Let’s initialize it with our secret key.

otp_gen = pyotp.TOTP(totp_secret)

We can use otp_gen.now() to generate current OTP which will be valid for a minute. We need to pass this value as twofa argument to our Zerodha class.

kite = Zerodha(user_id=user_id, password=password, twofa=otp_gen.now())
kite.login()
{'status': 'success', 'data': {'profile': {}}}

Voila! We are logged in, without any manual intervention! Let’s look at our holdings

kite.holdings()[0]
{'tradingsymbol': 'BAJAJFINSV',
 'exchange': 'BSE',
 'instrument_token': 136442372,
 'isin': 'INE918I01026',
 'product': 'CNC',
 'price': 0,
 'quantity': 60,
 'used_quantity': 0,
 't1_quantity': 0,
 'realised_quantity': 60,
 'authorised_quantity': 0,
 'authorised_date': '2023-11-16 00:00:00',
 'authorisation': {},
 'opening_quantity': 60,
 'short_quantity': 0,
 'collateral_quantity': 0,
 'collateral_type': '',
 'discrepancy': False,
 'average_price': 990.088333,
 'last_price': 1620.15,
 'close_price': 1594.05,
 'pnl': 37803.700020000004,
 'day_change': 26.100000000000136,
 'day_change_percentage': 1.637338853862811}

With that let’s wrap up. Happy trading!