Working with APIs in R

Instruction: Demonstrate how to connect to and consume data from an API in R.

Context: This question assesses the candidate's ability to integrate R with external data sources via APIs, a useful skill for accessing and analyzing live data.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

Build an HTTPS request with encoded query parameters, bounded timeouts and explicit response validation. Keep authentication values in an approved secret source and use the API's required header scheme; do not put secrets into printed URLs.

fetch_rows <- function(endpoint) { stopifnot(is.character(endpoint), length(endpoint) == 1L, !is.na(endpoint), startsWith(endpoint, "https://")) response <- httr2::request(endpoint) |> httr2::req_url_query(limit = 100) |> httr2::req_timeout(10) |> httr2::req_perform() body <- httr2::resp_body_json(response,...

Related Questions