Yahoo Fantasy Baseball API with R

By | June 3, 2017

Connecting to the Yahoo Fantasy Baseball API is tricky business. If you’ve found this post, you may have also found this post by Ryan Kuhn, and this post by Cory Nissen, both of whom have successfully implemented the API.

I want to build on their work a little bit, by adding some extra functionality and making some generalized functions for connecting to the API. Those functions can be found in Github here, and an example of using them is here.

Additionally, these links might be useful:

So first, a complete example, start to finish. This script will connect to the API and pull down a list of all the relievers in your league:

The final product is a data frame which contains all the relievers in your league, and their associated player data in Yahoo:

Data Frame with all RPs

1. Obtaining Credentials

This part is really easy. Go here and register a new application. Pick “Installed Application” at the top, and check “Fantasy Sports” at the bottom.

It will give you a consumer key and a secret key. I saved mine to a txt file but you can hardcode them into your R script.

2. Connecting with Oauth

This is where things get a bit trickier. Ryan Kuhn’s version tells you to use Oauth2.0, but then you will quickly end up in this conundrum where both Yahoo and R are asking for a 7-digit code.

Instead, we will use Oauth1.0, found in the httr library:

If you run this function with the keys yahoo provided you, you should end up at a screen that looks like this:

Yahoo Fantasy Oauth

Just click through; everything should be pretty simple. Note: I am also saving off a copy of the token, so that you don’t need to do this every time.

3. Figuring out your League Key

To identify your league, you need a League Key, like: 370.l.12345. There are three parts to this:

  1. Game ID: A number that represents game and season. So, MLB 2017 is 370.
  2. The lowercase letter “L”, for “league”.
  3. League ID The number that identifies your league. You can find that at the top of your league settings or in the URL

To get your game ID from Yahoo, you can use this function:

4. Querying the API

You’ll notice that I abstracted a function to do the queries. It queries the API, brings in the XML, and returns the root XML node which can be manipulated (such as with the xmlToDataFrame function.

Note: it wasn’t immediately obvious to me how to append more than one filter to the end of the URL. The Yahoo Documentation seems to imply that the URL is /players;position=RP;status=A to get a list of all the active Relief Pitchers.

In our case, though, the right way to construct the URL is: /players?&position=RP&status=A.

Obviously, there is a lot more that this API can do, and I will spend some time in the future trying to build out some better tools for connecting with the API. For now, I hope that this little guide provides the basic building blocks for someone trying to implement it.

I’ll keep working on this; if you are interested, follow me on twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *