Auth with the TD Ameritrade API is a bit of a pain. This is a web app that allows you to authorize and authenticate yourself to use the TD Websocket API. A TD trading account is required.
- Go to https://developer.tdameritrade.com and register an app. Use 'https://localhost:8080/td-login-redirect' as your redirect url.
- Copy your consumer key into the
VUE_APP_TD_CONSUMER_KEY
var in the .env.local file. npm install
npm run serve
- Navigate to https://localhost:8080 (Yes, we are running https locally. The TD redirect requires https.)
- Click the td auth link at the top of the page and go through the log in process.
After logging in, a refresh token is stored locally for 90 days, so next time you run the app you won't have to log in again.
- A link is created allowing you to log in to your td account. After logging in, it returns an OAuth code to the redirect url.
- If a refresh token exists in local storage, we use that to get an access token.
- Using the code, we retrieve an access token and store it in vuex. We also store the refresh token in local storage.
- Using the access token, we authenticate and connect to the websocket api.
- Requests are made to set the QOS to retrieve real time data and quotes on hot stock picks.
- This was created with vue cli
- We need to use history mode for the router instead of hash mode because td won't accept a redirect url with a hash. Since the redirect is a deep link back to localhost, historyApiFallback has to be set to true in vue.config.js
https://developer.tdameritrade.com/content/streaming-data
If you find yourself making an abundance of money (or are planning on making an abundance of money) using this code, please show your support to a fellow dev.