Python Algo Trading | Fyers Apis V3 Latest | How To Automate Login Process Using Totp In Python Script.

  • Posted on March 25, 2024
  • Fyers
  • By MmantraTech
  • 1934 Views

Title: Automate Your Fyers Trading Login with Python: A Step-by-Step Guide

When it comes to Algo Trading you really want to automate your login process. You don't want everyday to generate access token manually.  If you have this issue you have landed in right place. With Python scripting, you can automate the login process, saving you time and effort. In this tutorial, we'll walk you through the steps to set up automated login using Python code scripts. Before we dive in, ensure you've completed the pre-steps outlined below:

 

ROUTE GROUPS (1)-AdgztevkC2.png

 

 

 

Pre-Step: Setting Up Your Fyers App

  1. Create an App in your Fyers Dashboard.
  2. Activate your App via the provided URL.
  3. Enable two-factor authentication using an Authenticator App for added security. [ https://support.fyers.in/portal/en/kb/articles/how-to-set-up-time-based-one-time-password-totp-in-fyers ]

Once you've completed these pre-steps, you're ready to automate your Fyers trading login. Let's get started with the tutorial:

Step 1: Setting Up the Environment First, set up a virtual environment for your Python project. Open your command prompt or terminal and execute the following commands:

pip3 install virtualenv
python -m venv myenv
myenv\Scripts\activate

Remember to deactivate the virtual environment once you're done:

deactivate

Step 2: Installing Required Packages Ensure you have the necessary Python packages installed. Use pip to install the following packages:

pip3 install fyers-apiv3
pip3 install requests
pip3 install pyotp
pip3 install parse_qs
pip3 install urllib

Step 3: Importing Modules In your Python script, import the required modules for interacting with the Fyers API:

import json
import requests
import pyotp
from urllib import parse
from fyers_apiv3 import fyersModel
import credentials as cr
import time as tm
from urllib.parse import parse_qs, urlparse

 

Step 4: Automating the Login Process Now, let's break down the login automation process into manageable steps:

  1. Retrieve Request Key: Use the send_login_otp API to retrieve a request key.
  2. Generate TOTP: Generate a Time-Based One-Time Password (TOTP) for authentication.
  3. Verify TOTP: Verify the TOTP and obtain the request key from the verify_otp API.
  4. Verify PIN: Verify your PIN and receive the access token.
  5. Get Auth Code: Obtain the authentication code for your API V2 App from the trade access token.
  6. Get API V2 Access Token: Validate the authentication code to acquire the API V2 access token.

Let's Code Practical

Online Documentation link : https://myapi.fyers.in/docsv3

Python SDK Sample code online : https://pypi.org/project/fyers-apiv3/

 

Create a file called "credentials.py" and paste below code :

APP_ID=""
APP_TYPE=""
SECRET_KEY=""
FY_ID=""
APP_ID_TYPE=""
TOTP_KEY=""
PIN=""
REDIRECT_URI=https='https://myapi.fyers.in/'

Paster below code in your main.py file

from fyers_apiv3 import fyersModel
import credentials as cr
import time as tm
from urllib.parse import parse_qs, urlparse

APP_ID = cr.APP_ID
APP_TYPE = cr.APP_TYPE
SECRET_KEY = cr.SECRET_KEY
client_id = f'{APP_ID}-{APP_TYPE}'

FY_ID = cr.FY_ID
APP_ID_TYPE = cr.APP_ID_TYPE  # 2denotes web login

#You should take care here that  TOTP secret is generated when you have enabled 2Factor TOTP from Fyers account portal

TOTP_KEY = cr.TOTP_KEY
PIN = cr.PIN # User pin for fyers account
REDIRECT_URI = cr.REDIRECT_URI # Redirect url from the APP in fyers dashboard

# API endpoints
BASE_URL = "https://api-t2.fyers.in/vagator/v2"
BASE_URL_2 = "https://api-t1.fyers.in/api/v3"
URL_SEND_LOGIN_OTP = BASE_URL + "/send_login_otp"
URL_VERIFY_TOTP = BASE_URL + "/verify_otp"
URL_VERIFY_PIN = BASE_URL + "/verify_pin"
URL_TOKEN = BASE_URL_2 + "/token"
URL_VALIDATE_AUTH_CODE = BASE_URL_2 + "/validate-authcode"

STEPS Beakage by Code :

Retrieve request_key from send_login_otp API

def send_login_otp(fy_id, app_id):
    try:
        result_string = requests.post(url=URL_SEND_LOGIN_OTP, json={
            "fy_id": fy_id, "app_id": app_id})
        if result_string.status_code != 200:
            return [ERROR, result_string.text]
        result = json.loads(result_string.text)
        request_key = result["request_key"]
        return [SUCCESS, request_key]
    except Exception as e:
        return [ERROR, e]

Generate totp

def generate_totp(secret):
    try:
        generated_totp = pyotp.TOTP(secret).now()
        return [SUCCESS, generated_totp]

    except Exception as e:
        return [ERROR, e]

Verify totp and get request key from verify_otp API

def verify_totp(request_key, totp):
    print("6 digits>>>",totp)
    print("request key>>>",request_key)
    try:
        result_string = requests.post(url=URL_VERIFY_TOTP, json={
            "request_key": request_key, "otp": totp})
        if result_string.status_code != 200:
            return [ERROR, result_string.text]
        result = json.loads(result_string.text)
        request_key = result["request_key"]
        return [SUCCESS, request_key]
    except Exception as e:
        return [ERROR, e]

Verify pin and send back access token

def verify_PIN(request_key, pin):
    try:
        payload = {
            "request_key": request_key,
            "identity_type": "pin",
            "identifier": pin
        }

        result_string = requests.post(url=URL_VERIFY_PIN, json=payload)
        if result_string.status_code != 200:
            return [ERROR, result_string.text]

        result = json.loads(result_string.text)
        access_token = result["data"]["access_token"]

        return [SUCCESS, access_token]

    except Exception as e:
        return [ERROR, e]

Get auth code for API V2 App from trade access token

def token(fy_id, app_id, redirect_uri, app_type, access_token):
    try:
        payload = {
            "fyers_id": fy_id,
            "app_id": app_id,
            "redirect_uri": redirect_uri,
            "appType": app_type,
            "code_challenge": "",
            "state": "sample_state",
            "scope": "",
            "nonce": "",
            "response_type": "code",
            "create_cookie": True
        }
        headers={'Authorization': f'Bearer {access_token}'}

        result_string = requests.post(
            url=URL_TOKEN, json=payload, headers=headers
        )

        if result_string.status_code != 308:
            return [ERROR, result_string.text]

        result = json.loads(result_string.text)
        url = result["Url"]
        auth_code = parse.parse_qs(parse.urlparse(url).query)['auth_code'][0]

        return [SUCCESS, auth_code]

    except Exception as e:
        return [ERROR, e]

Entry point : Calling

def main():

    # Step 1 - Retrieve request_key from send_login_otp API

    session = fyersModel.SessionModel(client_id=client_id, secret_key=SECRET_KEY, redirect_uri=REDIRECT_URI,response_type='code', grant_type='authorization_code')

    urlToActivate = session.generate_authcode()
    print(f'URL to activate APP:  {urlToActivate}')

    send_otp_result = send_login_otp(fy_id=FY_ID, app_id=APP_ID_TYPE)

    if send_otp_result[0] != SUCCESS:
        print(f"send_login_otp msg failure - {send_otp_result[1]}")
        status=False
        sys.exit()
    else:
        print("send_login_otp msg SUCCESS")
        status=False


    # Step 2 - Generate totp
    generate_totp_result = generate_totp(secret=TOTP_KEY)

    if generate_totp_result[0] != SUCCESS:
        print(f"generate_totp msg failure - {generate_totp_result[1]}")
        sys.exit()
    else:
        print("generate_totp msg success")


    # Step 3 - Verify totp and get request key from verify_otp API
    for i in range(1, 3):

        request_key = send_otp_result[1]
        totp = generate_totp_result[1]
        print("otp>>>",totp)
        verify_totp_result = verify_totp(request_key=request_key, totp=totp)
        # print("r==",verify_totp_result)

        if verify_totp_result[0] != SUCCESS:
            print(f"verify_totp_result msg failure - {verify_totp_result[1]}")
            status=False

            tm.sleep(1)
        else:
            print(f"verify_totp_result msg SUCCESS {verify_totp_result}")
            status=False
            break

    if verify_totp_result[0] ==SUCCESS:

        request_key_2 = verify_totp_result[1]

        # Step 4 - Verify pin and send back access token
        ses = requests.Session()
        verify_pin_result = verify_PIN(request_key=request_key_2, pin=PIN)
        if verify_pin_result[0] != SUCCESS:
            print(f"verify_pin_result got failure - {verify_pin_result[1]}")
            sys.exit()
        else:
            print("verify_pin_result got success")


        ses.headers.update({
            'authorization': f"Bearer {verify_pin_result[1]}"
        })

         # Step 5 - Get auth code for API V2 App from trade access token
        token_result = token(
            fy_id=FY_ID, app_id=APP_ID, redirect_uri=REDIRECT_URI, app_type=APP_TYPE,
            access_token=verify_pin_result[1]
        )
        if token_result[0] != SUCCESS:
            print(f"token_result msg failure - {token_result[1]}")
            sys.exit()
        else:
            print("token_result msg success")

        # Step 6 - Get API V2 access token from validating auth code
        auth_code = token_result[1]
        session.set_token(auth_code)
        response = session.generate_token()
        if response['s'] =='ERROR':
            print("\n Cannot Login. Check your credentials thoroughly!")
            status=False
            tm.sleep(10)
            sys.exit()

        access_token = response["access_token"]
        print(access_token)

if __name__ == "__main__":
    main()

Complete Code 

Create a file "main.py" and paste below code :

import json
import requests
import pyotp
from urllib import parse
import sys
from fyers_apiv3 import fyersModel
import credentials as cr
import time as tm
from urllib.parse import parse_qs, urlparse

APP_ID = cr.APP_ID
APP_TYPE = cr.APP_TYPE
SECRET_KEY = cr.SECRET_KEY
client_id = f'{APP_ID}-{APP_TYPE}'

FY_ID = cr.FY_ID
APP_ID_TYPE = cr.APP_ID_TYPE  # 2denotes web login

#You should take care here that  TOTP secret is generated when you have enabled 2Factor TOTP from Fyers account portal

TOTP_KEY = cr.TOTP_KEY
PIN = cr.PIN # User pin for fyers account
REDIRECT_URI = cr.REDIRECT_URI # Redirect url from the APP in fyers dashboard

# API endpoints
BASE_URL = "https://api-t2.fyers.in/vagator/v2"
BASE_URL_2 = "https://api-t1.fyers.in/api/v3"
URL_SEND_LOGIN_OTP = BASE_URL + "/send_login_otp"
URL_VERIFY_TOTP = BASE_URL + "/verify_otp"
URL_VERIFY_PIN = BASE_URL + "/verify_pin"
URL_TOKEN = BASE_URL_2 + "/token"
URL_VALIDATE_AUTH_CODE = BASE_URL_2 + "/validate-authcode"

SUCCESS = 1
ERROR = -1



def send_login_otp(fy_id, app_id):
    try:
        result_string = requests.post(url=URL_SEND_LOGIN_OTP, json={
            "fy_id": fy_id, "app_id": app_id})
        if result_string.status_code != 200:
            return [ERROR, result_string.text]
        result = json.loads(result_string.text)
        request_key = result["request_key"]
        return [SUCCESS, request_key]
    except Exception as e:
        return [ERROR, e]


def verify_totp(request_key, totp):
    print("6 digits>>>",totp)
    print("request key>>>",request_key)
    try:
        result_string = requests.post(url=URL_VERIFY_TOTP, json={
            "request_key": request_key, "otp": totp})
        if result_string.status_code != 200:
            return [ERROR, result_string.text]
        result = json.loads(result_string.text)
        request_key = result["request_key"]
        return [SUCCESS, request_key]
    except Exception as e:
        return [ERROR, e]


def generate_totp(secret):
    try:
        generated_totp = pyotp.TOTP(secret).now()
        return [SUCCESS, generated_totp]

    except Exception as e:
        return [ERROR, e]


def verify_PIN(request_key, pin):
    try:
        payload = {
            "request_key": request_key,
            "identity_type": "pin",
            "identifier": pin
        }

        result_string = requests.post(url=URL_VERIFY_PIN, json=payload)
        if result_string.status_code != 200:
            return [ERROR, result_string.text]

        result = json.loads(result_string.text)
        access_token = result["data"]["access_token"]

        return [SUCCESS, access_token]

    except Exception as e:
        return [ERROR, e]


def token(fy_id, app_id, redirect_uri, app_type, access_token):
    try:
        payload = {
            "fyers_id": fy_id,
            "app_id": app_id,
            "redirect_uri": redirect_uri,
            "appType": app_type,
            "code_challenge": "",
            "state": "sample_state",
            "scope": "",
            "nonce": "",
            "response_type": "code",
            "create_cookie": True
        }
        headers={'Authorization': f'Bearer {access_token}'}

        result_string = requests.post(
            url=URL_TOKEN, json=payload, headers=headers
        )

        if result_string.status_code != 308:
            return [ERROR, result_string.text]

        result = json.loads(result_string.text)
        url = result["Url"]
        auth_code = parse.parse_qs(parse.urlparse(url).query)['auth_code'][0]

        return [SUCCESS, auth_code]

    except Exception as e:
        return [ERROR, e]

def main():

    # Step 1 - Retrieve request_key from send_login_otp API

    session = fyersModel.SessionModel(client_id=client_id, secret_key=SECRET_KEY, redirect_uri=REDIRECT_URI,response_type='code', grant_type='authorization_code')

    urlToActivate = session.generate_authcode()
    print(f'URL to activate APP:  {urlToActivate}')

    send_otp_result = send_login_otp(fy_id=FY_ID, app_id=APP_ID_TYPE)

    if send_otp_result[0] != SUCCESS:
        print(f"send_login_otp msg failure - {send_otp_result[1]}")
        status=False
        sys.exit()
    else:
        print("send_login_otp msg SUCCESS")
        status=False


    # Step 2 - Generate totp
    generate_totp_result = generate_totp(secret=TOTP_KEY)

    if generate_totp_result[0] != SUCCESS:
        print(f"generate_totp msg failure - {generate_totp_result[1]}")
        sys.exit()
    else:
        print("generate_totp msg success")


    # Step 3 - Verify totp and get request key from verify_otp API
    for i in range(1, 3):

        request_key = send_otp_result[1]
        totp = generate_totp_result[1]
        print("otp>>>",totp)
        verify_totp_result = verify_totp(request_key=request_key, totp=totp)
        # print("r==",verify_totp_result)

        if verify_totp_result[0] != SUCCESS:
            print(f"verify_totp_result msg failure - {verify_totp_result[1]}")
            status=False

            tm.sleep(1)
        else:
            print(f"verify_totp_result msg SUCCESS {verify_totp_result}")
            status=False
            break

    if verify_totp_result[0] ==SUCCESS:

        request_key_2 = verify_totp_result[1]

        # Step 4 - Verify pin and send back access token
        ses = requests.Session()
        verify_pin_result = verify_PIN(request_key=request_key_2, pin=PIN)
        if verify_pin_result[0] != SUCCESS:
            print(f"verify_pin_result got failure - {verify_pin_result[1]}")
            sys.exit()
        else:
            print("verify_pin_result got success")


        ses.headers.update({
            'authorization': f"Bearer {verify_pin_result[1]}"
        })

         # Step 5 - Get auth code for API V2 App from trade access token
        token_result = token(
            fy_id=FY_ID, app_id=APP_ID, redirect_uri=REDIRECT_URI, app_type=APP_TYPE,
            access_token=verify_pin_result[1]
        )
        if token_result[0] != SUCCESS:
            print(f"token_result msg failure - {token_result[1]}")
            sys.exit()
        else:
            print("token_result msg success")

        # Step 6 - Get API V2 access token from validating auth code
        auth_code = token_result[1]
        session.set_token(auth_code)
        response = session.generate_token()
        if response['s'] =='ERROR':
            print("\n Cannot Login. Check your credentials thoroughly!")
            status=False
            tm.sleep(10)
            sys.exit()

        access_token = response["access_token"]
        print(access_token)

if __name__ == "__main__":
    main()

 

 

By following these steps and executing the Python script, you'll be able to automate the login process for your Fyers trading account efficiently.

That's it! You've successfully automated your Fyers trading login using Python. With this automation in place, you can focus more on trading strategies and less on manual login procedures. Happy trading!

 

 

Author
No Image
Admin
MmantraTech

Mmantra Tech is a online platform that provides knowledge (in the form of blog and articles) into a wide range of subjects .

Write a Response