npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-tda

v1.0.0

Published

Library for accessing TDA Web API

Downloads

37

Readme

node-tda

Convenient library for TDA REST API.

Status

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

Contributing

Please review the contributing documentation.

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
  • npm install node-tda --no-optional if you don't need puppeteer for oauth authentication in a headless browser

Usage

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 automated oauth login:

tda_authenticate --CONSUMER_KEY='<CONSUMER_KEY>' --UID='<userid>' --PW='<password>'

For manual oauth login:

tda_authenticate --CONSUMER_KEY='<CONSUMER_KEY>'

Lib

REST API

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.

Examples:

// 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 REST API function signatures apiFunc(options, [callback]) include an optional callback, which when omitted, returns a promise.

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 = refreshTokenToken({
        ...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 = refreshTokenToken({
        ...token,
        consumerKey
      });
      console.log(JSON.stringify(newToken));
    }, 1800000);
  });
});

Streaming API

The Streamer Class extends EventEmitter and facilitates the use of the streaming API. See TDA's Streaming API documentation here.

Initialize Streaming Session:
const consumerKey = 'YourAppKey';
const refreshToken = 'YourRefreshToken';
async function streamingFun() {
  // refresh token:
  const { access_token } = await tda.refreshToken({
    consumerKey,
    refreshToken,
  });

  const userPrincipalsResponse = await tda.getUserPrincipals({
    token: access_token,
    fields: 'streamerSubscriptionKeys,streamerConnectionInfo',
  });

  const tdaStreamer = new tda.Streamer({
    userPrincipalsResponse,
  });

  tdaStreamer.on('connected', () => {
    tdaStreamer.request({
      requests: [
        {
          service: 'CHART_EQUITY',
          requestid: '2',
          command: 'SUBS',
          account: 'your_account',
          source: 'your_source_id',
          parameters: {
            keys: 'AAPL',
            fields: '0,1,2,3,4,5,6,7,8',
          },
        },
      ],
    });
  });

  tdaStreamer.on('message', (message) => {
    console.log(JSON.stringify(message));
  });
}

streamingFun();
Events:

| event | emits | | ------------ | -------------- | | error | Error | | message | message object | | connected | n/a | | disconnected | n/a |