NHL API: Get Team Rosters

 

Introduction

The NHL has a publicly accessible Application Programming Interface (or API) that allows users to obtain a lot of useful data (including detailed play-by-play data). There are a couple of obstacles though: 1) the NHL API does not come with a user manual; and 2) the NHL API is most easily accessed using a programming language such as R or Python. To help you overcome those obstacles I’m writing articles that provide the code (in the R programming language) for functions that pull data from the NHL API.

In this article I provide a function that pulls detailed roster data for each NHL team.

The Data

Before I provide the code, here’s the data it pulls:

player_id: 8478402

player: Connor McDavid

team_id: 22

team: Edmonton Oilers

position: C

age: 26

dob: 1997-01-13

nationality: CAN

height: 6’ 1”

weight: 193

shoots_catches: L

number: 97

The Code

Here’s the code for the function that pulls the above data.

get_team_rosters <- function() {
        
        # Create a vector of team_ids
        
        teams_site <- read_json("https://statsapi.web.nhl.com/api/v1/teams")
        
        teams <- teams_site$teams %>% 
                tibble() %>% 
                unnest_wider(1) %>% 
                select(team_id = id, full_team_name = name, team_abbr = abbreviation)
        
        team_ids <- teams$team_id
        
        # Loop through each team's API endpoint and collect expanded roster data
        
        temp_list <- list()
        
        for (i in team_ids) {
                
                roster_url <- paste0("https://statsapi.web.nhl.com/api/v1/teams/", i, "/roster?expand=roster.person")
                
                roster_site <- read_json(roster_url)
                
                roster_data <- roster_site$roster %>%
                        tibble() %>%
                        unnest_wider(1) %>%
                        unnest_wider(1) %>%
                        unnest_wider(currentTeam, names_sep = "_") %>%
                        unnest_wider(primaryPosition, names_sep = "_") %>%
                        unnest_wider(position, names_sep = "_")
                
                roster_data <- select(roster_data, 
                                 player_id = id,
                                 player = fullName,
                                 team_id = currentTeam_id,
                                 team = currentTeam_name,
                                 position = position_abbreviation,
                                 age = currentAge,
                                 dob = birthDate,
                                 nationality,
                                 height,
                                 weight,
                                 shoots_catches = shootsCatches,
                                 number = jerseyNumber
                                 )
                
                roster_data$dob <- as_date(roster_data$dob)
                
                temp_list[[i]] <- roster_data
        }
        
        rosters <- bind_rows(temp_list) 
        
        return(rosters)
}

Get It On GitHub

I’ve created a public repository on GitHub where I’ll be sharing my functions for accessing the NHL’s API. You can go to the repository by clicking here.

The End Of The Article

That’s the end of this article. Follow me on twitter to be notified about new content.

Cheers,

Mark (18 Skaters)