JavaScript · API · Beginner
Working with Categories
Fetch and explore product categories, then use them to filter the product list.
Quick topic start and explanations before exercises (exercises below):
Practical patterns with categories
#Category endpoints reference
#Exercises:
List all categories
#const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// fetch /api/categories/ and print count + names
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/categories/`);
const data = await response.json();
console.log(data.count);
for (const cat of data.results) {
console.log(cat.name);
}
}
main();
Find a category by name
#const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// find the "Bedroom" category and print id, name, slug
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/categories/`);
const data = await response.json();
const bedroom = data.results.find(c => c.name === 'Bedroom');
console.log(bedroom.id);
console.log(bedroom.name);
console.log(bedroom.slug);
}
main();
Products in a category
#const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// 1. get categories (use data.results)
// 2. use first category slug to filter products
// 3. print category name and product count
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const catResponse = await fetch(`${BASE_URL}/api/categories/`);
const catData = await catResponse.json();
const first = catData.results[0];
const params = new URLSearchParams({ category: first.slug });
const prodResponse = await fetch(`${BASE_URL}/api/products/?${params}`);
const prodData = await prodResponse.json();
console.log(first.name);
console.log(prodData.count);
}
main();
Count products per category
#const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// for each category in data.results: fetch count and print "Name: N products"
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const catResponse = await fetch(`${BASE_URL}/api/categories/`);
const catData = await catResponse.json();
for (const cat of catData.results) {
const params = new URLSearchParams({ category: cat.slug, page_size: 1 });
const prodResponse = await fetch(`${BASE_URL}/api/products/?${params}`);
const data = await prodResponse.json();
console.log(`${cat.name}: ${data.count} products`);
}
}
main();
Build a category slug to name map
#const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// build { slug: name } map from data.results
// print the map and look up 'bedroom'
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const response = await fetch(`${BASE_URL}/api/categories/`);
const data = await response.json();
const categoryMap = Object.fromEntries(data.results.map(c => [c.slug, c.name]));
console.log(categoryMap);
console.log(categoryMap['bedroom']);
}
main();