Picking an API

After getting an API key from developer.nytimes.com/apis, I have opted to use the Article Search API. I want to know about all the NYTimes articles concerning the very center of the universe: Absolute Bagels on the UWS. When I left NYC, I would sometimes wake up from bagel dreams, chewing on a wet pillow. Maybe this will help me relive my everything-double-toasted-with-butter days.

library(httr)
library(jsonlite)

Building an API call

An API call consists of a few necessary parts, and many optional ones. Here’s an example:

GET https://api.nytimes.com/svc/search/v2/articlesearch.json?q=%22absolute%20bagels%22&sort=relevance&api-key=XXXXXXXX
  1. the base URL
  2. the query string
  3. “&api-key=”
  4. the API key

In our example, we sort the results by relevance, denoted by sort=relevance, and we are just taking the default search results page.

We can construct it like this:

base <- "https://api.nytimes.com/svc/search/v2/articlesearch.json?q="
query <- "%22absolute%20bagels%22"
sort <- "&sort=relevance"
ak <- "&api-key="
key <- "rFlQcW68K4OTRn8dpDTy6CeWL1qk23jj"

q <- paste0(base, query, sort, ak, key)

We use paste0() instead of paste() because we do not need a separator, and it’s a tiny bit more efficient.

Let’s make the API call and save the content we are served:

req <- GET(q)
cont <- content(req, 'text')
json_data <- fromJSON(cont)

articles <- json_data$response$docs

Let’s check out the abstracts……..

articles$abstract
##  [1] "Ed Levine article comments on his search for best bagels in New York City; cites six sources for traditional and excellent bagels; photos (L)"                                                                                                                                                
##  [2] "The founder and chief executive of Teach for America spends her Sundays catching up on the week and on sleep, but still rises by 6 a.m."                                                                                                                                                      
##  [3] "GEOGRAPHICALLY speaking, West 72nd Street isn’t quite the center of the Upper West Side. But any street where a person can buy kosher barbecue, kosher pizza and kosher sushi, not to mention brisket, latkes and matzo ball soup, would seem to have its Jewish culinary bases well covered."
##  [4] "A self-taught baker in New Jersey is helping entrepreneurs all over the world, many with not even a schmear of experience, to open bagel shops."                                                                                                                                              
##  [5] "Why the author of the prize-winning children’s book “When You Reach Me” sets aside Sunday as a time for not writing."                                                                                                                                                                         
##  [6] "An Upper West Side native laments the closing of an iconic bagel store in a neighborhood that already lost other fixtures of daily life."                                                                                                                                                     
##  [7] "While Sarah Gelfand and Scott Schilson’s graduate law program at N.Y.U. kept them busy, they still managed to fall in love while sampling New York restaurants and bakeries."                                                                                                                 
##  [8] "A lot of tea is involved. There is a walk in the North Woods of Central Park. Ms. Fire also looks for inspiration in art galleries and Chinatown."                                                                                                                                            
##  [9] "And what else you need to know today."                                                                                                                                                                                                                                                        
## [10] "Who better than the father of “Seinfeld” and “Curb Your Enthusiasm” to guide us through the thicket of being together, alone?"