|
| 1 | +# Tradier OAuth Payload + Refresh Support (LumiBot) |
| 2 | + |
| 3 | +Date: 2026-02-07 |
| 4 | +Owner: Codex (implementation + research) |
| 5 | +Status: Implemented in `lumibot` (commit `52eef70b`) |
| 6 | + |
| 7 | +## Why this exists |
| 8 | + |
| 9 | +BotSpot deployments can link Tradier via OAuth. Tradier OAuth access tokens expire (per Tradier docs), so a long-running bot needs refresh-token support (when available) to keep trading without requiring users to re-link every day. |
| 10 | + |
| 11 | +## Runtime Inputs (env vars) |
| 12 | + |
| 13 | +This implementation supports both existing “manual token” Tradier usage and OAuth usage. |
| 14 | + |
| 15 | +### Existing (manual token) |
| 16 | + |
| 17 | +- `TRADIER_ACCESS_TOKEN` |
| 18 | +- `TRADIER_ACCOUNT_NUMBER` |
| 19 | +- `TRADIER_IS_PAPER` (`true`/`false`) |
| 20 | + |
| 21 | +### OAuth mode (BotSpot) |
| 22 | + |
| 23 | +BotSpot injects: |
| 24 | + |
| 25 | +- `TRADIER_TOKEN` |
| 26 | + - base64url JSON payload from the OAuth token exchange |
| 27 | + - expected fields: `access_token`, `expires_in` (optional), `issued_at` (optional), `refresh_token` (optional) |
| 28 | +- `TRADIER_REFRESH_TOKEN` (optional) |
| 29 | +- `TRADIER_OAUTH_CLIENT_ID` (required to refresh) |
| 30 | +- `TRADIER_OAUTH_CLIENT_SECRET` (required to refresh) |
| 31 | + |
| 32 | +Notes: |
| 33 | + |
| 34 | +- Tradier refresh tokens are **partner-only**; if `TRADIER_REFRESH_TOKEN` is not present, the bot can still start, but it may stop working once the access token expires. |
| 35 | + |
| 36 | +## What changed |
| 37 | + |
| 38 | +File: `lumibot/brokers/tradier.py` |
| 39 | + |
| 40 | +- Added support for decoding `TRADIER_TOKEN` (base64url JSON). |
| 41 | + - If `access_token` is missing/blank in config/args, it is sourced from the decoded payload. |
| 42 | +- Added best-effort refresh support via: |
| 43 | + - proactive refresh near expiry (when expiry metadata exists) |
| 44 | + - forced refresh and single retry when an API call fails with `401` |
| 45 | +- Refresh uses Tradier endpoint: |
| 46 | + - `POST https://api.tradier.com/v1/oauth/refreshtoken` |
| 47 | + - Basic Auth: `TRADIER_OAUTH_CLIENT_ID:TRADIER_OAUTH_CLIENT_SECRET` |
| 48 | + - Body: `grant_type=refresh_token&refresh_token=<TRADIER_REFRESH_TOKEN>` |
| 49 | +- When a refresh succeeds, the broker updates the access token across: |
| 50 | + - broker’s `lumiwealth_tradier` client |
| 51 | + - data source’s `lumiwealth_tradier` client |
| 52 | + |
| 53 | +## Limitations / Operational Notes |
| 54 | + |
| 55 | +- If Tradier rotates the refresh token on refresh, the new refresh token cannot be persisted back into task env vars. |
| 56 | + - The code logs a warning if refresh-token rotation is detected. |
| 57 | + - If rotation happens, users may need to re-link after the old refresh token becomes invalid. |
| 58 | + |
| 59 | +## Tests |
| 60 | + |
| 61 | +File: `tests/test_tradier.py` |
| 62 | + |
| 63 | +- Added unit tests for: |
| 64 | + - decoding OAuth payload into `access_token` |
| 65 | + - refreshing token when payload is expired (requests mocked; no network) |
| 66 | + |
0 commit comments