Skip to content

Commit 4bcb993

Browse files
authored
remove username/password auth (#270)
* remove user/pass sessions * update templates
1 parent 14d0ac8 commit 4bcb993

19 files changed

Lines changed: 166 additions & 876 deletions

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Since Tastytrade certification sessions are severely limited in capabilities, th
55
## Steps to follow to contribute
66

77
1. Fork the repository to your personal Github account and make your proposed changes.
8-
2. Export your username, password, and account number to the following Github Actions repository secrets: `TT_USERNAME`, `TT_PASSWORD`, and `TT_ACCOUNT`. The account should be a margin account.
8+
2. Export your username, password, and account number to the following Github Actions repository secrets: `TT_SECRET`, `TT_REFRESH`, and `TT_ACCOUNT`. The account should be a margin account.
99
3. Make sure you have at least one share of long $F in your account, which will be used to place the OCO complex order (nothing will fill), as well as at least $2 of buying power.
1010
4. Run `make install` to create the virtual environment, `make lint` to format your code, and `make test` to run the tests locally.

.github/pull_request_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Fixes ...
66
## Pre-merge checklist
77
- [ ] Code formatted correctly (check with `make lint`)
88
- [ ] Code implemented for both sync and async
9-
- [ ] Passing tests locally (check with `make test`, make sure you have `TT_USERNAME`, `TT_PASSWORD`, and `TT_ACCOUNT` environment variables set)
9+
- [ ] Passing tests locally (check with `make test`, make sure you have `TT_REFRESH`, `TT_SECRET`, and `TT_ACCOUNT` environment variables set)
1010
- [ ] New tests added (if applicable)
1111

1212
Please note that, in order to pass the tests, you'll need to set up your Tastytrade credentials as repository secrets on your local fork. Read more at CONTRIBUTING.md.

.github/workflows/python-app.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ jobs:
3232
run: |
3333
uv run pytest --cov=tastytrade --cov-report=term-missing tests/ --cov-fail-under=95
3434
env:
35-
TT_USERNAME: ${{ secrets.TT_USERNAME }}
36-
TT_PASSWORD: ${{ secrets.TT_PASSWORD }}
37-
TT_USERNAME_SANDBOX: ${{ secrets.TT_USERNAME_SANDBOX }}
38-
TT_PASSWORD_SANDBOX: ${{ secrets.TT_PASSWORD_SANDBOX }}
35+
TT_REFRESH_SANDBOX: ${{ secrets.TT_REFRESH_SANDBOX }}
36+
TT_SECRET_SANDBOX: ${{ secrets.TT_SECRET_SANDBOX }}
3937
TT_ACCOUNT: ${{ secrets.TT_ACCOUNT }}
4038
TT_REFRESH: ${{ secrets.TT_REFRESH }}
4139
TT_SECRET: ${{ secrets.TT_SECRET }}

docs/api/backtesting.rst

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/backtest.rst

Lines changed: 0 additions & 32 deletions
This file was deleted.

docs/index.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ A simple, reverse-engineered, sync/async SDK for Tastytrade built on their (now
3939
account-streamer
4040
data-streamer
4141
market-data
42-
backtest
4342
market-sessions
4443
watchlists
4544

@@ -49,7 +48,6 @@ A simple, reverse-engineered, sync/async SDK for Tastytrade built on their (now
4948
:hidden:
5049

5150
api/account
52-
api/backtesting
5351
api/dxfeed
5452
api/instruments
5553
api/market-data

docs/sessions.rst

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,33 @@
11
Sessions
22
========
33

4-
Creating a session
5-
------------------
4+
Creating an OAuth application
5+
-----------------------------
66

7-
A session object is required to authenticate your requests to the Tastytrade API.
8-
To create a production (real) session using your normal login:
7+
A session object is required to authenticate your requests to the Tastytrade API. Tastytrade uses OAuth logins, which allow you to connect applications (third-party or private) to your trading account to use the API.
98

10-
.. code-block:: python
9+
To get started, create a new OAuth application `here <https://my.tastytrade.com/app.html#/manage/api-access/oauth-applications>`_. Check all the scopes you plan to use, then create the application. **Save the client secret**, then go to OAuth Applications > Manage > Create Grant to create a new grant with the same scopes. This will give you a refresh token, **which you should also save**.
1110

12-
from tastytrade import Session
13-
session = Session('username', 'password')
11+
At this point, OAuth is now setup correctly! Doing the above once is sufficient for **indefinite usage** of ``Session`` for authentication to the API, since refresh tokens never expire. From now on you can simply authenticate with your client secret and refresh token.
1412

15-
A certification (test) account can be created `here <https://developer.tastytrade.com/sandbox/>`_, then used to create a session.
13+
Creating a session
14+
------------------
1615

1716
.. code-block:: python
1817
1918
from tastytrade import Session
20-
session = Session('username', 'password', is_test=True)
21-
22-
You can make a session persistent by generating a remember token, which is valid for 24 hours:
23-
24-
.. code-block:: python
25-
26-
session = Session('username', 'password', remember_me=True)
27-
remember_token = session.remember_token
28-
# remember token replaces the password for the next login
29-
new_session = Session('username', remember_token=remember_token)
30-
31-
.. note::
32-
If you used a certification (test) account to create the session associated with the `remember_token`, you must set `is_test=True` when creating subsequent sessions.
33-
34-
OAuth sessions
35-
--------------
36-
37-
Tastytrade has recently added support for OAuth logins, which allow you to connect an application for the purposes of managing trades on your behalf. Apart from allowing you to connect to 3rd-party apps (or build your own), you can also build a private OAuth application, which provides better security compared to username/password authentication since you don't have to expose your login information.
38-
39-
To get started, create a new OAuth application `here <https://my.tastytrade.com/app.html#/manage/api-access/oauth-applications>`_. You'll need to check all the scopes and save the client ID and client secret. Then, run this code:
4019
41-
.. code-block:: python
42-
43-
from tastytrade.oauth import login
44-
45-
login()
46-
47-
This will open up a web interface in your browser where you'll be prompted to paste your client ID and client secret. These credentials will then be used to connect your application to Tastytrade. After following the steps in your browser, you should see your refresh token in the browser and in the console, which you should save.
48-
49-
At this point, OAuth is now setup correctly! Doing the above once is sufficient for **indefinite usage** of ``OAuthSession`` for authentication to the API, since refresh tokens never expire. From now on you can simply authenticate like so:
50-
51-
.. code-block:: python
20+
session = Session('client_secret', 'refresh_token')
5221
53-
from tastytrade import OAuthSession
54-
55-
session = OAuthSession('my-client-secret', 'my-refresh-token')
56-
57-
These session objects can be used almost anywhere you can use a normal session:
22+
These session objects can be used to make API requests:
5823

5924
.. code-block:: python
6025
6126
from tastytrade import Account
6227
6328
accounts = Account.get(session)
6429
65-
Note that OAuth sessions make API requests using a special session token, which has a duration of only 15 minutes. However, since the refresh tokens last forever, you can call ``OAuthSession.refresh()`` to refresh the session token whenever needed. The session object will keep track of session expiration time for you to make it easier to know when to refresh:
30+
Note that OAuth sessions make API requests using a special session token, which has a duration of only 15 minutes. However, since the refresh tokens last forever, you can call ``Session.refresh()`` to refresh the session token whenever needed. The session object will keep track of session expiration time for you to make it easier to know when to refresh:
6631

6732
.. code-block:: python
6833
@@ -71,3 +36,10 @@ Note that OAuth sessions make API requests using a special session token, which
7136
if now_in_new_york() > session.session_expiration:
7237
session.refresh()
7338
print(Account.get(session))
39+
40+
A sandbox account for testing can be created `here <https://developer.tastytrade.com/sandbox/>`_, then used to create a session in the same way:
41+
42+
.. code-block:: python
43+
44+
from tastytrade import Session
45+
session = Session('client_secret', 'refresh_token', is_test=True)

tastytrade/__init__.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import logging
22

33
API_URL = "https://api.tastyworks.com"
4-
BACKTEST_URL = "https://backtester.vast.tastyworks.com"
54
CERT_URL = "https://api.cert.tastyworks.com"
65
VAST_URL = "https://vast.tastyworks.com"
7-
VERSION = "10.3.1"
6+
VERSION = "11.0.0"
87

98
__version__ = VERSION
109
version_str: str = f"tastyware/tastytrade:v{VERSION}"
@@ -15,13 +14,7 @@
1514
# ruff: noqa: E402
1615

1716
from .account import Account
18-
from .session import OAuthSession, Session
17+
from .session import Session
1918
from .streamer import AlertStreamer, DXLinkStreamer
2019

21-
__all__ = [
22-
"Account",
23-
"AlertStreamer",
24-
"DXLinkStreamer",
25-
"OAuthSession",
26-
"Session",
27-
]
20+
__all__ = ["Account", "AlertStreamer", "DXLinkStreamer", "Session"]

tastytrade/account.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,8 @@ async def a_get_order_chains(
16811681
Get a list of order chains (open + rolls + close) for given symbol
16821682
over the given time frame, with total P/L, commissions, etc.
16831683
1684+
Not supported for OAuth sessions--write Tasty to get this added!
1685+
16841686
:param session: the session to use for the request.
16851687
:param symbol: the underlying symbol for the chains.
16861688
:param start_time: the beginning time of the query.
@@ -1720,6 +1722,8 @@ def get_order_chains(
17201722
Get a list of order chains (open + rolls + close) for given symbol
17211723
over the given time frame, with total P/L, commissions, etc.
17221724
1725+
Not supported for OAuth sessions--write Tasty to get this added!
1726+
17231727
:param session: the session to use for the request.
17241728
:param symbol: the underlying symbol for the chains.
17251729
:param start_time: the beginning time of the query.

0 commit comments

Comments
 (0)