JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q23007F
  • License ISC

Library for accessing TDA Web API

Package Exports

  • node-tda

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-tda) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-tda

Convenient library for TDA REST API.

Status

GitHub package.json version Build nycrc config on GitHub issues issues

Usage

Prerequisites

  1. Follow these instructions to create a tda developer account and app. Set your app redirect uri to https://localhost:8443/.
  2. Take note of the consumer key generated for your app.
  3. Install openssl on your system if you want to use the CLI to aid in retrieving your tokens.

Install

  • npm install node-tda

CLI

For CLI usage, also install node-tda as a global package npm install -g node-tda

The cli uses an https server to retrieve access tokens from the tda oauth login. For convenience, self-signed certs are generated automatically upon install. If your system is missing openssl from its path, the certs will not be generated and the cli won't work.

For oauth login, run: tda_authenticate --CONSUMER_KEY="<CONSUMER_KEY>"

Lib

API Functions

All functions names are a camelCase reflection their respective names found in the TDA API documentation. As of the writing of this document, all of the functions published in TDA's web api have been implemented.

i.e.:

// Get Accounts
// https://developer.tdameritrade.com/account-access/apis/get/accounts-0
const { getAccounts } = require('node-tda');

// Place Order
// https://developer.tdameritrade.com/account-access/apis/post/accounts/%7BaccountId%7D/orders-0
const { placeOrder } = require('node-tda');

The function signatures apiFunc(options, callback); include an optional callback. When the callback is omitted, a promise is returned.

Using Promises:

const { authenticate, generateTokens, refreshToken, getAccounts } = require('node-tda');

async function auth(consumerKey) {
  const grant = await authenticate({
    consumerKey
  });
  const token = await generateTokens({
    consumerKey,
    grant
  });
  return token['access_token'];
}

const consumerKey = "someKey";
auth(consumerKey)
  .then(async (token) => {
    const accounts = await getAccounts({ token });
    console.log(JSON.stringify(accounts));
    setInterval(async () => {
      // refresh token
      const newToken = refreshToken({
        ...token,
        consumerKey
      });
      console.log(JSON.stringify(newToken));
    }, 1800000);
  })
  .catch((err) => {
    console.log(err);
  });

Using Callbacks:

const { authenticate, generateTokens, refreshToken, getAccounts } = require('node-tda');

function auth(consumerKey, callback) {
  authenticate({
    consumerKey
  }, (grant) => {
    generateTokens({
      consumerKey,
      grant
    }, (token) => {
      callback(null, token['access_token']);
    });
  });
}

const consumerKey = "someKey";
auth(consumerKey, (err, token) => {
  getAccounts({ token }, (accounts) => {
    console.log(JSON.stringify(accounts));
    setInterval(async () => {
      // refresh token
      const newToken = refreshToken({
        ...token,
        consumerKey
      });
      console.log(JSON.stringify(newToken));
    }, 1800000);
  });
});