├── .env ├── .gitignore ├── LICENSE ├── README.md ├── call_api.py ├── create_access_token.py └── requirements.txt /.env: -------------------------------------------------------------------------------- 1 | USERNAME=YOUR_USERNAME 2 | PASSWORD=YOUR_PASWORD 3 | PANCARD=YOUR_PANCARD 4 | APPID=YOUR_APPID 5 | SECRETID=YOUR_SECRETID 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | bin/ 28 | include/ 29 | 30 | 31 | 32 | # PyInstaller 33 | # Usually these files are written by a python script from a template 34 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 35 | *.manifest 36 | *.spec 37 | 38 | # Installer logs 39 | pip-log.txt 40 | pip-delete-this-directory.txt 41 | 42 | # Unit test / coverage reports 43 | htmlcov/ 44 | .tox/ 45 | .coverage 46 | .coverage.* 47 | .cache 48 | nosetests.xml 49 | coverage.xml 50 | *.cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | 63 | # Flask stuff: 64 | instance/ 65 | .webassets-cache 66 | 67 | # Scrapy stuff: 68 | .scrapy 69 | 70 | # Sphinx documentation 71 | docs/_build/ 72 | 73 | # PyBuilder 74 | target/ 75 | 76 | # Jupyter Notebook 77 | .ipynb_checkpoints 78 | 79 | # pyenv 80 | .python-version 81 | 82 | # celery beat schedule file 83 | celerybeat-schedule 84 | 85 | # SageMath parsed files 86 | *.sage.py 87 | 88 | # Environments 89 | .venv 90 | env/ 91 | venv/ 92 | ENV/ 93 | env.bak/ 94 | venv.bak/ 95 | 96 | # Spyder project settings 97 | .spyderproject 98 | .spyproject 99 | 100 | # Rope project settings 101 | .ropeproject 102 | 103 | # mkdocs documentation 104 | /site 105 | 106 | # mypy 107 | .mypy_cache/ 108 | 109 | ##my files 110 | check.py 111 | *.pdf 112 | *.pickle 113 | 114 | pyvenv.cfg 115 | .DS_Store 116 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | MIT License 3 | 4 | Copyright (c) 2020 Udit Vashisht 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SaralGyaan - Automatically Generate Access Token for your FYERS API 2 | 3 | A Python Script to automatically generate Access Token for your [FYERS API](https://fyers.in?id=RP0015) 4 | 5 | ## Pre-requisites 6 | * An account with FYERS. If you don't have an account, you can open the same using this [link](https://open-an-account.fyers.in/?id=RP0015) 7 | * Chromium Driver (Chrome) or Gecko Driver (Mozilla) for Selenium. You can download the [Chromium Driver](https://chromedriver.chromium.org/downloads) or [Gecko Driver](https://github.com/mozilla/geckodriver/releases) depending upon your operating System. 8 | * The Chromium Driver or the Gecko Driver should be in the directory, which has been added to the PATHs of your System. 9 | 10 | ## Requirements 11 | * Python 3 12 | * fyers_api 13 | * selenium 14 | * python-decouple 15 | 16 | You can use the following command to install the necessary modules 17 | ``` 18 | pip install -r requirements.txt 19 | ``` 20 | 21 | ## Installation 22 | Either download the directory or clone it using 23 | ``` 24 | git clone https://github.com/uditvashisht/Generate_Access_Token_FYERS_API.git 25 | ``` 26 | ``` 27 | cd Generate_Access_Token_FYERS_API 28 | python3 -m venv . #You can use python or python3 or python3.8 depending on your system 29 | source bin/activate 30 | pip install -r requirements.txt 31 | ``` 32 | ## Usage 33 | Change the .env file and add your details in it 34 | ``` 35 | USERNAME=YOUR_USERNAME 36 | PASSWORD=YOUR_PASWORD 37 | PANCARD=YOUR_PANCARD 38 | APPID=YOUR_APPID 39 | SECRETID=YOUR_SECRETID 40 | ``` 41 | You can generate the Access Token in your system by running 42 | ``` 43 | python create_access_token.py 44 | ``` 45 | 46 | And later on call the API using 47 | ``` 48 | python call_api.py 49 | ``` 50 | ## Usage Guide 51 | You can also check the detailed [video tutorial]() on Youtube. 52 | 53 | ## License 54 | 55 | © 2020 Udit Vashisht 56 | This repository is licensed under the MIT license. See LICENSE for details. 57 | -------------------------------------------------------------------------------- /call_api.py: -------------------------------------------------------------------------------- 1 | # call_api.py 2 | 3 | # Necessary Imports 4 | from fyers_api import fyersModel 5 | import pickle 6 | 7 | # Load the token from Pickle file generated by create_access_token.py file 8 | with open('fyers_token.pickle', 'rb') as f: 9 | token = pickle.load(f) 10 | 11 | is_async = False # (By default False, Change to True for asnyc API calls.) 12 | 13 | # Create Model 14 | fyers = fyersModel.FyersModel(is_async) 15 | 16 | # Check your Profile 17 | print (fyers.get_profile(token=token)) 18 | 19 | # Check Funds 20 | print(fyers.funds(token=token)) 21 | 22 | # Check Holdings 23 | print(fyers.holdings(token=token)) 24 | -------------------------------------------------------------------------------- /create_access_token.py: -------------------------------------------------------------------------------- 1 | # create_access_token.py 2 | 3 | # Necessary Imports 4 | from fyers_api import accessToken 5 | # from fyers_api import fyersModel 6 | from selenium import webdriver 7 | import sys 8 | from decouple import config 9 | import time 10 | import re 11 | import pickle 12 | 13 | # If you are using Firefox 14 | driver = webdriver.Firefox() 15 | # If you are using Chrome 16 | # driver = webdriver.Chrome() 17 | 18 | # Load all the credentials from .env file 19 | 20 | USERNAME = config('USERNAME') 21 | PASSWORD = config('PASSWORD') 22 | PANCARD = config('PANCARD') 23 | APPID = config('APPID') 24 | SECRETID = config('SECRETID') 25 | 26 | 27 | def generate_token_url(app_id, secret_id): 28 | """ This function will generate the url which contains the Access Token 29 | 30 | Parameters: 31 | ----------- 32 | app_id: string 33 | App Id is generated when we create an app in Fyers API. Saved in .env file as APPID 34 | secret_id: string 35 | Secret Id is generated when we create an app in Fyers API. Saved in .env file as SECRETID 36 | 37 | Returns: 38 | -------- 39 | url_with_token: string 40 | It returns the url which contains the token of the kind 41 | https://127.0.0.1?access_token=gAAAAABc3Sh9QpE5mNx2mSz6vvvT29SAsELqkfbKQKa2977zHw3NdPBhe6jAZCBumHvYUum87j53-AzMEPXMjQw31wkRviZ1TdM5OimgTYWEEorWDmWuHnY=&user_id=FYXXXX 42 | """ 43 | 44 | app_session = accessToken.SessionModel(app_id, secret_id) 45 | response = app_session.auth() 46 | # Check if we gets the response 47 | if response["code"] != 200: 48 | sys.exit() 49 | print('Error- Response Code != 200') 50 | # Get Authorization Code 51 | auth_code = response['data']['authorization_code'] 52 | app_session.set_token(auth_code) 53 | # Get URL with the Authorization Code 54 | generate_token_url = app_session.generate_token() 55 | # Open the URL in browser 56 | driver.get(generate_token_url) 57 | # Get credentials elements from the html 58 | user_name = driver.find_element_by_id('fyers_id') 59 | password = driver.find_element_by_id('password') 60 | pan_card = driver.find_element_by_id('pancard') 61 | submit_button = driver.find_element_by_id('btn_id') 62 | # Fill in the credentials 63 | user_name.send_keys(USERNAME) 64 | password.send_keys(PASSWORD) 65 | pan_card.send_keys(PANCARD) 66 | submit_button.click() 67 | # Wait for a while so that the url changes 68 | time.sleep(30) 69 | # Get the current URL (which contains access token) 70 | url_with_token = driver.current_url 71 | driver.quit() # Close the browser 72 | return url_with_token 73 | 74 | 75 | def extract_token(full_url): 76 | """ This function extracts the Access Token from the complete url returned by generate_token_url() function using regex. 77 | 78 | Parameters: 79 | ----------- 80 | full_url: str 81 | It is the complete url returned by generate_token_url 82 | https://127.0.0.1?access_token=gAAAAABc3Sh9QpE5mNx2mSz6vvvT29SAsELqkfbKQKa2977zHw3NdPBhe6jAZCBumHvYUum87j53-AzMEPXMjQw31wkRviZ1TdM5OimgTYWEEorWDmWuHnY=&user_id=FYXXXX 83 | 84 | Returns: 85 | access_token : sting > pickle 86 | It returns the access token in str format and later on saves it as a pickle in a file called fyers_token.pickle 87 | This access token is valid through 7-8 AM of the next day. 88 | """ 89 | access_token = re.search(r'(?<=https://127.0.0.1/\?access_token=).*?(?=user_id=RP0015)', full_url).group(0) 90 | if access_token: 91 | with open('fyers_token.pickle', 'wb') as f: 92 | pickle.dump(access_token, f) 93 | else: 94 | print("No token generated") 95 | 96 | 97 | def main(): 98 | full_url = generate_token_url(APPID, SECRETID) 99 | extract_token(full_url) 100 | 101 | 102 | if __name__ == '__main__': 103 | main() 104 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2020.6.20 2 | chardet==3.0.4 3 | fyers-api==1.0.9 4 | idna==2.10 5 | python-decouple==3.3 6 | requests==2.24.0 7 | selenium==3.141.0 8 | tornado==6.0.4 9 | urllib3==1.25.10 10 | --------------------------------------------------------------------------------