The New York Times Books API provides information about books on The New York Times Best Sellers lists, including book titles, authors, rankings, and the number of weeks each book has appeared on a list.
For this assignment, I will use the Books API to explore the Hardcover Fiction Best Sellers list. My goal is to identify the top 10 best-selling books and examine which books have spent the most weeks on the current list.
I will focus on two research questions:
What are the top 10 best-selling hardcover fiction books on The New York Times Best Sellers list?
Which books have spent the most weeks on The New York Times Hardcover Fiction Best Sellers list?
I will use R to retrieve the data, process the JSON response, and organize the results into a clean data frame to answer these questions.
Planned Approach
I will use the New York Times Books API to retrieve the latest available Hardcover Fiction Best Sellers list.
API Endpoint: https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json
I will send a GET request in R using an API key stored in an environment variable instead of including it directly in the code.
After retrieving the data, I will use the jsonlite package to parse the JSON response and extract the book information. I will create a tidy data frame containing book titles, authors, rankings, and the number of weeks each book has spent on the list.
I will check for missing values, empty strings, and data types to make sure the data is ready for analysis. I will select only the relevant columns and arrange the data based on the research questions.
To answer the first question, I will identify the top 10 books based on their current rankings. For the second question, I will compare the number of weeks each book has spent on the list to identify the longest-running bestsellers among the books in the current list.
Finally, I will summarize the findings and use the results to answer both research questions.
Anticipated Challenges
One possible challenge will be connecting to the New York Times Books API and making sure the API request works correctly. I will need to store my API key securely and check the response status to identify any potential errors.
Another challenge may be extracting the necessary information from the nested fields in the JSON response. I will need to select the relevant columns and organize the data into a tidy data frame.
I will also need to check for missing values and incorrect data types to make sure the data is suitable for analysis.
Code Base
Load Required Packages
In this section, I will load the R packages needed to work with the New York Times Books API. I will use httr to send API requests, jsonlite to process JSON data, and dplyr to clean and organize the data.
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 Authentication
I will retrieve my New York Times API key from an environment variable using Sys.getenv(). This allows me to authenticate API requests without including the key directly in my code.
api_key <-Sys.getenv("NYT_API_KEY")if (api_key =="") {stop("NYT_API_KEY is not set.")}
Request Data from the NYT Books API
I will use the New York Times Books API to retrieve the latest available Hardcover Fiction Best Sellers list. I will send a GET request using the httr package and include my API key as a query parameter. I will also check the response status to make sure the request was successful.
The endpoint uses current to retrieve the latest available list and hardcover-fiction to select the Hardcover Fiction category.
I used the api-key query parameter for authentication. The API key was stored in an environment variable instead of being included directly in the code.
The API request returned an HTTP status code of 200, indicating that the request was successful.
Parse the JSON Response
In this section, I will convert the JSON response from the New York Times Books API into an R object using the jsonlite package. This will allow me to access the book information for further processing and analysis.
nyt_data <-fromJSON(content(response, as ="text", encoding ="UTF-8"))
I will examine the structure of the JSON data to understand how the information is organized and identify the fields needed for my analysis.
str(nyt_data, max.level =2)
List of 5
$ status : chr "OK"
$ copyright : chr "Copyright (c) 2026 The New York Times Company. All Rights Reserved."
$ num_results : int 15
$ last_modified: chr "2026-10-28T18:01:24Z"
$ results :List of 12
..$ display_name : chr "Hardcover Fiction"
..$ list_name : chr "Hardcover Fiction"
..$ list_name_encoded : chr "hardcover-fiction"
..$ previous_published_date: chr "2026-11-01"
..$ published_date : chr "2026-11-08"
..$ bestsellers_date : chr "2026-10-24"
..$ normal_list_ends_at : int 15
..$ updated : chr "WEEKLY"
..$ list_id : int 1
..$ uri : chr "nyt://bestsellerslist/08cd31e0-67da-5319-9a9e-2f575cbbd839"
..$ books :'data.frame': 15 obs. of 28 variables:
..$ corrections : list()
Examine the Book Data
I will extract the book data from the JSON response and examine its column names. This will help me identify the variables needed to answer my research questions.
I will select the columns needed for my analysis: book title, author, rank, and weeks on the bestseller list. I will then arrange the books by their current rankings.
title author rank
1 THRESHING DAY Rebecca Yarros 1
2 MISS WOLCOTT'S GHOST Louise Penny 2
3 A MARRIED LITTLE CHRISTMAS Abby Jimenez 3
4 THE FAMILY CROSS James Patterson 4
5 CHAIN REACTION Lee Child and Andrew Child 5
6 THE FRENCH ILLUSION John Grisham 6
7 THE WITCH Freida McFadden 7
8 WITH HEARTS OF FLAME Briar Boleyn 8
9 THE CALAMITY CLUB Kathryn Stockett 9
10 PARTITA Barbara Kingsolver 10
11 HOLLOW BONES Jodi Picoult 11
12 PECK & PECK Bonnie Garmus 12
13 THE DUNGEON ANARCHIST'S COOKBOOK Matt Dinniman 13
14 THE GATE OF THE FERAL GODS Matt Dinniman 14
15 MERCY'S CHILD Richard Paul Evans 15
weeks_on_list
1 4
2 1
3 1
4 1
5 1
6 4
7 3
8 1
9 25
10 3
11 6
12 2
13 32
14 21
15 1
Check for Missing Values
I will check for missing values in each column of the cleaned data frame to identify any incomplete information.
colSums(is.na(books_clean))
title author rank weeks_on_list
0 0 0 0
I will check for empty strings in the text columns. Empty strings may represent missing information even when they are not identified as NA values.
colSums(books_clean[c("title", "author")] =="")
title author
0 0
Check Data Types
I will examine the data types of each column to make sure that book titles and authors are stored as text, while rankings and weeks on the list are stored as numeric values.
str(books_clean)
'data.frame': 15 obs. of 4 variables:
$ title : chr "THRESHING DAY" "MISS WOLCOTT'S GHOST" "A MARRIED LITTLE CHRISTMAS" "THE FAMILY CROSS" ...
$ author : chr "Rebecca Yarros" "Louise Penny" "Abby Jimenez" "James Patterson" ...
$ rank : int 1 2 3 4 5 6 7 8 9 10 ...
$ weeks_on_list: int 4 1 1 1 1 4 3 1 25 3 ...
Data Cleaning Summary
The original API response contained nested JSON data. I extracted the book information and selected four columns relevant to my research questions: title, author, rank, and weeks_on_list.
I checked for missing values and empty strings, but none were found. I also verified the data types. Book titles and authors were stored as character values, while rankings and weeks on the list were stored as integers. Therefore, no missing-value removal or data type conversion was necessary.
Top 10 Best-Selling Hardcover Fiction Books
I will identify the top 10 best-selling hardcover fiction books based on their current rankings. I will use the cleaned data frame and select the first 10 books from the list.
title author rank weeks_on_list
1 THRESHING DAY Rebecca Yarros 1 4
2 MISS WOLCOTT'S GHOST Louise Penny 2 1
3 A MARRIED LITTLE CHRISTMAS Abby Jimenez 3 1
4 THE FAMILY CROSS James Patterson 4 1
5 CHAIN REACTION Lee Child and Andrew Child 5 1
6 THE FRENCH ILLUSION John Grisham 6 4
7 THE WITCH Freida McFadden 7 3
8 WITH HEARTS OF FLAME Briar Boleyn 8 1
9 THE CALAMITY CLUB Kathryn Stockett 9 25
10 PARTITA Barbara Kingsolver 10 3
The results show that “Threshing Day” by Rebecca Yarros ranks first on The New York Times Hardcover Fiction Best Sellers list, followed by “Miss Wolcott’s Ghost” by Louise Penny and “A Married Little Christmas” by Abby Jimenez.
Among the top 10 books, “The Calamity Club” by Kathryn Stockett has spent the most time on the list, with 25 weeks, despite ranking ninth. Several other books have appeared on the list for only one week.
These findings suggest that a book’s current ranking does not necessarily reflect how long it has remained on the bestseller list.
Books with the Most Weeks on the Bestseller List
I will compare the number of weeks each book has spent on the Hardcover Fiction Best Sellers list. I will sort the books by weeks_on_list in descending order to identify which books have remained on the list the longest.
title author rank
1 THE DUNGEON ANARCHIST'S COOKBOOK Matt Dinniman 13
2 THE CALAMITY CLUB Kathryn Stockett 9
3 THE GATE OF THE FERAL GODS Matt Dinniman 14
4 HOLLOW BONES Jodi Picoult 11
5 THRESHING DAY Rebecca Yarros 1
6 THE FRENCH ILLUSION John Grisham 6
7 THE WITCH Freida McFadden 7
8 PARTITA Barbara Kingsolver 10
9 PECK & PECK Bonnie Garmus 12
10 MISS WOLCOTT'S GHOST Louise Penny 2
11 A MARRIED LITTLE CHRISTMAS Abby Jimenez 3
12 THE FAMILY CROSS James Patterson 4
13 CHAIN REACTION Lee Child and Andrew Child 5
14 WITH HEARTS OF FLAME Briar Boleyn 8
15 MERCY'S CHILD Richard Paul Evans 15
weeks_on_list
1 32
2 25
3 21
4 6
5 4
6 4
7 3
8 3
9 2
10 1
11 1
12 1
13 1
14 1
15 1
The results show that “The Dungeon Anarchist’s Cookbook” by Matt Dinniman has spent the most time on the current Hardcover Fiction Best Sellers list, with 32 weeks, followed by “The Calamity Club” by Kathryn Stockett with 25 weeks and “The Gate of the Feral Gods” by Matt Dinniman with 21 weeks.
Interestingly, these three books are currently ranked 13th, 9th, and 14th, respectively. In comparison, the number-one bestseller, “Threshing Day” by Rebecca Yarros, has spent only four weeks on the list.
These findings suggest that books can remain on the bestseller list for many weeks even when they are not among the highest-ranked titles. Current rankings and weeks on the list provide different information about a book’s bestseller performance.
Conclusion
In this assignment, I used the New York Times Books API to retrieve and analyze the latest available Hardcover Fiction Best Sellers list. I successfully collected the data, parsed the JSON response, and created a clean data frame in R.
The results showed that “Threshing Day” by Rebecca Yarros ranked first, while “The Dungeon Anarchist’s Cookbook” by Matt Dinniman had spent the most weeks on the current list, with 32 weeks.
Overall, the analysis showed that a book’s current ranking does not necessarily reflect how long it has remained on the bestseller list. The Books API provided a useful way to explore these differences using real-world data.
Citation
I used ChatGPT (GPT-5.6 Luna), developed by OpenAI, to help me understand some of the functions I used in this assignment and to check my work.
OpenAI. (2026). ChatGPT (GPT-5.6 Luna) [Large language model]. [https://chatgpt.com/ Accessed October 29, 2026].