Automated Fyers broker access token generation with encrypted storage
Project description
fyers-auto-auth
Automated Fyers broker access token generation with encrypted storage.
Handles the complete TOTP-based login flow, generates access tokens, and caches them encrypted on disk — refreshing automatically each day. Built for use in automated trading systems.
Features
- Fully automated — no manual browser login needed (after one-time setup)
- Encrypted storage — access tokens are encrypted at rest using Fernet symmetric encryption
- Smart caching — tokens are cached in memory and on disk; a new token is fetched only when the cached one is stale
- Configurable paths — choose where to store your encrypted tokens and encryption keys
- CLI tools — generate encryption keys and perform first-time app setup from the terminal
- Retry logic — automatic retries with jitter on transient failures
Installation
pip install fyers-auto-auth
Prerequisites
You need a Fyers API app. If you don't have one:
- Go to Fyers API Dashboard and create a new app.
- Note down your Client ID, Secret Key, and Redirect URI.
- Enable TOTP on your Fyers account and save the TOTP secret key.
Quick Start
1. First-Time App Setup (one-time only)
When you create a new Fyers API app, you must authorize it once in a browser:
fyers-auto-auth setup-app --client-id "XXXXXXXXXX-100" --secret-key "your_secret_key"
This opens a URL in your browser — log in and grant permissions. This is a one-time step.
Or programmatically:
from fyers_auto_auth import setup_app
setup_app(client_id="XXXXXXXXXX-100", secret_key="your_secret_key")
2. Generate an Encryption Key
fyers-auto-auth generate-key
# Key saved to: ~/.fyers_auto_auth/fernet.key
Or with a custom path:
fyers-auto-auth generate-key --output /path/to/my_key.key
Or programmatically:
from fyers_auto_auth import generate_fernet_key
key = generate_fernet_key(save_to="~/.fyers_auto_auth/fernet.key")
3. Get Access Tokens
from fyers_auto_auth import FyersAuth, load_fernet_key
auth = FyersAuth(
client_id="XXXXXXXXXX-100",
secret_key="your_secret_key",
username="your_fyers_id",
totp_key="YOUR_TOTP_BASE32_KEY",
pin="1234",
encryption_key=load_fernet_key(),
)
# Get token — cached, auto-refreshes daily
access_token = auth.get_token()
# Or use the shorthand
access_token = auth()
4. Use with Fyers API
from fyers_apiv3 import fyersModel
from fyers_auto_auth import FyersAuth, load_fernet_key
auth = FyersAuth(
client_id="XXXXXXXXXX-100",
secret_key="your_secret_key",
username="your_fyers_id",
totp_key="YOUR_TOTP_BASE32_KEY",
pin="1234",
encryption_key=load_fernet_key(),
)
fyers = fyersModel.FyersModel(
client_id="XXXXXXXXXX-100",
token=auth.get_token(),
is_async=False,
log_path="",
)
print(fyers.get_profile())
Configuration
Token File Location
By default, encrypted tokens are stored at ~/.fyers_auto_auth/tokens.json.
You can customize this:
# Option 1: Pass directly
auth = FyersAuth(..., token_file="/path/to/my_tokens.json")
# Option 2: Environment variable
# export FYERS_TOKEN_FILE=/path/to/my_tokens.json
auth = FyersAuth(...) # picks up from env automatically
Resolution order: explicit argument → FYERS_TOKEN_FILE env var → default path.
Encryption Key Location
The load_fernet_key() helper looks for the key in this order:
- Explicit
pathargument:load_fernet_key("/path/to/key.key") FYERS_FERNET_KEYenv var (the raw key value)FYERS_FERNET_KEY_FILEenv var (path to a key file)- Default:
~/.fyers_auto_auth/fernet.key
Environment Variables
| Variable | Description |
|---|---|
FYERS_TOKEN_FILE |
Path to the encrypted token file |
FYERS_FERNET_KEY |
Fernet key value (raw) |
FYERS_FERNET_KEY_FILE |
Path to a file containing the Fernet key |
CLI Reference
fyers-auto-auth generate-key [--output PATH]
Generate a new Fernet encryption key.
fyers-auto-auth setup-app --client-id ID --secret-key KEY [--redirect-uri URI] [--no-browser]
First-time Fyers API app authorization.
API Reference
FyersAuth(client_id, secret_key, username, totp_key, pin, encryption_key, token_file=None, redirect_uri=None)
Main class for automated token management.
get_token()→str— Get a valid access token (auto-refreshes if stale).auth()→str— Shorthand forget_token().
generate_fernet_key(save_to=None) → bytes
Generate a new Fernet encryption key. Optionally save to a file.
load_fernet_key(path=None) → bytes
Load a Fernet key from file or environment variable.
setup_app(client_id, secret_key, redirect_uri=None, open_browser=True) → str
Generate and display the first-time authorization URL.
How It Works
get_token() called
│
├─ Check in-memory cache → return if today's token
│
├─ Check encrypted file on disk → decrypt & return if today's token
│
└─ Run full login flow:
1. Send login OTP request
2. Verify OTP using TOTP (generated from your secret key)
3. Verify PIN
4. Get authorization code
5. Exchange auth code for access token
6. Encrypt & save to disk
7. Cache in memory & return
Security Notes
- Never commit your
.envfiles,.keyfiles, ortokens.jsonto version control. - Add these to your
.gitignore:*.env *.key tokens.json - The Fernet encryption key is the master secret — treat it like a password.
- Tokens are valid for one trading day only.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fyers_auto_auth-0.1.0.tar.gz.
File metadata
- Download URL: fyers_auto_auth-0.1.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
053c8421e71150c6023eec8b5ac465bc7c1188f98230c17b5403b6e985803a9c
|
|
| MD5 |
13d5026d44300d18e639cb275bc5d179
|
|
| BLAKE2b-256 |
717d99acea8bb05ba7999fd40299c3fda954edd4c601c671d0db3b59c468c530
|
Provenance
The following attestation bundles were made for fyers_auto_auth-0.1.0.tar.gz:
Publisher:
pypi-package-publish.yml on KoushikEng/fyers-auto-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fyers_auto_auth-0.1.0.tar.gz -
Subject digest:
053c8421e71150c6023eec8b5ac465bc7c1188f98230c17b5403b6e985803a9c - Sigstore transparency entry: 1581951079
- Sigstore integration time:
-
Permalink:
KoushikEng/fyers-auto-auth@ffde8da510fd14f0ff746cd251a04a2b548a7ef0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KoushikEng
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package-publish.yml@ffde8da510fd14f0ff746cd251a04a2b548a7ef0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fyers_auto_auth-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fyers_auto_auth-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9c8a0a0d2f68a54c42eb0a9a49181a7fe5771baf79c3257ce1fe5303c4586e2
|
|
| MD5 |
090edbbb609bb42f3eef2284adeaec0e
|
|
| BLAKE2b-256 |
d8e6311097c0603497497d1f3dd66b3d672bfd55ff052b7ba68eef47201155cd
|
Provenance
The following attestation bundles were made for fyers_auto_auth-0.1.0-py3-none-any.whl:
Publisher:
pypi-package-publish.yml on KoushikEng/fyers-auto-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fyers_auto_auth-0.1.0-py3-none-any.whl -
Subject digest:
a9c8a0a0d2f68a54c42eb0a9a49181a7fe5771baf79c3257ce1fe5303c4586e2 - Sigstore transparency entry: 1581951214
- Sigstore integration time:
-
Permalink:
KoushikEng/fyers-auto-auth@ffde8da510fd14f0ff746cd251a04a2b548a7ef0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KoushikEng
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package-publish.yml@ffde8da510fd14f0ff746cd251a04a2b548a7ef0 -
Trigger Event:
push
-
Statement type: