To autogenerate TOTP in Python for AngelOne, enable TOTP on the SmartAPI portal, copy the secret token shown below the QR code, and pass it to pyotp.TOTP(token).now(). PyOTP then produces the same 6 digit code as Google Authenticator, which your script can use to log in automatically without typing an OTP every morning.
In this tutorial, we are going to autogenerate time-based TOTP using python for Automated Login purposes. TOTP-based login verification is a standard now most brokers across the country had adopted as Two Factor Authentication. In this tutorial used AngelOne Trading Account for the TOTP generation process. Broker to Broker the TOTP generation procedure might vary.
- TOTP is a time-based one-time password; brokers use it as the second factor for login.
- The secret token from the AngelOne enable-TOTP page is what both Google Authenticator and PyOTP use.
- Standard TOTP codes change every 30 seconds (RFC 6238 and PyOTP default).
- Keep the TOTP secret, MPIN and API key out of your code and never share them.
- From 1 April 2026, AngelOne accepts SmartAPI orders only from your registered static IP, so automated login is only half of a working algo setup.
What is 2-factor authentication (2-FA)?
The 2FA or 2-factor authentication is a user authentication process where brokers offer to traders/investors any two of the following three types of authentication mechanisms to users for logging in to their online trading account-
- Knowledge factor (i.e. something that only the user knows) e.g. PIN, password, etc.
- Possession factor (i.e. something that only the user has) e.g. OTP, security token, authenticator apps on smartphones, etc.
- Biometrics (i.e. biological identity markers of the person) e.g. fingerprint, face id, pattern, etc.
Manual TOTP Generation Process using Google Authenticator
Step 1 – Visit smartapi.angelbroking.com/enable-totp
Step 2 – Enter your Angel One client id and trading terminal password or MPIN
Step 3 – Enter OTP sent to the Registered email & mobile. Once OTP is entered, you will see a QR code on the screen and a token number on the screen.

Step 4 – Open the Google Authenticator App. Install it from Android Playstore or Apple Store
Step 5 – Scan the QR code generated from our site on your authenticator app
Google authenticator now generates TOTP (Time-based OTP). Note: the original version of this post said the code is valid for 60 seconds. Standard TOTP, as defined in RFC 6238 and used by Google Authenticator and PyOTP, changes every 30 seconds. Use this for manual TOTP verification.

AngelOne’s SmartAPI portal is also available at smartapi.angelone.in after the Angel Broking rebrand. If the old link redirects, follow the same steps on the new domain.
How to Generate TOTP using Python Automatically?
PyOTP is a Python library for generating and verifying TOTP-based login verification. It can be used to implement two-factor (2FA) or multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in.
Install pyOTP library using the PIP command
pip install pyOTP
Python Code for TOTP generation
import pyotp
import time
token = 'enter your QR code token'
totp = pyotp.TOTP(token).now()
print("Totp is : "+totp)
Output:
Totp is : 979337
The generated TOTP should be exactly matching with Google Authenticator
Updated version (2026)
The same logic with two small improvements: the secret is read from an environment variable instead of being typed into the script, and spaces are stripped in case you copied the token with spaces. The package name is case insensitive, so pip install pyotp and pip install pyOTP both work (PyOTP documentation).
import os
import pyotp
# Store the TOTP secret (the token shown below the QR code) outside your code,
# for example in an environment variable or a .env file that is not shared.
token = os.environ["ANGEL_TOTP_SECRET"].replace(" ", "")
totp = pyotp.TOTP(token) # 6 digits, 30 second interval (TOTP defaults)
code = totp.now()
print("TOTP is :", code)
# Optional check: should print True within the same 30 second window
print("Valid now:", totp.verify(code))
Autogenerate TOTP in Python for AngelOne login
In a real script you pass the generated TOTP straight into the SmartAPI login call. This is the same pattern used in my SmartAPI Python tutorial for realtime quotes:
import os
import pyotp
from SmartApi import SmartConnect
api_key = os.environ["ANGEL_API_KEY"]
client_id = os.environ["ANGEL_CLIENT_ID"]
mpin = os.environ["ANGEL_MPIN"]
totp_key = os.environ["ANGEL_TOTP_SECRET"]
obj = SmartConnect(api_key=api_key)
session = obj.generateSession(client_id, mpin, pyotp.TOTP(totp_key).now())
print(session["status"], session["message"])
Generate the TOTP immediately before the login call. If you create it, wait, and then log in, the code may already have rolled over to the next 30 second window.
Troubleshooting: invalid TOTP errors
When you autogenerate TOTP in Python and the broker still rejects the login, the cause is almost always one of these:
- System clock is wrong. TOTP depends on time. Enable automatic time sync (NTP) on your PC or server; a clock that is off by more than a few seconds gives codes the broker rejects.
- Wrong secret. Use the token text displayed below the QR code, not your client ID, API key or password.
- Spaces or lower case in the secret. The secret is base32; remove spaces when you paste it.
- TOTP re-enabled. If you disable and enable TOTP again, a new secret is issued and the old one stops working in both the app and your script.
- Code reused. Generate a fresh code for each login attempt rather than reusing one from a previous run.
Security: keeping your TOTP secret safe
Anyone who has your TOTP secret together with your client ID and MPIN can log in to your account. Treat the secret like a password: store it in environment variables or a secrets manager, keep it out of Git repositories and screenshots, and restrict access to the machine that runs your automation.
Regulation has also tightened around API trading. AngelOne states that from 1 April 2026 API orders are accepted only if they originate from your registered primary static IP, and that a static IP is mandatory for client-generated algos (AngelOne SmartAPI changes from April 2026). Plan your hosting accordingly if your automated login is part of an order-placing system.
If you want a broker-agnostic setup that handles login and order routing for many Indian brokers, have a look at OpenAlgo, the open-source algo trading framework. For live data after login, see the WebSocket tutorial for traders and Python developers.
Frequently Asked Questions
How long is an AngelOne TOTP valid?
Standard TOTP codes, including those from Google Authenticator and PyOTP, change every 30 seconds.
Is it safe to automate TOTP generation?
It is as safe as the place where you store the secret. Keep the secret in environment variables or a secrets manager on a machine only you control, and never commit it to code repositories.
Why does PyOTP generate a different code from Google Authenticator?
Usually because the computer clock is out of sync or the secret was copied incorrectly. Sync the system time and paste the token again without spaces.
Can I use the same method for other brokers?
Yes, if the broker gives you the TOTP secret key during setup. The PyOTP code is the same; only the enable-TOTP steps and the login API differ by broker.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

