Interacting with the Art Institute of Chicago’s API

Author

Jillian Cox

Introduction to the API

The Art Institute of Chicago is one of the oldest and largest museums in the United States. It is home to over 300,000 pieces, containing the largest collection of Impressionist art in the world. The Art Institutes API includes data on collections, shop sales, tours, and mobile publications, websites, etc. This information would be useful for a wide range of users, particularly artists, educators, and researchers. A few uses for this data could be analyzing trends in art over time, integrating virtual galleries, and creating new artistic collections by reimagining existing pieces.

The API is useful for a multitude of reasons. It is most useful when turning the data into a website or application. Since the data is uploaded nightly, it is also useful when you need immediate access to it. Despite its arguable convenience, the AICs API only accepts 60 requests per minute and 10,000 results from a single search.

Setting up the API

The AIC provides their data for free and without an API key. We can access it easily and anonymously through the museums website.

Once we get into R we need to load our packages.

library(tidyverse)
library(jsonlite)
library(httr)
library(magrittr)

Now we can create a URL and retrieve data from the API.

art_api_url <- 
  "https://api.artic.edu/api/v1/artworks"

Define the API URL

The final URL will be a GET request found in the console. It will contain data on our inputs, which are title, id, artist title, is on view and style title.

# Create a url function
create_art_url <- 
  function(art_title, page, limit) {
    
  # Insert endpoint and define parameters
  art_api_endpoint <- 
  "https://api.artic.edu/api/v1/artworks/search"  
  
  art_title <- paste("?q=", art_title, sep = "")
  
  parameters <- 
    paste("&fields=id,title,artist_title,is_on_view,style_title")
  
  page <- paste("&page=", page, sep = "")
  
  limit <- paste("&limit=", limit, sep = "")
  #Final API Call
  aic_api_url <- 
    paste(art_api_endpoint, art_title, parameters, page, limit, sep = "")
  
  return(aic_api_url)
  }
# Test the url function
create_art_url(art_title = "sunday", 
               page = 0,
               limit = 100)
[1] "https://api.artic.edu/api/v1/artworks/search?q=sunday&fields=id,title,artist_title,is_on_view,style_title&page=0&limit=100"

Retrieve Artworks from the URL

Using the URL generating functionabove, we can create another function. This function turns the URL arguments directly into a data frame.

# Retrieve the data frame from the url
request_aic_api_df <- 
  function(art_title, page, limit) {
    aic_api_url <- 
      create_art_url(art_title, page, limit)
  # Use the URL to retrieve the JSON data and create the data frame  
  museum_data <- 
    aic_api_url %>% 
    GET() %>% 
    content(as = "text",
            encoding = "UTF-8") %>% 
    fromJSON() %>% 
    use_series(data) %>% 
    bind_rows()
  
  return(museum_data)
  }

Call the API

Since my favorite painting at the Art Institute is A Sunday Afternoon on the Island of La Grande Jatte, I decided to search for other artworks with “Sunday” in the title. The call returned the first 15 instances of my search, with information on the works artist, style, ect. Since my moms favorite painting is The Old Guitarist, I decided to loop through a data frame to find art with “old” in the name.

# Test the data frame
sunday_art_df <- 
  request_aic_api_df(art_title = "sunday",
                     page = 0,
                     limit = 15)
# Attempted loop
artworks_df <- data.frame()
# Harvest the first 3 pages of the data frame
for (i in 1:3) {
  artworks_df <- 
    request_aic_api_df(art_title = "old",
                       page = i-1,
                       limit = 5) %>% 
    bind_rows(artworks_df)
  
  print(paste("Page", i,"of", 3, "results collected", sep = " "))
  
  if (i<3) {
    Sys.sleep(1)
  }
}
[1] "Page 1 of 3 results collected"
[1] "Page 2 of 3 results collected"
[1] "Page 3 of 3 results collected"