This script demonstrates how to access and process data from the New York Times Top Stories API using the httr2 package in R. The goal is to retrieve JSON content from a selected section, transform it into a tidy DataFrame, and prepare it for analysis or export.
Load the necessary tools for making HTTP requests and handling JSON data.
# Load libraries
library(httr2)
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
library(tibble)
library(purrr)
##
## Attaching package: 'purrr'
## The following object is masked from 'package:jsonlite':
##
## flatten
Retrieve your API key to authenticate requests to the NYT API.
# Loading the API key from .Renviron
api_key <- Sys.getenv("NYT_API_KEY")
We built the URL dynamically and sent the request to the NYT server.
# Choosing a section
section <- "science"
# Constructing the request
req <- request(paste0("https://api.nytimes.com/svc/topstories/v2/", section, ".json")) |>
req_url_query("api-key" = api_key)
# Send the request
resp <- req_perform(req)
# Check response status
resp_status(resp)
## [1] 200
Parse the JSON response and convert it into a structured R DataFrame.
# Extract JSON content from the response
json_data <- resp_body_json(resp)
# Convert the list of articles to a DataFrame
nyt_df <- map_dfr(json_data$results, function(article) {
tibble(
title = article$title %||% NA,
abstract = article$abstract %||% NA,
url = article$url %||% NA,
byline = article$byline %||% NA,
published_date = article$published_date %||% NA,
section = article$section %||% NA
)
})
# Preview the result
head(nyt_df)
## # A tibble: 6 × 6
## title abstract url byline published_date section
## <chr> <chr> <chr> <chr> <chr> <chr>
## 1 "Sign Up for the Science Times N… "Every … "nul… "" 2016-02-05T18… science
## 2 "" "" "" "" 2015-04-16T14… admin
## 3 "Exxon Sues California Over New … "The oi… "htt… "By K… 2025-10-25T11… climate
## 4 "An E.P.A. Plan to Kill a Major … "Some c… "htt… "By K… 2025-10-25T05… climate
## 5 "Take a Look at Rare Photos of R… "Three … "htt… "By F… 2025-10-25T04… science
## 6 "In Fight Against Malaria, an Un… "Treati… "htt… "By S… 2025-10-25T00… health
View(nyt_df)
This script successfully connects to the New York Times Top Stories API, retrieves JSON data, and transforms it into a tidy R DataFrame. The result is a clean dataset containing article metadata such as title, abstract, author, and publication date. This workflow can be adapted to other NYT APIs or extended with visualizations, text analysis, or interactive dashboards.