Python · API · Beginner

Query Parameters

6 tasks

Filter, search, sort, and paginate API results using query parameters.

Filtering and sorting with params={}

#
Query parameters are key-value pairs appended to a URL after a question mark: ``` https://apilearn.tukas.dev/api/products/?category=kitchen&ordering=price ``` They let the server know what you want — which category, which page, how to sort. You could build this string yourself, but the requests library gives you a cleaner way: pass a dict as the params argument and it handles the encoding for you: ```python import requests BASE_URL = 'https://apilearn.tukas.dev' params = { 'category': 'kitchen', 'ordering': 'price', } response = requests.get(f'{BASE_URL}/api/products/', params=params) print(response.url) # shows the full URL that was sent ``` response.url is useful for debugging — it shows the exact URL the library built, including all encoded parameters. The /api/products/ endpoint accepts these parameters: - category — filter by category slug (e.g. 'kitchen', 'bedroom') - ordering — sort field; prefix with - for descending (e.g. 'price', '-price', 'name') - search — full-text search in product names and descriptions - min_price / max_price — price range filter - has_discount — set to 'true' to return only discounted products - page / page_size — pagination (covered in the Pagination topic) Parameters can be combined freely. The server applies all filters together, so you can ask for bedroom products under $200 sorted by name in one request.

Filter, sort, and search — patterns

#
Filter by a single category: ```python response = requests.get(f'{BASE_URL}/api/products/', params={'category': 'office'}) data = response.json() print(data['count']) # products in the office category only ``` Filter by price range: ```python params = {'min_price': 50, 'max_price': 150} response = requests.get(f'{BASE_URL}/api/products/', params=params) for product in response.json()['results']: print(product['name'], product['sell_price']) ``` Sort by price descending (most expensive first): ```python params = {'ordering': '-price', 'page_size': 5} response = requests.get(f'{BASE_URL}/api/products/', params=params) for p in response.json()['results']: print(p['name'], p['price']) ``` Search by keyword: ```python params = {'search': 'chair'} response = requests.get(f'{BASE_URL}/api/products/', params=params) print(response.json()['count'], 'products match') ``` Combine multiple filters in one request: ```python params = { 'category': 'bedroom', 'max_price': 300, 'has_discount': 'true', 'ordering': 'name', } response = requests.get(f'{BASE_URL}/api/products/', params=params) data = response.json() print(f'{data["count"]} discounted bedroom products under $300') ``` Note that has_discount takes the string 'true', not the Python boolean True. Query parameters are always strings in HTTP — requests converts simple values, but it is safest to be explicit with boolean-like flags.

Query parameter reference

#
Query parameters for GET /api/products/: ``` category string Category slug: kitchen, bedroom, living-room, office, hardware, decor, bathroom, kids-room, outdoor, dining-room, storage, lighting ordering string Sort field. Prefix with - for descending: price, -price, name, -name search string Full-text search in name and description min_price number Minimum price (inclusive) max_price number Maximum price (inclusive) has_discount string 'true' — return only products with a discount page int Page number (default: 1) page_size int Items per page (default: 20) ``` All parameters are optional and can be combined. The response always includes count, next, previous, and results. Quick examples: ```python # Cheapest 5 items in kitchen params = {'category': 'kitchen', 'ordering': 'price', 'page_size': 5} # All discounted products params = {'has_discount': 'true'} # Products matching 'table' between $50 and $300 params = {'search': 'table', 'min_price': 50, 'max_price': 300} ```
01

Filter by category

#

Fetch all products in the "bedroom" category and print how many there are in total.

import requests

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

# Fetch bedroom products and print the total count
Solution
import requests

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

params = {'category': 'bedroom'}
response = requests.get(f'{BASE_URL}/api/products/', params=params)
data = response.json()
print(data['count'])
02

Filter by price range

#

Fetch products priced between $50 and $100 and print the name and sell_price of each product on the first page.

import requests

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

# Fetch products between $50 and $100
Solution
import requests

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

params = {'min_price': 50, 'max_price': 100}
response = requests.get(f'{BASE_URL}/api/products/', params=params)
for product in response.json()['results']:
    print(product['name'], product['sell_price'])
03

Sort results

#

Fetch the 5 most expensive products across the entire catalog and print their names and prices.

import requests

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

# Get 5 most expensive products
Solution
import requests

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

params = {'ordering': '-price', 'page_size': 5}
response = requests.get(f'{BASE_URL}/api/products/', params=params)
for product in response.json()['results']:
    print(product['name'], product['price'])
04

Search by keyword

#

Search for all products containing the word "table" and print how many results were found.

import requests

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

# Search for 'table' and print the result count
Solution
import requests

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

params = {'search': 'table'}
response = requests.get(f'{BASE_URL}/api/products/', params=params)
print(response.json()['count'])
05

Combine multiple filters

#

Fetch discounted products in the "dining-room" category with a price under $400, sorted alphabetically by name. Print the name and sell_price of each result.

import requests

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

# Combine category, max_price, has_discount, and ordering
Solution
import requests

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

params = {
    'category': 'dining-room',
    'max_price': 400,
    'has_discount': 'true',
    'ordering': 'name',
}
response = requests.get(f'{BASE_URL}/api/products/', params=params)
for product in response.json()['results']:
    print(product['name'], product['sell_price'])
06

Inspect the built URL

#

Build a request with category="office", ordering="-price", and page_size=3. Before reading the response, print the full URL that requests constructed (including all query parameters).

import requests

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

params = {
    'category': 'office',
    'ordering': '-price',
    'page_size': 3,
}
response = requests.get(f'{BASE_URL}/api/products/', params=params)

# Print the full URL that was sent
Solution
import requests

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

params = {
    'category': 'office',
    'ordering': '-price',
    'page_size': 3,
}
response = requests.get(f'{BASE_URL}/api/products/', params=params)
print(response.url)
print(response.json()['count'], 'total results')