Automated Fyers API login using Python: A Step by Step Guide
Published in
1 min readJan 8, 2022
This article assumes that you already have following things ready:
- A fyers account
- Created an app using the website API.
- Installed fyers python package
Step 1 : Create a JSON file like “appid_key.json” and store the confidential information in that like following:
Step 2: Make necessary imports.
from fyers_api import fyersModel
from fyers_api import accessToken
from urllib.parse import urlparse, parse_qs
Step 3: Retrive credentials form json file.
Make sure your redirect_url is same as the one created in your Fyers application
Step 4: Creation of Session
Log into Fyers pages using the session created above.
It requires your authentication data. After that, we have a redirected URL that contains the access_token which is used can be used for different tasks that you wish to accomplish.
Step 5 : Setting the auth_code to session and generating access token.
session.set_token(auth_code)
response = session.generate_token()
print(response)access_token = response["access_token"]log_path = "/home/tushar/Documents/Logs_now/" # Your Logs folder
fyers = fyersModel.FyersModel(client_id=client_id, token=access_token,log_path= log_path)is_async = True # Set to true in you need async API calls
Finally if you have reached till here and if you run :
print(fyers.get_profile())
# {'s': 'ok', 'code': 200, 'message': '', 'data': .....
And gets your details as output . Congratulations 🙌🎉 you have successfully logged in to Fyers API.