whatadewitt / yahoo-fantasy-sports-api

NodeJS wrapper for the Yahoo! Fantasy Sports API
https://yahoo-fantasy-node-docs.vercel.app/
MIT License
205 stars 54 forks source link
fantasy-api fantasy-sports javascript nodejs sports yahoo

Yahoo! Fantasy API Node Module

This is a node module created to wrap the Yahoo! Fantasy Sports API (link). At the moment, not all subresources are available, nor are any of the 'collection' elements. I do hope to add them, and they have been added to the code, but as of now this project is very much in an open beta phase.

The API is designed to act as a helper for those interacting with the Y! Fantasy API. The goal is for ease of use for the user, both in terms of querying endpoints and parsing responses. I've noticed that in working with the API, the data is not always the easiest to understand, so hopefully what I have created here will help people out.

Installation

You can install the module via npm by running:

$ npm install yahoo-fantasy

Licence

This module is available under the MIT Licence

Documentation

More complete documentation can be found using the application sandbox (Link). This sandbox is always a work in progress, if I've learned anything it's that nothing is ever complete.

The API can be used by simply importing the module and querying data, since version 4.0 the authentication flow has been built into the library to make things easier for users.

// import the library
const YahooFantasy = require('yahoo-fantasy');

// you can get an application key/secret by creating a new application on Yahoo!
const yf = new YahooFantasy(
  Y!APPLICATION_KEY, // Yahoo! Application Key
  Y!APPLICATION_SECRET, // Yahoo! Application Secret
  tokenCallbackFunction, // callback function when user token is refreshed (optional)
  redirectUri // redirect endpoint when user authenticates (optional)
);

// you can authenticate a user by setting a route to call the auth function
// note: from v4.0 on, public queries are now supported; that is, you can query
// public resources without authenticating a user (ie/ game meta, player meta,
// and information from public leagues)
yf.auth(
  response // response object to redirect the user to the Yahoo! login screen
)

// you also need to set up the callback route (defined as the redirect uri above)
// note: this will automatically set the user and refresh token if the request is
// successful, but you can also call them manually, described below
yf.authCallback(
  request, // the request will contain the auth code from Yahoo!
  callback // callback function that will be called after the token has been retrieved
)

// if you're not authenticating via the library you'll need to set the Yahoo!
// token for the user
yf.setUserToken(
  Y!CLIENT_TOKEN
);

// you can do the same for the refresh token...
// if you set this and the token expires (lasts an hour) then the token will automatically
// refresh and call the above "tokenCallbackFunction" that you've specified to persist the
// token elsewhere
yf.setRefreshToken(
  Y!CLIENT_REFRESH_TOKEN
);

// query a resource/subresource
yf.{resource}.{subresource} (
  {possible argument(s)},
  function cb(err, data) {
    // handle error
    // callback function
    // do your thing
  }
);

Starting with v3.1.0 you can also use a promise chain to query resources and subresources

yf.{resource}.{subresource} (
  {possible argument(s)}
)
.then(data => // do your thing)
.catch(err => // handle error)

This also opens the door to use async/await in version of node that support it

try {
  let data = await yf.{resource}.{subresource} (
    {possible argument(s)}
  )

  // do your thing
} catch(err) {
  // handle error
}

Bugs & Issues

This project is very much still a work in progress, please report any issues via the GitHub issues page.

Changelog

5.3.0

5.2.2

5.2.1

5.2.0

5.1.0

5.0.0

4.2.0

4.1.5

4.1.4

4.1.3

4.1.2

4.1.1

4.1.0

4.0.0

3.2.0

3.1.2

3.1.1

3.1.0

3.0.4

3.0.3

3.0.2

3.0.1

3.0.0

2.0.4

2.0.3

2.0.2

2.0.1

2.0.0

1.0.2

1.0.1

1.0.0

0.5.3

0.5.2

0.5.1

0.5.0

0.4.4

0.4.3

0.4.2

0.4.1

0.4.0

0.3.1

0.3.0

0.2.2

0.2.0

0.1.2

0.1.1

0.1