Low Level NHL API

If you would like to interact with the API directly without any helpers, the nhl_get_data() function is made for this purpose. It takes as input one or more urls (a character vector) and returns a list of the same length, each element containing the raw results returned from the API for the corresponding URL.

For example:

library(nhlapi)
urls <- c(
  "https://statsapi.web.nhl.com/api/v1/teams/1",
  "https://statsapi.web.nhl.com/api/v1/people/8477474"
)

nhl_get_data(urls)
#> [[1]]
#> [[1]]$copyright
#> [1] "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2021. All Rights Reserved."
#> 
#> [[1]]$teams
#>   id              name            link abbreviation teamName locationName
#> 1  1 New Jersey Devils /api/v1/teams/1          NJD   Devils   New Jersey
#>   firstYearOfPlay  shortName                 officialSiteUrl franchiseId active
#> 1            1982 New Jersey http://www.newjerseydevils.com/          23   TRUE
#>          venue.name          venue.link venue.city venue.timeZone.id
#> 1 Prudential Center /api/v1/venues/null     Newark  America/New_York
#>   venue.timeZone.offset venue.timeZone.tz division.id   division.name
#> 1                    -5               EST          25 MassMutual East
#>          division.link conference.id conference.name       conference.link
#> 1 /api/v1/divisions/25             6         Eastern /api/v1/conferences/6
#>   franchise.franchiseId franchise.teamName        franchise.link
#> 1                    23             Devils /api/v1/franchises/23
#> 
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[2]]
#> [[2]]$copyright
#> [1] "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2021. All Rights Reserved."
#> 
#> [[2]]$people
#>        id      fullName                   link firstName lastName primaryNumber
#> 1 8477474 Madison Bowey /api/v1/people/8477474   Madison    Bowey            29
#>    birthDate currentAge birthCity birthStateProvince birthCountry nationality
#> 1 1995-04-22         25  Winnipeg                 MB          CAN         CAN
#>   height weight active alternateCaptain captain rookie shootsCatches
#> 1  6' 2"    202   TRUE            FALSE   FALSE  FALSE             R
#>   rosterStatus currentTeam.id   currentTeam.name currentTeam.link
#> 1            Y             16 Chicago Blackhawks /api/v1/teams/16
#>   primaryPosition.code primaryPosition.name primaryPosition.type
#> 1                    D           Defenseman           Defenseman
#>   primaryPosition.abbreviation
#> 1                            D
#> 
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"

Since it is helpful for work with R to get data in a flat data frame format, the function will by default attempt to flatten nested data frame columns and return flat data frames where possible. If you really want raw data returned, you can set the flatten argument to FALSE:

nhl_get_data(urls, flatten = FALSE)
#> [[1]]
#> [[1]]$copyright
#> [1] "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2021. All Rights Reserved."
#> 
#> [[1]]$teams
#>   id              name            link        venue.name          venue.link
#> 1  1 New Jersey Devils /api/v1/teams/1 Prudential Center /api/v1/venues/null
#>   venue.city venue.timeZone.id venue.timeZone.offset venue.timeZone.tz
#> 1     Newark  America/New_York                    -5               EST
#>   abbreviation teamName locationName firstYearOfPlay division.id
#> 1          NJD   Devils   New Jersey            1982          25
#>     division.name        division.link conference.id conference.name
#> 1 MassMutual East /api/v1/divisions/25             6         Eastern
#>         conference.link franchise.franchiseId franchise.teamName
#> 1 /api/v1/conferences/6                    23             Devils
#>          franchise.link  shortName                 officialSiteUrl franchiseId
#> 1 /api/v1/franchises/23 New Jersey http://www.newjerseydevils.com/          23
#>   active
#> 1   TRUE
#> 
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[2]]
#> [[2]]$copyright
#> [1] "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2021. All Rights Reserved."
#> 
#> [[2]]$people
#>        id      fullName                   link firstName lastName primaryNumber
#> 1 8477474 Madison Bowey /api/v1/people/8477474   Madison    Bowey            29
#>    birthDate currentAge birthCity birthStateProvince birthCountry nationality
#> 1 1995-04-22         25  Winnipeg                 MB          CAN         CAN
#>   height weight active alternateCaptain captain rookie shootsCatches
#> 1  6' 2"    202   TRUE            FALSE   FALSE  FALSE             R
#>   rosterStatus currentTeam.id   currentTeam.name currentTeam.link
#> 1            Y             16 Chicago Blackhawks /api/v1/teams/16
#>   primaryPosition.code primaryPosition.name primaryPosition.type
#> 1                    D           Defenseman           Defenseman
#>   primaryPosition.abbreviation
#> 1                            D
#> 
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"

In case there are errors encountered when retrieving the data, the process does not fail, but returns a list with class nhl_get_data_error:

nhl_get_data(c("https://statsapi.web.nhl.com/api/v1/wrongurl", urls))
#> 14:18:46 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 0 HTTP error 404.
#> 14:18:47 | E | The following 1 of 3 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/wrongurl
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : HTTP error 404.\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): HTTP error 404.>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/wrongurl"
#> 
#> [[2]]
#> [[2]]$copyright
#> [1] "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2021. All Rights Reserved."
#> 
#> [[2]]$teams
#>   id              name            link abbreviation teamName locationName
#> 1  1 New Jersey Devils /api/v1/teams/1          NJD   Devils   New Jersey
#>   firstYearOfPlay  shortName                 officialSiteUrl franchiseId active
#> 1            1982 New Jersey http://www.newjerseydevils.com/          23   TRUE
#>          venue.name          venue.link venue.city venue.timeZone.id
#> 1 Prudential Center /api/v1/venues/null     Newark  America/New_York
#>   venue.timeZone.offset venue.timeZone.tz division.id   division.name
#> 1                    -5               EST          25 MassMutual East
#>          division.link conference.id conference.name       conference.link
#> 1 /api/v1/divisions/25             6         Eastern /api/v1/conferences/6
#>   franchise.franchiseId franchise.teamName        franchise.link
#> 1                    23             Devils /api/v1/franchises/23
#> 
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[3]]
#> [[3]]$copyright
#> [1] "NHL and the NHL Shield are registered trademarks of the National Hockey League. NHL and NHL team marks are the property of the NHL and its teams. © NHL 2021. All Rights Reserved."
#> 
#> [[3]]$people
#>        id      fullName                   link firstName lastName primaryNumber
#> 1 8477474 Madison Bowey /api/v1/people/8477474   Madison    Bowey            29
#>    birthDate currentAge birthCity birthStateProvince birthCountry nationality
#> 1 1995-04-22         25  Winnipeg                 MB          CAN         CAN
#>   height weight active alternateCaptain captain rookie shootsCatches
#> 1  6' 2"    202   TRUE            FALSE   FALSE  FALSE             R
#>   rosterStatus currentTeam.id   currentTeam.name currentTeam.link
#> 1            Y             16 Chicago Blackhawks /api/v1/teams/16
#>   primaryPosition.code primaryPosition.name primaryPosition.type
#> 1                    D           Defenseman           Defenseman
#>   primaryPosition.abbreviation
#> 1                            D
#> 
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"

The URLs themselves are preserved via the url attribute:

lapply(nhl_get_data(urls), attr, which = "url")
#> [[1]]
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[2]]
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"