Projects

Displaying API Data in JavaScript

In our previous lesson, we learned how to fetch data from an API using JavaScript. Now, we’re going to take it a step further by learning how to display this data on our webpage. To make it simple, we'll use a fun and easy API: the Dog CEO's Dog API, which gives us random dog images. Imagine it like having a magic box that gives you a new dog picture every time you ask for it!

By the end of this lesson, you'll understand:

1. Setting Up the HTML

First, let’s set up a basic HTML file. This file will contain a place to show our dog image. Think of this like setting up a blank photo frame where we will put our picture later.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dog Image App</title>
</head>
<body>
<h1>Random Dog Image</h1>
<!-- Placeholder for the dog image -->
<img id="dog-image" alt="Random Dog" width="300" />
<script src="app.js"></script>
</body>
</html>

In this HTML file:

2. Writing the JavaScript to Fetch and Display Data

Next, let’s write some JavaScript to fetch a random dog image from our API and put it into our HTML page. Imagine this like calling a friend (the API) to get a dog picture and then placing that picture into our photo frame (HTML).


const apiUrl = 'https://dog.ceo/api/breeds/image/random';
const dogImageElement = document.getElementById('dog-image');

fetch(apiUrl)
.then(response => response.json())
.then(data => {
dogImageElement.src = data.message;
})
.catch(error => {
console.error('Error fetching data:', error);
});

Let’s break down this JavaScript code:

3. How It All Connects

This lesson builds on our previous knowledge of fetching data:

Think of fetching data as getting a delivery from the store, and displaying data as putting that delivery on display for everyone to see.

4. What Happens When the Code Runs

Here’s a step-by-step of what happens when you load the webpage:

5. Full Example (HTML + JavaScript)

Here’s a complete example that combines HTML and JavaScript:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dog Image App</title>
</head>
<body>
<h1>Random Dog Image</h1>
<img id="dog-image" alt="Random Dog" width="300" />
<script>
const apiUrl = 'https://dog.ceo/api/breeds/image/random';
const dogImageElement = document.getElementById('dog-image');

fetch(apiUrl)
.then(response => response.json())
.then(data => {
dogImageElement.src = data.message;
})
.catch(error => {
console.error('Error fetching data:', error);
});
</script>
</body>
</html>

6. Key Takeaways

With these skills, you can now fetch and display data from APIs, making your web pages more engaging and responsive to real-time information.

7. Practice Challenge

Try using a different API to fetch and display other types of data, such as random quotes or fun facts. This will help you get comfortable with working with different kinds of data and APIs.