In the previous Article we have discussed about How to Get An API Key & Secret Key from Angel One SMART API.

In this article we will learn about how to Auto Generate TOTP – Time Based OTP using python for automated login in the Angel One demat account.

Step 1: Enable TOTP from Angel One Smart API 

Go to the URL and Login With Your Angel One Client ID & MPIN or Password 

How to enable TOTP in Angel One Demat Account

Step 2: OTP Verification 

OTP will come to your registered mobile number and email.

Angel One OTP Screen Hemang Dave

Step 3:  TOTP Token & QR Code

After Logging in Successfully you will redirect the OTP Screen where you can see TOTP Token and One QR Code, Please copy this TOTP Token and paste it on the safe place for now.

How to generate TOTP manually using Google Authenticator App

Step 1: For Manually Login Please Download the Google Authenticator App From Google Play Store OR App Store.

Step 2: Open the App and click on the + plus icon from the bottom right corner, you will see 2 options, one for Scan QR code and one for Enter a setup key you can directly scan the QR code, or Enter the TOTP token manually.

Angel One TOTP Using Google Authenticator

Once you scan the QR code Google Authenticator will start generating TOTP, This is the Manual process for generating TOTP from Angel One Demat Account. 

Angel One TOTP Using Google Authenticator

Every time Generating TOTP from the Google Authenticator and entering it in the code is a very painful process, we have one more convenient way to make this process Automated using python

How to Auto Generate TOTP using Python from Angel One Demat Account

Step 1:  Install python Packages: 

First of all We need to install some python packages. For that please run these command on your Terminal 

pyotp: We will use this python library to generate TOTP from the TOTP token.

pip install pyotp

Always Follow Good Practices 
We must follow Good Practice to Save all confidential details like TOTP Token API Key , API Secret, we will save all this details on the file named .env and save all variables in this file.

dotenv:  we will use this library to get environment variables from the .env file which is located in the root folder of the project

pip install dotenv 

Step 2 : Create .env File in your project’s root folder

Create a file named .env in your project root folder, and copy the following code and paste them into the .env file and replace xxx values with the real one.

AngelTtoken = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
AngelAPiKey = "xxxxx"
AngelAPisecret = "xxxxxxxxxxxx"
AngelClientId = "xxxxx"

Naming of keys is totally up to you, you can change these keys names with any other relevant name.

Note: Never share this .env file with anyone if you are using Git for version control make sure this file is added in the gitignore file. 

Step 3: Create file 

Create a file named (you can choose a different name ) and paste the following code into it. 

import os
from pyotp import TOTP
from dotenv import load_dotenv


Here, we have imported some dependencies, we have already discussed pyotp and dotenv libraries, and we will use the OS for getting environment variables with the help of the dotenv library. load_dotenv() function lods environment variables from the .env file. 

Step 4: Get TOTP Token From the .env file 

To get a token or any other variable from the .env file we will use the OS library, paste the following code into file just next where all the libraries are imported.

AngelTotptoken = os.environ.get("AngelTtoken")

os.environ.get() function takes the variable name as an argument and returns the value of the variable.  Here we have passed “AngelTtoken” which we have defined in the .env file 

Step 5 : Auto Generate TOTP

To generate TOTP from the TOTP token we will use following code 

myTotp = TOTP(AngelTotptoken).now()

Your Whole file will look like

import os
from pyotp import TOTP
from dotenv import load_dotenv


AngelTotptoken = os.environ.get("AngelTtoken")

myTotp = TOTP(AngelTotptoken).now()


Step: 6 Match the auto generated TOTP with Google Authenticator TOTP 

It’s time to check whether our auto generated TOTP is matched with Google Authenticator TOTP or not, for this we have to  print myTotp variable, same time we have to  check the fresh TOTP value in the Google Authenticator if they both are same that means we have generated a valid TOTP.

We need this TOTP for further Articles when we will try to getting Historical Data From the Angle One Smart API and Place order Automatically. so Stay tuned.

I hope you will like the Article and it will helps you a lot and you learned something from it, if you like my content please share it with your friends and family because knowledge will increase by sharing.

Post Main Image Credit: Image by storyset