| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
The TradeStation API Python Wrapper uses OAuth 2.0 for authentication. This document explains how to authenticate with the TradeStation API using this library.
To use this library, you need to obtain TradeStation API credentials:
The library supports several methods for providing authentication credentials. The client prioritizes credentials passed directly, then looks at environment variables.
You can set credentials as environment variables:
# Mandatory
CLIENT_ID=your_client_id
REFRESH_TOKEN=your_refresh_token # Note: Only CLIENT_ID is automatically read from env during initialization.
ENVIRONMENT=Live # or Simulation (Used for API endpoints, not the auth endpoint)You can use a .env file for this purpose. The library includes a .env.sample file you can copy and modify.
You can pass configuration directly when creating the client:
from src.client.tradestation_client import TradeStationClient
client = TradeStationClient({
"client_id": "your_client_id",
"refresh_token": "your_refresh_token",
"environment": "Live" # or "Simulation"
})This is the recommended way to provide the refresh_token.
You can provide some parameters directly to the constructor:
from src.client.tradestation_client import TradeStationClient
client = TradeStationClient(
refresh_token="your_refresh_token",
environment="Live"
)When using this method, the client will still look for CLIENT_ID in the environment variables if not provided directly or in a config dictionary. The refresh_token must be provided either directly or via the config dictionary.
To obtain your initial refresh token:
https://api.tradestation.com/v2/authorize? response_type=code& client_id=YOUR_CLIENT_ID& redirect_uri=YOUR_REDIRECT_URI& audience=https://api.tradestation.com& scope=openid offline_access profile MarketData ReadAccount Trade Matrix OptionSpreads& state=YOUR_STATE_VALUE
curl -X POST https://signin.tradestation.com/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET" \ # If applicable
-d "code=YOUR_CODE" \
-d "redirect_uri=YOUR_REDIRECT_URI"(Note: The token exchange endpoint is https://signin.tradestation.com/oauth/token)
The library automatically handles token refreshing. When an access token expires, or is within 5 minutes of expiring, the library will use the stored refresh token to obtain a new access token by making a request to https://signin.tradestation.com/oauth/token.
Importantly, the refresh process itself might return a new refresh token. The library will automatically store this new refresh token and use it for future refreshes.
You don't need to manage this process manually, but ensure the initial refresh_token is provided correctly. If the refresh process fails (e.g., due to an invalid refresh token or network issue), the library may raise a ValueError.
sequenceDiagram
participant UserCode as User Code
participant Client as TradeStationClient
participant TokenManager as TokenManager
participant AuthServer as Auth Server<br>(signin.tradestation.com)
UserCode->>Client: Initialize(config)
Client->>TokenManager: Initialize(config)
Note over TokenManager: Stores client_id, refresh_token
UserCode->>Client: Make API Call (e.g., get_accounts())
Client->>TokenManager: get_valid_access_token()
TokenManager->>TokenManager: Check if token exists and is valid
alt Token is valid (not expired/near expiry)
TokenManager-->>Client: Return cached access_token
else Token needs refresh or doesn't exist
TokenManager->>AuthServer: POST /oauth/token (grant_type=refresh_token, client_id, refresh_token)
AuthServer-->>TokenManager: {access_token, expires_in, refresh_token*} (*optional new refresh token)
TokenManager->>TokenManager: Store new access_token, expiry, potentially new refresh_token
TokenManager-->>Client: Return new access_token
end
Client->>Client: Add access_token to API request header
Client->>AuthServer: Perform API Request (e.g., GET /v3/accounts)
AuthServer-->>Client: API Response
Client-->>UserCode: Return API result
You may need to retrieve the current refresh token (e.g., to save it for future sessions after it might have been updated):
refresh_token = client.get_refresh_token()
if refresh_token:
print(f"Current refresh token: {refresh_token}")
else:
print("No refresh token available.")TradeStation provides two environments:
Specify the environment ("Live" or "Simulation") when creating the client. This setting determines the base URL for API calls but does not affect the authentication endpoint, which is always https://signin.tradestation.com/oauth/token.
You can enable debug mode for more detailed logging, potentially including information about the authentication process:
client = TradeStationClient(debug=True)(Note: The specifics of debug logging might vary depending on the client implementation details.)
| Back | FazBrowse Home | New Git URL |