Guide to secure your SmartAPI Account with two factor authentication
-
How to enable Time-based One Time Password (TOTP) ? (one-time setup)
Step 1 - Visit smartapi.angelbroking.com/enable-totp
Step 2 - Enter your Angel One client id and trading terminal password or PIN
Step 3 - Enter OTP sent to Registered email & mobile. Once OTP is entered, you will see a QR code on the screen.
Step 4 - Open the authenticator app of your choice
Step 5 - Scan the QR code generated from our site on your authenticator appHow to login to SmartAPI using password and TOTP?
Once your authenticator app scans the QR code, it will automatically generate an authentication code (called as TOTP) every 30 seconds. You are required to pass the TOTP code in our existing loginbypassword API endpoint (/rest/auth/angelbroking/user/v1/loginByPassword) by sending a new "totp" parameter via the request body.
The below request parameters illustrates how to send the client code, password and totp to authenticate and get the tokens
{
"clientcode":"your_client_code",
"password":"your_password",
"totp":"enter_the_code_displayed_on_your_authenticator_app"
}Is the new login flow supported via SDKs?
Yes. To support the login authentication flow using TOTP we have updated and released libraries for Python and Java. Same can be referred here -
Python - https://github.com/angel-one/smartapi-python
Totp changes have been deployed in the latest python release 1.3.0
Please use the following command to upgrade to the latest python release
pip install smartapi-python --upgradeJava - https://github.com/angel-one/smartapi-java
- The latest jar version v2.0.0 containing login with totp changes has been released here - https://github.com/angel-one/smartapi-java/tree/main/dist
- Sample Java Code - https://github.com/angel-one/smartapi-java/blob/main/src/main/java/com/angelbroking/smartapi/sample/LoginWithTOTPSample.java
GOLANG - https://github.com/angel-one/smartapigo
What is 2FA?
Two-factor authentication (2FA) is a method of authentication using the following two factors:
- Knowledge factor i.e. something only the user knows like. password, PIN etc
- Possession factor like OTP, authenticator apps etc
Why use it now?
The exchange vide circular ref no. NSE/COMP/52623 (https://www1.nseindia.com/content/circulars/COMP52623.pdf) has now mandated 2FA for login purposes from September 30, 2022
Which authenticator app to use?
Tested and recommended 2FA authentication applications:
- Google Authenticator
- Microsoft Authenticator
-
@admin How will I be able to automate the login flow using this method on the server?
-
@admin https://smartapi.angelbroking.com/enable-totp not opening
-
-
Nse circular does not say that you have to use TOTP. This process will make things complicated and orders may fail. Because before placing the orders, the bot/server will have to check if the TOTP is valid or not and then wait/refresh for the latest TOTP. And with 30 second hash
-
Simple solution is to have a one time otp for logging everyday.
-
-
@admin Should User Add OTP every day? if yes then I think it is the End of Trading using APIs because no one wants to do the Same Process Again and Again.
I think SEBI Circular is not Telling the OTP required for API Trading. it is only required to Login Account on a Web or Mobile App or Broker.
-
@admin One Solution is to Provide the TOTP Via API by Sending the Username & Password of User.
-
@gautamnaik1994 I Agree, need something to Automate the Process.
-
How to enable TOTP
-
-
@gautamnaik1994 If you are using python, you can use totp module to generate totp automatically
-
Using smart api-Angel Broker plugin, include following js file
<script src="https://smartapi.angelbroking.com/common/v1.js"></script>
then passing api key; <smartapi-login href="#" data-smartapi="<api-key>">Login</smartapi-login>
How to solve with this integration?
-
@admin not open link
-
@labeebta what should be the Secret Key to Generate TOTP?
-
@admin It already 13:00 and the link still does not work. How can I access the API ?
-
The link http://smartapi.angelbroking.com/enable-totp is not working @admin @administrators . Please help. already last 2 days your APIs never worked with all changes you did without proper testing.
Resolve this please.
-
-
@admin The site is working but OTP is not working or sent to the email.
-
Hi folks
If anyone looking for automating totp part , you can use the following python sample for the same. The secret here comes after validating on enable-totp url .
from smartapi import SmartConnect # or from smartapi.smartConnect import SmartConnect
import pyotpimport smartapi.smartExceptions(for smartExceptions)
create object of call
obj = SmartConnect(api_key="your api key here ")
login api call
totp = pyotp.TOTP(s='secret key in qr uri after qr generation')
attempts = 5
while attempts > 0:
attempts = attempts-1
data = obj.generateSession("Your client id ", "your password ", totp.now())
if not data['message'].contains('Invalid totp'):
break
time.sleep(2) ###rate limiting might block if we try immediatelyrefreshToken = data['data']['refreshToken']
-
@gautamnaik1994 I have provided a sample code below. You can give it a shot. Worked for me .
-