Pulling Data from the Sleeper API

This next post in our Fantasy Basketball Optimization series focuses on our second data source: the Sleeper API. The Sleeper fantasy app provides a variety of different API endpoints for users to pull league data (i.e. teams, rosters, available players, etc.), which we used to add to our fantasy basketball optimization database.

This article outlines how we used Python to pull and re-format the data for our relational database of CSV files. Note that while the final pipeline implements an AWS Lambda function for this, we’ll demo the code in a Jupyter notebook for the scope of this article.

Getting your League ID

First things first, you’ll need to find your specific league ID, which tells the API which league you’re pulling data for. One way to get this is to start with your Sleeper account username and use an endpoint to get your user_id from it. There’s another endpoint to list out all of the NBA fantasy leagues associated with your account, and we’ll take the first value from the list.

Users Table

Once we have our league ID, we’ll create a table of all the teams in our league from this league users endpoint. In the rest of the data, each team is identified by a USER_ID attribute, so this table identifies each team by USER_ID.

Rosters Table

Next, we’ll pull each team’s roster with this rosters endpoint. The data has an OWNER_ID field to join with the USER_ID from the Users table to identify each team. Sleeper also provides their own unique ID (PLAYER_ID_SLEEPER) to identify each player.

Players Table

Since Sleeper has its own PLAYER_ID attribute (that’s different from the PLAYER_ID in the NBA stats data), we need to somehow connect the two. We can join them by player name, so we’ll create a third table that lists out the different PLAYER_ID_SLEEPER values with player names and other info.

Final Data Model

We can connect these 3 new tables into our existing data model from the last post so that we can filter the data by the context of our league.

Conclusion

With the help of the Sleeper API, we now have our league-specific data incorporated into our relational database. In the following posts, we’ll add our third and final data source (the lock-in spreadsheet) and put all the data together to optimize our fantasy basketball team.

Feel free to download the full Jupyter notebook of Python code and try it out yourself:

If you found this guide helpful, please drop a comment below with questions and/or feedback. Your input helps us improve future posts and inspire new ideas!

Thanks for joining us today, and check out our next post in the series: Managing the Lock-in Spreadsheet

Leave a Comment

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

Scroll to Top