JavaScript · API · Beginner
Query Parameters
Filter, search, sort, and paginate API results using query parameters with URLSearchParams.
Quick topic start and explanations before exercises (exercises below):
Filter, sort, and search — patterns
#Query parameter reference
#Exercises:
Filter by category
#Fetch all products in the "bedroom" category using `URLSearchParams`. Print the count of results and the name of the first product.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// Use URLSearchParams to filter by category='bedroom'
// print count and first product name
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const params = new URLSearchParams({ category: 'bedroom' });
const response = await fetch(`${BASE_URL}/api/products/?${params}`);
const data = await response.json();
console.log(data.count);
console.log(data.results[0].name);
}
main();
Sort by price
#Fetch the 5 cheapest products using `ordering=price` and `page_size=5`. Print each product's name and price.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// ordering=price, page_size=5
// print name and price for each
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const params = new URLSearchParams({ ordering: 'price', page_size: 5 });
const response = await fetch(`${BASE_URL}/api/products/?${params}`);
const data = await response.json();
for (const p of data.results) {
console.log(p.name, p.price);
}
}
main();
Search products
#Search for products matching "oak" using the `search` parameter. Print how many total results were found and list the names.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// search=oak, print count and each name
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const params = new URLSearchParams({ search: 'oak' });
const response = await fetch(`${BASE_URL}/api/products/?${params}`);
const data = await response.json();
console.log(`${data.count} results`);
for (const p of data.results) {
console.log(p.name);
}
}
main();
Combine category and ordering
#Fetch the most expensive "dining-room" products — combine `category=dining-room` and `ordering=-price`. Print the name and price of each result on the first page.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// category=dining-room, ordering=-price
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const params = new URLSearchParams({ category: 'dining-room', ordering: '-price' });
const response = await fetch(`${BASE_URL}/api/products/?${params}`);
const data = await response.json();
for (const p of data.results) {
console.log(p.name, p.price);
}
}
main();
Reusable getProducts() function
#Write a `getProducts(params = {})` function that accepts an object of query parameters and returns the `results` array. Call it three ways: no params, with `category`, and with `search` + `ordering`.
const BASE_URL = 'https://apilearn.tukas.dev';
async function getProducts(params = {}) {
// build URL from params, return results array
}
async function main() {
const all = await getProducts();
console.log('all:', all.length);
const bedroom = await getProducts({ category: 'bedroom' });
console.log('bedroom:', bedroom.length);
const found = await getProducts({ search: 'wood', ordering: 'price' });
console.log('wood asc:', found.length);
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function getProducts(params = {}) {
const qs = new URLSearchParams(params);
const response = await fetch(`${BASE_URL}/api/products/?${qs}`);
const data = await response.json();
return data.results;
}
async function main() {
const all = await getProducts();
console.log('all:', all.length);
const bedroom = await getProducts({ category: 'bedroom' });
console.log('bedroom:', bedroom.length);
const found = await getProducts({ search: 'wood', ordering: 'price' });
console.log('wood asc:', found.length);
}
main();