SnapTrade Python SDK
The SnapTrade Python SDK provides a client library to easily interact with the SnapTrade API, enabling applications to connect brokerage accounts for live positions and trading. It abstracts away direct API calls, handling authentication and data formatting. The library is actively maintained with frequent updates, currently at version 11.0.177, and supports Python versions 3.8 and newer.
Common errors
-
ModuleNotFoundError: No module named 'snaptrade.api_client'
cause Attempting to import the client using an outdated import path from an older major version of the SDK.fixChange the import statement to `from snaptrade_client import SnapTrade`. -
snaptrade_client.exceptions.ApiException: (401) Reason: Unauthorized HTTP response headers: {'Content-Type': 'application/json'} HTTP response body: {'error': 'Unable to verify signature sent'}cause This error typically indicates incorrect `CLIENT_ID`, `CONSUMER_KEY`, or `userSecret` used in the request, or an issue with the signature generation (which the SDK handles automatically if credentials are correct).fixVerify that your `SNAPTRADE_CLIENT_ID` and `SNAPTRADE_CONSUMER_KEY` environment variables are correct and match the credentials from your SnapTrade Dashboard. Ensure the `user_secret` (if applicable) is also correct and not compromised. If the issue persists, contact SnapTrade support. -
snaptrade_client.exceptions.ApiException: (429) Reason: Too Many Requests HTTP response headers: {'Content-Type': 'application/json', 'X-RateLimit-Limit': '250', 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': 'X'}cause The application has exceeded the API rate limit (default 250 requests per minute).fixImplement retry logic with exponential backoff. Distribute API calls over time, especially for bulk operations. Consider contacting SnapTrade support for an increased rate limit if your use case demands it.
Warnings
- breaking The import path for the main client class changed significantly in major version 11. Older versions used `from snaptrade.api_client import SnapTradeAPIClient`, while current versions use `from snaptrade_client import SnapTrade`.
- gotcha The `consumerKey` and `userSecret` are highly sensitive. Never hardcode them directly into your application's source code, especially for public-facing deployments. Use environment variables or a secure secret management system.
- gotcha The SnapTrade API implements rate limiting, typically 250 requests per minute per client. Exceeding this limit will result in HTTP 429 'Too Many Requests' errors.
- gotcha There might be confusion with an older, deprecated Python package also named 'snaptrade'. Ensure you are installing and using `snaptrade-python-sdk`, which is the actively maintained SDK.
Install
-
pip install snaptrade-python-sdk
Imports
- SnapTrade
from snaptrade.api_client import SnapTradeAPIClient
from snaptrade_client import SnapTrade
- ApiException
from snaptrade_client import ApiException
Quickstart
import os
from pprint import pprint
from snaptrade_client import SnapTrade, ApiException
CLIENT_ID = os.environ.get('SNAPTRADE_CLIENT_ID', 'YOUR_CLIENT_ID')
CONSUMER_KEY = os.environ.get('SNAPTRADE_CONSUMER_KEY', 'YOUR_CONSUMER_KEY')
if not CLIENT_ID or not CONSUMER_KEY:
print("Please set SNAPTRADE_CLIENT_ID and SNAPTRADE_CONSUMER_KEY environment variables.")
else:
try:
# 1. Initialize the SnapTrade client
snaptrade = SnapTrade(
consumer_key=CONSUMER_KEY,
client_id=CLIENT_ID,
)
# 2. Check API status
api_status_response = snaptrade.api_status.check()
print("API Status:")
pprint(api_status_response)
# 3. Register a new user (or use an existing one)
# In a real application, you'd associate this user_id with your own user system.
# user_id = "unique-user-id-from-your-app"
# register_response = snaptrade.authentication.register_snap_trade_user(user_id=user_id)
# user_secret = register_response.user_secret
# print(f"Registered user: {register_response.user_id}, Secret: {user_secret}")
# Example: List existing users (for demonstration)
list_users_response = snaptrade.authentication.list_snap_trade_users()
if list_users_response and list_users_response.users:
print("\nExisting SnapTrade users:")
for user_info in list_users_response.users:
pprint(user_info)
else:
print("\nNo SnapTrade users found yet. Register one to proceed with connections.")
except ApiException as e:
print(f"An API error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")