If you’re into algorithmic trading with Zerodha, you’ve probably faced the recurring issue of logging into Kite API every single day to generate a new access token. This daily manual login process breaks automation and makes algo trading less seamless than it should be.
The good news? You can fully automate the Zerodha login process for both the Free Token Method and the Paid Zerodha Kite API. In this guide, I’ll show you exactly how to set it up in Python so that your trading scripts run without manual intervention.
Whether you’re running backtests, deploying live strategies, or building your own algo trading platform, automated login is a huge time-saver.
Why Automate Zerodha Kite API Login?
- No Daily Manual Effort – Normally, you’d need to log in to Kite and manually fetch the request token. Automation skips this step.
- Uninterrupted Algo Execution – Your trading bots can start on their own every morning.
- Professional-Grade Setup – A true algorithmic trading system should handle authentication without human involvement.
Two Ways to Automate Zerodha Login
Zerodha offers two approaches to connect with its API. Depending on whether you’re using the Free Token Hack (unofficial method) or the Paid API version, you’ll need slightly different scripts. Let’s go through both.
1. Free Token Method (Unofficial)
This approach works using enctoken, and while it’s not the officially supported method, many algo traders still use it for quick setups.
Here’s the process in Python:
# ------------------------------------------------------------------------------------
# FabTrader Algorithmic Trading Platform
# ------------------------------------------------------------------------------------
# Copyright (c) 2025 FabTrader
#
# LICENSE: PROPRIETARY SOFTWARE
# - This software is the exclusive property of FabTrader.
# - Unauthorized copying, modification, distribution, or use is strictly prohibited.
# - Written permission from the author is required for any use beyond personal,
# non-commercial purposes.
#
# CONTACT:
# - Website: https://fabtrader.in
# - Email: hello@fabtrader.in
#
# Usage: Internal use only. Not for commercial redistribution.
# Permissions and licensing inquiries should be directed to the contact email.
from datetime import date, datetime
import pyotp
import json
import os
from kite_trade import KiteApp, get_enctoken
def kite_login():
user_id = 'your id goes here'
password = 'your password goes here'
totp_key = 'your totp key goes here'
# Download Broker Token saved within BrokerToken.json file
try:
access_token_file = os.path.join('Config', 'BrokerToken.json')
with open(access_token_file, 'r') as access_token:
broker_token = json.load(access_token)
except:
print(datetime.now(), " : FabTrader - ERROR : Access Token file is missing. Quitting")
exit(-1)
run_date = date.today().strftime("%Y-%m-%d")
# Check if Access token was generated in the last 24 hrs. If so use that one.
if run_date in broker_token.keys():
encrypted_token = broker_token[run_date]
else:
# Generate fresh Access Token and write to access token file (to be used through the day)
totp = pyotp.TOTP(totp_key).now()
encrypted_token = get_enctoken(
user_id,
password,
totp
)
if encrypted_token is None:
print(datetime.now(), " : FabTrader - ERROR : Bad Broker Encryption token. Quitting")
exit(-1)
# Save token into token file
new_token = {run_date: encrypted_token}
with open(access_token_file, 'w+') as token_file:
json.dump(new_token, token_file)
# set broker handle and access token to the instance
broker_handle = KiteApp(enctoken=encrypted_token)
# Test the broker login with a ping and confirm if its successful
ping = broker_handle.profile()
if ping is None:
print(datetime.now(), " : FabTrader - ERROR : Broker Connection failed. Quitting")
exit(-1)
# Everything looks ok. Login was successful
print("Broker Login Successful")
return broker_handle
if __name__ == "__main__":
kite = kite_login()
if kite is not None:
print(kite.profile())
Key points:
- The token is generated once daily and stored in a JSON file.
- On the next run, your algo reuses the stored token unless a new one is required.
- The script integrates TOTP-based 2FA for secure login.
2. Paid Zerodha Kite API (Official Method)
If you have a paid Kite Connect subscription, the official API login can also be fully automated. This is a cleaner and more reliable method than the free hack.
Here’s a Python implementation:
Key points:
# ------------------------------------------------------------------------------------
# FabTrader Algorithmic Trading Platform
# ------------------------------------------------------------------------------------
# Copyright (c) 2025 FabTrader
#
# LICENSE: PROPRIETARY SOFTWARE
# - This software is the exclusive property of FabTrader.
# - Unauthorized copying, modification, distribution, or use is strictly prohibited.
# - Written permission from the author is required for any use beyond personal,
# non-commercial purposes.
#
# CONTACT:
# - Website: https://fabtrader.in
# - Email: hello@fabtrader.in
#
# Usage: Internal use only. Not for commercial redistribution.
# Permissions and licensing inquiries should be directed to the contact email.
from kiteconnect import KiteConnect
import pyotp
import requests
from urllib.parse import urlparse, parse_qs
import re
def broker_login():
"""
Function enables logging into broker api account.
:return:
Kite Object, in case the login was successful
None, if the login fails
"""
user_id = 'your id goes here'
password = 'your pwd goes here'
totp_key = 'your Totp key goes here'
api_key = 'your api key goes here'
app_secret = 'your app secret goes here'
# Download Broker Login credentials from Broker app config file
session = requests.Session()
login_payload = {
"user_id": user_id,
"password": password,
}
response = session.post("https://kite.zerodha.com/api/login", login_payload)
login_response = response.json()
if login_response['status'] != 'success':
print('Broker Login : Login failed with message : %s', login_response['message'])
return None
totp = pyotp.TOTP(totp_key).now()
twofa_payload = {
"user_id": user_id,
"request_id": login_response["data"]["request_id"],
"twofa_value": totp,
"twofa_type": "totp",
"skip_session": True,
}
response = session.post("https://kite.zerodha.com/api/twofa", twofa_payload)
totp_response = response.json()
if totp_response['status'] != 'success':
print('Broker Login : Login failed with message : %s', totp_response['message'])
return None
kite = KiteConnect(api_key=api_key)
try:
# Extracting query parameters from redirect URL
response = session.get(kite.login_url())
parse_result = urlparse(response.url)
query_parms = parse_qs(parse_result.query)
except Exception as e:
# Extracting query parameters from error message in case of error
pattern = r"request_token=[A-Za-z0-9]+"
# Extracting request token
query_parms = parse_qs(re.findall(pattern, e.__str__())[0])
try:
request_token = query_parms["request_token"][0]
data = kite.generate_session(request_token=request_token, api_secret=app_secret)
kite.set_access_token(data["access_token"])
except Exception as e:
print('Broker Login : Login failed. Check Credentials and try again')
return None
return kite
if __name__ == "__main__":
kite = broker_login()
if kite is not None:
print(kite.profile())
else:
print("Broker Login failed")
- Uses official
kiteconnectlibrary. - Handles login, 2FA (via TOTP), and request token generation automatically.
- Once the access token is set, your algo can place orders, fetch data, and manage positions without manual steps.
Which Method Should You Use?
- Hobby/Testing → Free Token Method works but is unofficial. Use at your own risk.
- Serious/Production Algo Trading → Paid API method is more stable, compliant, and supported by Zerodha.
Final Thoughts
Automating Zerodha Kite API login is one of the first steps toward a truly hands-free trading system. No more waiting for manual logins every morning — just run your strategies and let them trade.
Both the Free Token and Paid API scripts get the job done. Choose based on your trading needs and budget.
If you’re building a serious algorithmic trading platform, I’d strongly recommend the official Paid Kite Connect API, as it ensures reliability and compliance.