Upstox TOTP Python SDKο
A modern, lightweight Python package that simplifies Upstox API authentication by handling TOTP-based login and token generation automatically. With this library, you can securely generate and refresh access tokens required to connect to the Upstox trading platform without manual intervention.
Featuresο
π Automated TOTP Authentication β Generate secure time-based one-time passwords (TOTP) for Upstox login
β‘ Token Management β Fetch, refresh, and store Upstox access tokens with ease
π οΈ Simple API β Minimal, developer-friendly methods for quick integration
π Trading Ready β Instantly plug into Upstox APIs for real-time market data, order placement, and portfolio management
π Pythonic Design β Built with modern async/session handling for robust performance
π― CLI Tool β Command-line interface for quick token generation
π§ Environment Configuration β Auto-configuration from environment variables
π‘ Helpful Error Messages β Clear error messages with troubleshooting guidance
π Secure by Design β Uses secure SecretStr for sensitive data handling
Quick Startο
Installationο
# Add as a dependency to your project
uv add upstox-totp
# Or install with pip
pip install upstox-totp
Basic Usageο
from upstox_totp import UpstoxTOTP
# Initialize (auto-loads from environment variables or .env file)
upx = UpstoxTOTP()
# Generate access token
try:
response = upx.app_token.get_access_token()
if response.success and response.data:
print(f"β
Access Token: {response.data.access_token}")
print(f"π€ User: {response.data.user_name} ({response.data.user_id})")
print(f"π§ Email: {response.data.email}")
print(f"π’ Broker: {response.data.broker}")
# Use the token for Upstox API calls
access_token = response.data.access_token
except Exception as e:
print(f"β Error: {e}")
CLI Usageο
# Check if environment is properly configured
upstox_cli check-env
# Generate access token
upstox_cli generate-token
Documentation Contentsο
User Guide
- Installation Guide
- Quick Start Guide
- Prerequisites
- Step 1: Install the Package
- Step 2: Set Up Your Credentials
- Step 3: Your First Token Generation
- Step 4: Using the CLI Tool
- Step 5: Using the Token with Upstox API
- Step 6: Context Manager Usage
- Common Configuration Patterns
- Error Handling Best Practices
- Next Steps
- Common Issues
- Getting Help
- Congratulations! π
- Configuration Guide
- Advanced Usage Guide
- CLI Reference
Examples & Tutorials
Additional Information
Indices and tablesο
Requirementsο
Python 3.12 or higher
Active Upstox trading account
Upstox Developer App (for client credentials)
TOTP app setup (Google Authenticator, Authy, etc.)
Licenseο
MIT License - see License for details.
Important Notesο
Warning
π Access Token Expiry: Upstox access tokens have a 24-hour expiration time. For production applications, itβs recommended to:
Store tokens securely in a database or cache (Redis, etc.)
Implement automatic token refresh logic
Monitor token expiry and regenerate proactively
Note
This is an unofficial library for Upstox API authentication. Please ensure you comply with Upstoxβs terms of service and API usage guidelines. Use at your own risk.