options(repos = c(CRAN = "https://cran.rstudio.com/"))
install.packages("httr")
## 
## The downloaded binary packages are in
##  /var/folders/01/fn2zbnk50d1c7bvpjbdfvs_w0000gn/T//RtmpwKeUri/downloaded_packages
install.packages("jsonlite")
## 
## The downloaded binary packages are in
##  /var/folders/01/fn2zbnk50d1c7bvpjbdfvs_w0000gn/T//RtmpwKeUri/downloaded_packages
library(httr)
library(jsonlite)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
api_key <- "zfh69Js8SkUX8ajxaTx9JNnsVGtRDBam"

# endpoint
url <- paste0("https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=", api_key)

# GET request
response <- GET(url)
if (status_code(response) == 200) {
  data <- fromJSON(content(response, as = "text"), flatten = TRUE)
  results_df <- as.data.frame(data$results$books)
  print(head(results_df))
} else {
  print("Failed to fetch data")
}
##   rank rank_last_week weeks_on_list asterisk dagger primary_isbn10
## 1    1              0             1        0      0     031656379X
## 2    2              1             4        0      0     0593449592
## 3    3              4            37        0      0     1250178630
## 4    4              3             4        0      0     0374602638
## 5    5              6            50        0      0     1649374178
## 6    6              2             2        0      0     1250355664
##   primary_isbn13                 publisher
## 1  9780316563796             Little, Brown
## 2  9780593449592              Random House
## 3  9781250178633              St. Martin's
## 4  9780374602635 Farrar, Straus and Giroux
## 5  9781649374172                 Red Tower
## 6  9781250355669                   Bramble
##                                                                                                                          description
## 1           The sixth book in the Ballard and Bosch series. Bosch’s daughter, Maddie, becomes a new volunteer on the cold case unit.
## 2 A man in search of the father he never knew encounters a single mom and rumors circulate of the nearby appearance of a white deer.
## 3                   In 1965, a nursing student follows her brother to serve during the Vietnam War and returns to a divided America.
## 4                     After the passing of their father, seemingly different brothers engage in relationships and seek ways to cope.
## 5  The second book in the Empyrean series. Violet Sorrengail’s next round of training might require her to betray the man she loves.
## 6               Astraea must decide whether to sneak in as a substitute in the trials that will determine the safety of her kingdom.
##   price               title             author           contributor
## 1  0.00         THE WAITING   Michael Connelly   by Michael Connelly
## 2  0.00   COUNTING MIRACLES    Nicholas Sparks    by Nicholas Sparks
## 3  0.00           THE WOMEN     Kristin Hannah     by Kristin Hannah
## 4  0.00          INTERMEZZO       Sally Rooney       by Sally Rooney
## 5  0.00          IRON FLAME     Rebecca Yarros     by Rebecca Yarros
## 6  0.00 THE STARS ARE DYING Chloe C. Peñaranda by Chloe C. Peñaranda
##   contributor_note
## 1                 
## 2                 
## 3                 
## 4                 
## 5                 
## 6                 
##                                                             book_image
## 1 https://storage.googleapis.com/du-prd/books/images/9780316563796.jpg
## 2 https://storage.googleapis.com/du-prd/books/images/9780593449592.jpg
## 3 https://storage.googleapis.com/du-prd/books/images/9781250178633.jpg
## 4 https://storage.googleapis.com/du-prd/books/images/9780374602635.jpg
## 5 https://storage.googleapis.com/du-prd/books/images/9781649374172.jpg
## 6 https://storage.googleapis.com/du-prd/books/images/9781250355669.jpg
##   book_image_width book_image_height
## 1              323               500
## 2              330               500
## 3              333               500
## 4              326               500
## 5              309               500
## 6              341               500
##                                          amazon_product_url age_group
## 1 https://www.amazon.com/dp/031656379X?tag=thenewyorktim-20          
## 2 https://www.amazon.com/dp/0593449592?tag=thenewyorktim-20          
## 3 https://www.amazon.com/dp/1250178630?tag=thenewyorktim-20          
## 4 https://www.amazon.com/dp/0374602638?tag=thenewyorktim-20          
## 5 https://www.amazon.com/dp/1649374178?tag=thenewyorktim-20          
## 6 https://www.amazon.com/dp/1250355664?tag=thenewyorktim-20          
##   book_review_link first_chapter_link sunday_review_link article_chapter_link
## 1                                                                            
## 2                                                                            
## 3                                                                            
## 4                                                                            
## 5                                                                            
## 6                                                                            
##                                                                             isbns
## 1 031656379X, 0316580791, 0316563811, 9780316563796, 9780316580793, 9780316563819
## 2                            0593449592, 0593449606, 9780593449592, 9780593449608
## 3                            1250178630, 1250178657, 9781250178633, 9781250178657
## 4                            0374602638, 0374602646, 9780374602635, 9780374602642
## 5                            1649374178, 1705085083, 9781649374172, 9781705085080
## 6                            1250355664, 1250370442, 9781250355669, 9781250370440
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            buy_links
## 1                          Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/031656379X?tag=thenewyorktim-20, https://goto.applebooks.apple/9780316563796?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780316563796, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FTHE%2BWAITING%2FMichael%2BConnelly%2F9780316563796, https://bookshop.org/a/3546/9780316563796
## 2                     Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/0593449592?tag=thenewyorktim-20, https://goto.applebooks.apple/9780593449592?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780593449592, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FCOUNTING%2BMIRACLES%2FNicholas%2BSparks%2F9780593449592, https://bookshop.org/a/3546/9780593449592
## 3                              Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1250178630?tag=thenewyorktim-20, https://goto.applebooks.apple/9781250178633?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781250178633, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FTHE%2BWOMEN%2FKristin%2BHannah%2F9781250178633, https://bookshop.org/a/3546/9781250178633
## 4                                 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/0374602638?tag=thenewyorktim-20, https://goto.applebooks.apple/9780374602635?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780374602635, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FINTERMEZZO%2FSally%2BRooney%2F9780374602635, https://bookshop.org/a/3546/9780374602635
## 5                             Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1649374178?tag=thenewyorktim-20, https://goto.applebooks.apple/9781649374172?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781649374172, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FIRON%2BFLAME%2FRebecca%2BYarros%2F9781649374172, https://bookshop.org/a/3546/9781649374172
## 6 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1250355664?tag=thenewyorktim-20, https://goto.applebooks.apple/9781250355669?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781250355669, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FTHE%2BSTARS%2BARE%2BDYING%2FChloe%2BC.%2BPe%25C3%25B1aranda%2F9781250355669, https://bookshop.org/a/3546/9781250355669
##                                          book_uri
## 1 nyt://book/02a9b619-657e-5ed7-a70e-fd8fb9eba9e6
## 2 nyt://book/974caeee-6bf0-524c-809c-29bfdd99d65f
## 3 nyt://book/9ce735af-71cf-5ff3-a367-43ee07e3fdd7
## 4 nyt://book/a3bcdd05-312a-566b-90ec-df50e894bce6
## 5 nyt://book/d3c570c9-3c3a-5c8b-a740-85ea5e92bfc9
## 6 nyt://book/00d75ee8-72b6-5ca0-a903-a770b5b6901c