Show a photo, offer four breed names, and keep score, using the Dog Breed API.
Subscribe to the free BASIC plan on the RapidAPI listing, then copy your key from the RapidAPI dashboard. Requests go to dogbreeddb.p.rapidapi.com. In a real app, call the API from your server so the key never reaches the browser; the snippets below use YOUR_KEY for brevity.
The /paginated/ endpoint returns 10 breeds per page as {count, next, previous, results}. Each result has a breedName and an imgThumb image URL. Use ?page=N to move through the pages.
const HEADERS = { 'x-rapidapi-key': 'YOUR_KEY', 'x-rapidapi-host': 'dogbreeddb.p.rapidapi.com' };
const BASE = 'https://dogbreeddb.p.rapidapi.com';
async function getPage(page, extra = '') {
const res = await fetch(`${BASE}/paginated/?page=${page}${extra}`, { headers: HEADERS });
const data = await res.json();
return data.results.filter(b => b.imgThumb); // the quiz needs a picture
}
Pick one breed as the answer, add three other names as wrong options, and shuffle them. A page has at most 10 breeds and some have no image, so check that you have at least 4 before building a question, and fetch another page if not. The breedType filter in the next step narrows the list further, so the same check applies there.
function shuffle(a) { return a.sort(() => Math.random() - 0.5); }
function makeQuestion(breeds) {
const [answer, ...others] = shuffle([...breeds]);
const options = shuffle([answer, ...others.slice(0, 3)]).map(b => b.breedName);
return { image: answer.imgThumb, answer: answer.breedName, options };
}
const breeds = await getPage(1);
const q = makeQuestion(breeds);
document.querySelector('#photo').src = q.image;
const choices = document.querySelector('#choices');
choices.replaceChildren(...q.options.map(name => {
const b = document.createElement('button');
b.dataset.name = name;
b.textContent = name; // textContent keeps API data out of the markup
return b;
}));
Filter by breedType (an exact match, such as a breed group) so the wrong options look similar to the right one. Use the values you see in the breedType field of the results.
const first = (await getPage(1))[0];
const sameGroup = await getPage(1, `&breedType=${encodeURIComponent(first.breedType)}`);
const hard = makeQuestion(sameGroup);
Some images are Creative Commons and need credit. Show each breed's imgAttribution next to its photo when imgCreativeCommons is true.
let score = 0;
document.querySelector('#choices').addEventListener('click', e => {
if (e.target.dataset.name === q.answer) score++;
// load the next page and call makeQuestion again
});
Building a similar quiz for cats? See the Cat Breed API, which has the same endpoints and filters.