Practice and reinforce the concepts from Lesson 1
In this activity, you'll:
Total time: 45-60 minutes
What you'll do: Access the project template Time: 5 minutes
Access the template here: Dog API Explorer Template
git clone
the repository and navigate to the template folderindex.html
- The webpage structurescript.js
- Where you'll write JavaScriptstyles.css
- Basic styling (already done)README.md
- Instructions and documentation💡 Tip: Open
index.html
directly in your browser to start working!
What you'll do: Explore APIs using just your browser Time: 10 minutes
Choose 2-3 APIs from these fun options and test them in new browser tabs:
https://catfact.ninja/fact
- Random cat factshttps://dog.ceo/api/breeds/image/random
- Random dog photoshttps://pokeapi.co/api/v2/pokemon/pikachu
- Pokemon datahttps://official-joke-api.appspot.com/random_joke
- Setup and punchlinehttps://www.boredapi.com/api/activity
- Things to do when boredhttps://swapi.dev/api/people/1
- Star Wars character datahttp://numbersapi.com/42
- Interesting facts about numbershttps://catfact.ninja/fact
- Random cat triviahttps://api.quotable.io/random
- Inspirational quotes💡 Tip Notice how each API returns different data structure. Some have single values, others have nested objects! Pick the ones that interest you most.
What you'll do: Fetch data using JavaScript Time: 15 minutes
Choose ONE API from Step 1 and implement it. Here are examples for different choices:
// Function to get a cat fact
async function getCatFact() {
document.getElementById('result').innerHTML = 'Loading cat fact...';
try {
const response = await fetch('https://catfact.ninja/fact');
const data = await response.json();
document.getElementById('result').innerHTML = `
<div class="fact-card">
<h3>🐱 Cat Fact</h3>
<p>${data.fact}</p>
<small>Length: ${data.length} characters</small>
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Oops! Could not load cat fact.';
console.error('Error:', error);
}
}
// Function to get Pokemon data
async function getPokemon() {
document.getElementById('result').innerHTML = 'Loading Pokemon...';
try {
const response = await fetch('https://pokeapi.co/api/v2/pokemon/pikachu');
const data = await response.json();
document.getElementById('result').innerHTML = `
<div class="pokemon-card">
<h3>⚡ ${data.name.charAt(0).toUpperCase() + data.name.slice(1)}</h3>
<img src="${data.sprites.front_default}" alt="${data.name}">
<p>Height: ${data.height} | Weight: ${data.weight}</p>
<p>Type: ${data.types[0].type.name}</p>
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Oops! Could not load Pokemon.';
console.error('Error:', error);
}
}
// Function to get Star Wars character
async function getStarWarsCharacter() {
document.getElementById('result').innerHTML = 'Loading character...';
try {
const response = await fetch('https://swapi.dev/api/people/1');
const data = await response.json();
document.getElementById('result').innerHTML = `
<div class="character-card">
<h3>🌟 ${data.name}</h3>
<p>Height: ${data.height}cm</p>
<p>Hair Color: ${data.hair_color}</p>
<p>Birth Year: ${data.birth_year}</p>
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Oops! Could not load character.';
console.error('Error:', error);
}
}
What you'll do: Create functions for different APIs Time: 15 minutes
Add 1-2 more APIs from your browser testing. Mix and match based on your interests:
Random Jokes API:
async function getJoke() {
document.getElementById('result').innerHTML = 'Loading joke...';
try {
const response = await fetch('https://official-joke-api.appspot.com/random_joke');
const data = await response.json();
document.getElementById('result').innerHTML = `
<div class="joke-card">
<h3>😄 Random Joke</h3>
<p class="setup">${data.setup}</p>
<p class="punchline">...${data.punchline}!</p>
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Could not load joke.';
}
}
Dog Images API:
async function getDogImage() {
document.getElementById('result').innerHTML = 'Loading dog photo...';
try {
const response = await fetch('https://dog.ceo/api/breeds/image/random');
const data = await response.json();
document.getElementById('result').innerHTML = `
<div class="dog-card">
<h3>🐕 Random Dog</h3>
<img src="${data.message}" alt="Random dog" style="max-width: 300px; border-radius: 10px;">
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Could not load dog image.';
}
}
Activity Suggestions API:
async function getActivity() {
document.getElementById('result').innerHTML = 'Finding something to do...';
try {
const response = await fetch('https://www.boredapi.com/api/activity');
const data = await response.json();
document.getElementById('result').innerHTML = `
<div class="activity-card">
<h3>💡 Activity Suggestion</h3>
<p>${data.activity}</p>
<p>Type: ${data.type} | Participants: ${data.participants}</p>
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Could not load activity.';
}
}
What you'll do: Show the actual API response Time: 10 minutes
Create a function to show raw JSON from any of your chosen APIs:
// Function to show raw JSON data (update the URL to match your choice)
async function showRawData() {
document.getElementById('result').innerHTML = 'Loading raw data...';
try {
// Replace this URL with any API you've been using
const response = await fetch('https://catfact.ninja/fact');
const data = await response.json();
// Display formatted JSON
document.getElementById('result').innerHTML = `
<div class="raw-data">
<h3>📋 Raw JSON Response</h3>
<pre>${JSON.stringify(data, null, 2)}</pre>
</div>
`;
} catch (error) {
document.getElementById('result').innerHTML = 'Could not load data.';
}
}
💡 Tip
JSON.stringify(data, null, 2)
formats the JSON with 2-space indentation to make it readable! Try different APIs to see how data structures vary.
What you'll do: Practice what you learned Time: 10 minutes
Choose 1-2 challenges based on your API preferences:
For Pokemon fans:
https://pokeapi.co/api/v2/pokemon/charmander
https://pokeapi.co/api/v2/pokemon/${Math.floor(Math.random() * 150) + 1}
For number lovers:
http://numbersapi.com/42/trivia?json
42
to any number you want!Dog Pictures:
https://dog.ceo/api/breeds/image/random
<img>
tagPokemon Images:
data.sprites.front_default
Test what happens when APIs fail:
https://catfact.ninja/wrong
https://pokeapi.co/api/v2/pokemon/invalidname
When complete, your API Explorer should:
Before submitting:
Submit your StackBlitz link when complete!
If you finish early, try these:
Great work exploring APIs! Next lesson, we'll learn about parsing more complex JSON data and working with different response formats. 🚀