Fetch One Real Artwork

So far, every artwork in the planning script has come from the kiosk stand-in, which answers from a few records after a fixed delay. The real museum, the Art Institute of Chicago, has a public API that a program can access through a URL to request data in JSON. Anyone can use it without an account or a key.

In this lesson, you’ll ask the museum about Grandma Bisi’s pick, artwork 27992, and log its title, artist, and date. You’ll use the same await you used with the kiosk, twice. Start by sending the request.

Ask the Museum for Grandma Bisi’s Pick

Node.js has a built-in function for asking a server for data, called fetch(). You give it a URL, and it returns a promise, so you await it like a kiosk lookup. Save this as fetch_artwork.mjs in your museum folder:

Language: JavaScript Filename: fetch_artwork.mjs
const url =
  "https://api.artic.edu/api/v1/artworks/27992" +
  "?fields=id,title,artist_title,date_display";

const response = await fetch(url);
const body = await response.json();
const data = body.data;

console.log(data.title);
console.log(data.artist_title);
console.log(data.date_display);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.