Python · API · Intermediate
Orders
Place orders from your cart and inspect your order history.
Quick topic start and explanations before exercises (exercises below):
Placing and retrieving orders
#Order endpoints reference
#Exercises:
Place an order
#Add at least one product to your cart, then POST to /api/orders/ to place an order. Print the order id and status from the response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Authenticate # 2. Add a product to cart # 3. POST /api/orders/ and print order id + total
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
product_slug = requests.get(f'{BASE_URL}/api/products/').json()['results'][0]['slug']
requests.post(
f'{BASE_URL}/api/cart/items/',
json={'product_slug': product_slug, 'quantity': 1},
headers=headers,
)
order = requests.post(
f'{BASE_URL}/api/orders/',
json={'phone_number': '+1234567890'},
headers=headers,
).json()
print(order['id'])
print(order['status'])
List your orders
#Fetch the order history from GET /api/orders/. Print how many orders you have and the id and status of each.
import requests BASE_URL = 'https://apilearn.tukas.dev' # GET /api/orders/ and print count + each order total
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
data = requests.get(f'{BASE_URL}/api/orders/', headers=headers).json()
print(f'{data["count"]} orders')
for order in data['results']:
print(f" #{order['id']} — {order['status']}")
Get order details
#Place an order, then fetch its details from GET /api/orders/{id}/. Print each item name, quantity, and price.
import requests
BASE_URL = 'https://apilearn.tukas.dev'
# 1. Authenticate, add items, place order
# 2. GET /api/orders/{id}/ and print each item
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
products = requests.get(f'{BASE_URL}/api/products/').json()['results']
for p in products[:2]:
requests.post(
f'{BASE_URL}/api/cart/items/',
json={'product_slug': p['slug'], 'quantity': 1},
headers=headers,
)
order = requests.post(
f'{BASE_URL}/api/orders/',
json={'phone_number': '+1234567890'},
headers=headers,
).json()
order_id = order['id']
detail = requests.get(f'{BASE_URL}/api/orders/{order_id}/', headers=headers).json()
for item in detail['items']:
print(item['name'], item['quantity'], item['price'])
Full order flow
#Complete the full flow in one script: authenticate, add 3 products to the cart, place an order, confirm the order appears in the order list, and print the order id and item count.
import requests BASE_URL = 'https://apilearn.tukas.dev' # Full flow: auth → add 3 items → place order → confirm in list
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
session = requests.Session()
session.headers['Authorization'] = f'Token {token}'
products = session.get(f'{BASE_URL}/api/products/').json()['results'][:3]
for p in products:
session.post(f'{BASE_URL}/api/cart/items/', json={'product_slug': p['slug'], 'quantity': 1})
order = session.post(f'{BASE_URL}/api/orders/', json={'phone_number': '+1234567890'}).json()
order_id = order['id']
orders_list = session.get(f'{BASE_URL}/api/orders/').json()
order_ids = [o['id'] for o in orders_list['results']]
print(f'Order {order_id} in list: {order_id in order_ids}')
print(f'Items: {len(order["items"])}')
Calculate total spend
#Place two separate orders (add items, place, add more items, place again). Then fetch all orders and calculate your total spend across all orders. Print the sum.
import requests BASE_URL = 'https://apilearn.tukas.dev' # Place 2 orders, then sum all order totals
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
session = requests.Session()
session.headers['Authorization'] = f'Token {token}'
products = session.get(f'{BASE_URL}/api/products/').json()['results']
# First order
session.post(f'{BASE_URL}/api/cart/items/', json={'product_slug': products[0]['slug'], 'quantity': 1})
session.post(f'{BASE_URL}/api/orders/', json={'phone_number': '+1234567890'})
# Second order
session.post(f'{BASE_URL}/api/cart/items/', json={'product_slug': products[1]['slug'], 'quantity': 2})
session.post(f'{BASE_URL}/api/orders/', json={'phone_number': '+1234567890'})
orders = session.get(f'{BASE_URL}/api/orders/').json()['results']
total_spend = sum(
float(item['price']) * item['quantity']
for o in orders
for item in o['items']
)
print(f'Total spend: {total_spend:.2f}')