Python · API · Beginner

Working with Categories

5 tasks

Fetch and explore product categories, then use them to filter the product list.

The categories endpoint

#
The categories endpoint gives you a list of all product groups in the catalog. There are two ways to use it: List all categories: ```python import requests BASE_URL = 'https://apilearn.tukas.dev' response = requests.get(f'{BASE_URL}/api/categories/') data = response.json() print(data['count']) # 13 print(data['results']) # list of category objects ``` Fetch a single category by its slug: ```python response = requests.get(f'{BASE_URL}/api/categories/kitchen/') category = response.json() print(category['id']) # 7 print(category['name']) # 'Kitchen' print(category['slug']) # 'kitchen' ``` Each category object has three fields: id (integer), name (display string), and slug (the lowercase hyphenated identifier used in URLs and filter params). The slug is the key connection point: it is exactly the value you pass to the category= query parameter when filtering products. So after fetching the category list, you can immediately use any slug to query products in that category without hardcoding anything: ```python categories = requests.get(f'{BASE_URL}/api/categories/').json()['results'] for cat in categories: count = requests.get( f'{BASE_URL}/api/products/', params={'category': cat['slug']}, ).json()['count'] print(f"{cat['name']}: {count} products") ``` This pattern — fetch a list of identifiers, then use them as parameters in other requests — is fundamental in REST API work.

Practical patterns with categories

#
Build a slug-to-name lookup dict from the category list: ```python response = requests.get(f'{BASE_URL}/api/categories/') categories = response.json()['results'] slug_to_name = {cat['slug']: cat['name'] for cat in categories} print(slug_to_name) # {'kitchen': 'Kitchen', 'bedroom': 'Bedroom', ...} ``` This is handy when you have a slug from a product response and want to display the human-readable name without making an extra request per product. Find the category with the most products: ```python response = requests.get(f'{BASE_URL}/api/categories/') categories = response.json()['results'] best = None best_count = 0 for cat in categories: count = requests.get( f'{BASE_URL}/api/products/', params={'category': cat['slug'], 'page_size': 1}, ).json()['count'] if count > best_count: best_count = count best = cat['name'] print(f'{best}: {best_count} products') ``` Notice page_size=1 — you only need the count field, not the actual products. Requesting a single item per page keeps the response small and fast. Print names of all products in a given category: ```python slug = 'lighting' url = f'{BASE_URL}/api/products/' params = {'category': slug} while url: data = requests.get(url, params=params).json() for product in data['results']: print(product['name']) url = data['next'] params = {} ```

Category endpoints reference

#
Category endpoints: ``` GET /api/categories/ List all 13 categories (paginated) GET /api/categories/{slug}/ Single category by slug ``` Category object fields: ``` id int Numeric identifier name string Display name (e.g. 'Kitchen') slug string URL key (e.g. 'kitchen') — use this in params ``` Available category slugs: ``` all, kitchen, bedroom, living-room, office, hardware, decor, bathroom, kids-room, outdoor, dining-room, storage, lighting ``` Note: `all` is a special category that returns all products — equivalent to calling /api/products/ with no category filter. Connecting categories to products: ```python # Use the slug as the category= filter parameter requests.get(f'{BASE_URL}/api/products/', params={'category': cat['slug']}) ```
01

List all categories

#

Fetch the category list and print the name of every category, one per line.

import requests

BASE_URL = 'https://apilearn.tukas.dev'

# Fetch all categories and print each name
Solution
import requests

BASE_URL = 'https://apilearn.tukas.dev'

response = requests.get(f'{BASE_URL}/api/categories/')
for cat in response.json()['results']:
    print(cat['name'])
02

Build a slug-to-name mapping

#

Fetch all categories and build a dict that maps each slug to its display name. Print the dict.

import requests

BASE_URL = 'https://apilearn.tukas.dev'

# Build {slug: name} dict and print it
Solution
import requests

BASE_URL = 'https://apilearn.tukas.dev'

response = requests.get(f'{BASE_URL}/api/categories/')
categories = response.json()['results']
slug_to_name = {cat['slug']: cat['name'] for cat in categories}
print(slug_to_name)
03

Fetch a single category

#

Fetch the "outdoor" category by its slug and print its id, name, and slug.

import requests

BASE_URL = 'https://apilearn.tukas.dev'

# Fetch the outdoor category and print id, name, slug
Solution
import requests

BASE_URL = 'https://apilearn.tukas.dev'

response = requests.get(f'{BASE_URL}/api/categories/outdoor/')
cat = response.json()
print(cat['id'])
print(cat['name'])
print(cat['slug'])
04

Count products per category

#

For each category, print its name and the total number of products it contains. Format each line as: "Category name: N products".

import requests

BASE_URL = 'https://apilearn.tukas.dev'

# For each category, fetch the product count and print it
Solution
import requests

BASE_URL = 'https://apilearn.tukas.dev'

categories = requests.get(f'{BASE_URL}/api/categories/').json()['results']

for cat in categories:
    count = requests.get(
        f'{BASE_URL}/api/products/',
        params={'category': cat['slug'], 'page_size': 1},
    ).json()['count']
    print(f"{cat['name']}: {count} products")
05

Find the largest category

#

Find the category that contains the most products and print its name and product count.

import requests

BASE_URL = 'https://apilearn.tukas.dev'

# Find the category with the most products
Solution
import requests

BASE_URL = 'https://apilearn.tukas.dev'

categories = requests.get(f'{BASE_URL}/api/categories/').json()['results']

counts = []
for cat in categories:
    count = requests.get(
        f'{BASE_URL}/api/products/',
        params={'category': cat['slug'], 'page_size': 1},
    ).json()['count']
    counts.append((count, cat['name']))

best_count, best_name = max(counts)
print(f'{best_name}: {best_count} products')