SleeperAPI

Service

This Sleeper API provides data around NFL leagues, stats, etc. For example: you can find statistics for any given NFL player.

Someone would want to use this for doing sports analytics for the NFL and building on-screen visuals surrounding NFL players characteristics.

Load Libraries

library(httr)
library(jsonlite)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ readr     2.1.5
✔ ggplot2   4.0.0     ✔ stringr   1.5.1
✔ lubridate 1.9.4     ✔ tibble    3.3.0
✔ purrr     1.1.0     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter()  masks stats::filter()
✖ purrr::flatten() masks jsonlite::flatten()
✖ dplyr::lag()     masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(purrr)

Connecting API endpoint (fetching all players)

sleeper_url <- "https://api.sleeper.app/v1/players/nfl" 

response <- GET(sleeper_url)

players <- fromJSON(content(response, "text"))

Search on Google for “Sleeper API Free”, then click the “Sleeper API: introduction”, then scroll down to players -> Fetch all players.

From there you will find

GET https://api.sleeper.app/v1/players/nfl

which we will use to pull the API. This website also includes an example of every column and what they are for example : “college”: “Michigan”

This is grabbing all players in the NFL and is not intended to be called every time so make sure you save to your own server. Not to be called more than once per day.

##Searching Data

players_df <- map_dfr(players, function(p) {
  tibble(
    full_name = p$full_name,
    position = p$position,
    team = p$team,
    depth_chart_order = p$depth_chart_order,
    college = p$college,
    status = p$status,
    height = p$height,
    weight = p$weight,
    years_exp = p$years_exp
  )
}, .id = "player_id")

This first part is where you can search what position player you are looking for and where he his on the depth chart to find out if he plays a lot or not along with how many years of experience he has, along with many other criteria.

select_players <- players_df %>% 
  filter(full_name %in% c("Aaron Rodgers", "Patrick Mahomes", "Bo Nix"))
  
print(select_players)
# A tibble: 3 × 10
  player_id full_name      position college status height weight years_exp team 
  <chr>     <chr>          <chr>    <chr>   <chr>  <chr>  <chr>      <int> <chr>
1 4046      Patrick Mahom… QB       Texas … Active 74     225            8 KC   
2 96        Aaron Rodgers  QB       Califo… Active 74     223           20 PIT  
3 11563     Bo Nix         QB       Oregon  Active 74     217            1 DEN  
# ℹ 1 more variable: depth_chart_order <int>

This shows a select player based on their name and all its data.

TOP 10 # of players per position

top10_positions <- players_df %>% 
  filter(!is.na(position)) %>% 
  group_by(position) %>% 
  summarise(count = n()) %>% 
  slice_max(order_by = count, n = 10) %>% 
  arrange(desc(count))

ggplot(top10_positions, aes(x = reorder(position, count), y = count)) + 
  geom_col() +
  labs(
    title = "NFL player counts by position",
    x = "position",
    y = "# of players"
  )

This could be helpful for knowing which positions are sawed after and which positions there isn’t a lot of, to understand which positions a coach might want to go after in a Draft/Free Agency in order to better there team.