Low Level NHL API

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)
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:45 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:46 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:47 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:47 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:48 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:49 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 02:42:49 | E | The following 2 of 2 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[2]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> 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)
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:50 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:51 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:52 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:52 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:53 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:54 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 02:42:54 | E | The following 2 of 2 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[2]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> 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))
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/wrongurl': status was 'Couldn't resolve
#> host name'
#> 02:42:54 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/wrongurl': status was 'Couldn't resolve
#> host name'
#> 02:42:55 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/wrongurl': status was 'Couldn't resolve
#> host name'
#> 02:42:56 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/wrongurl error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:56 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:58 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:42:59 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:42:59 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:43:00 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:43:01 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 02:43:01 | E | The following 3 of 3 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/wrongurl$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/wrongurl'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/wrongurl"
#> 
#> [[2]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> attr(,"url")
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[3]]
#> [[1]]
#> [1] "Error in open.connection(con, \"rb\") : \n  cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <simpleError in open.connection(con, "rb"): cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'>
#> 
#> attr(,"class")
#> [1] "list"               "nhl_get_data_error"
#> 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")
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:43:01 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:43:02 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/teams/1': status was 'Couldn't resolve
#> host name'
#> 02:43:03 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/teams/1 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/teams/1'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:43:03 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 0 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:43:05 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 1 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> Warning in open.connection(con, "rb"): URL
#> 'https://statsapi.web.nhl.com/api/v1/people/8477474': status was 'Couldn't
#> resolve host name'
#> 02:43:06 | W | nhl_from_json https://statsapi.web.nhl.com/api/v1/people/8477474 error for attempt no: 2 cannot open the connection to 'https://statsapi.web.nhl.com/api/v1/people/8477474'
#> 02:43:06 | E | The following 2 of 2 url retrievals errored:$ https://statsapi.web.nhl.com/api/v1/teams/1$ https://statsapi.web.nhl.com/api/v1/people/8477474
#> [[1]]
#> [1] "https://statsapi.web.nhl.com/api/v1/teams/1"
#> 
#> [[2]]
#> [1] "https://statsapi.web.nhl.com/api/v1/people/8477474"