Uses the NYT Books API to fetch the current Hardcover Fiction best-seller list, parse the JSON, and transform it into a tidy DataFrame.

1 1) Setup (minimal memory)

# Install once if needed:
# install.packages(c("httr","jsonlite"))
library(httr)
library(jsonlite)

2 2) API key (keep it out of the file you submit)

# Set your key in the Console (not here):
# Sys.setenv(NYT_API_KEY = "YOUR_KEY_HERE")

api_key <- Sys.getenv("NYT_API_KEY", unset = NA)
if (is.na(api_key) || nchar(api_key) == 0) {
  stop("API key not found. In the Console, run: Sys.setenv(NYT_API_KEY = 'YOUR_KEY_HERE')")
}

3 3) Fetch the Books list (Hardcover Fiction)

url <- "https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json"

# Try query param; if 401, fall back to header
resp <- GET(url, query = list(`api-key` = api_key))
if (status_code(resp) == 401) {
  resp <- GET(url, add_headers(`api-key` = api_key))
}

sc <- status_code(resp)
cat("HTTP status:", sc, "\n")
## HTTP status: 200
raw_text <- content(resp, as="text", encoding="UTF-8")
if (sc != 200) {
  cat("Response preview (first 400 chars):\n", substr(raw_text, 1, 400), "\n")
  stop("Request did not succeed. See status & preview above.")
}
nchar(raw_text)
## [1] 23384

4 4) Parse JSON → DataFrame

json <- fromJSON(raw_text, flatten = TRUE)

if (!is.null(json$results$books) && NROW(json$results$books) > 0) {
  books <- subset(json$results$books, select = c(
    "rank","title","author","publisher","primary_isbn13",
    "weeks_on_list","amazon_product_url","description"
  ))
} else {
  books <- data.frame()
}
head(books, 10)

5 5) Save CSV (artifact)

out_file <- "nyt_books_hardcover_fiction.csv"
write.csv(books, out_file, row.names = FALSE)
paste("Wrote:", out_file, "with", nrow(books), "rows")
## [1] "Wrote: nyt_books_hardcover_fiction.csv with 15 rows"

6 6) Notes