Free, open-source Node.js client for the TD Ameritrade Trading Platform.
WARNING This library is still in its early stages of development and thus far from ready for production use.
There are 3 interfaces available:
TDAmeritrade
- the main API interface.TDAccount
- a helper interface to ease the use of account specific functions.TDStreamer
- the data streaming interface.
The API client is very close to being complete. All documented API methods have been implemented.
The data streaming interface implements most of what the documentation talks about except Actives, Level 1 Options and Level 2 order book. Documentation also mentions services NEWS_STORY
and NEWS_HEADLINE_LIST
but does not provide any information.
An attempt to provide typescript definitions is also in progress.
Using npm:
$ npm install @knicola/tdameritrade
Using yarn:
$ yarn add @knicola/tdameritrade
const { TDAmeritrade, TDAccount, TDStreamer } = require('@knicola/tdameritrade')
const config = {
accessToken: 'access_token',
apiKey: 'testClientId', // `@AMER.OAUTHAP` suffix is not required
returnFullResponse: false, // Set to true to return the full axios response (default: false)
}
// main api interface
const api = new TDAmeritrade(config)
const accounts = await api.getAccounts()
const orders = api.getOrders(accounts[0].accountId).then(orders => {
// do something with orders ..
return orders
})
// account specific interface
const account = new TDAccount(accounts[0].accountId, config)
const orders = account.getOrders()
// data streamer
const userPrincipals = await api.getUserPrincipals([
'streamerSubscriptionKeys',
'streamerConnectionInfo',
])
const streamer = new TDStreamer(userPrincipals)
streamer.on('authenticated', () => streamer.subsChartEquity('SPY'))
streamer.on('chart', data => {
// do something with chart data ..
})
streamer.connect()
Official API documentation can be found here: https://developer.tdameritrade.com/apis
Generate an SSL certificate and place it in the root folder before running any tests:
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout selfsigned.key -out selfsigned.crt -batch
For live tests create an access token:
- Register for a developer's account and add a new app.
- Copy
.env.example
to.env
and fill in theCLIENT_ID
var with the app'sConsumer Key
. - Run
npm run authorize
and login with your regular TD Ameritrade account.
Command | Description |
---|---|
npm test |
Run test suites |
npm run lint |
Run linting rules |
npm run coverage |
Generate coverage report |
npm run build |
Bundle the project for browser use |
npm run authorize |
Run oauth and get an access token for testing (saves the token for later use) |
npm run refresh_token |
Refresh the oauth access token (also saves the new token) |
npm run user_principals |
Get the user principals object used to authenticate to the json streaming service |
This project is open-sourced software licensed under the MIT license.