Python · API · Intermediate
Shopping Cart
Add, update, and remove cart items using authenticated requests.
Quick topic start and explanations before exercises (exercises below):
Adding, updating, and removing cart items
#Cart API reference
#Exercises:
View your cart
#Authenticate and send a GET to /api/cart/. Print the number of items in the cart and the total price.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Get a token # 2. GET /api/cart/ and print item count and 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}'}
cart = requests.get(f'{BASE_URL}/api/cart/', headers=headers).json()
print(len(cart['items']))
print(cart['total_price'])
Add a product to cart
#Find any product from /api/products/ and add it to your cart with quantity=1. Print the cart item id from the response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Authenticate # 2. Pick a product id from /api/products/ # 3. POST to /api/cart/items/ and print the cart item id
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}'}
# Pick first product — take its slug
product_slug = requests.get(f'{BASE_URL}/api/products/').json()['results'][0]['slug']
response = requests.post(
f'{BASE_URL}/api/cart/items/',
json={'product_slug': product_slug, 'quantity': 1},
headers=headers,
)
print(response.json()['id'])
Update item quantity
#Add a product to your cart, then update its quantity to 3 using PATCH /api/cart/items/{id}/. Print the updated quantity from the response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Authenticate # 2. Add a product # 3. PATCH the quantity to 3
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']
item = requests.post(
f'{BASE_URL}/api/cart/items/',
json={'product_slug': product_slug, 'quantity': 1},
headers=headers,
).json()
cart_item_id = item['id']
updated = requests.patch(
f'{BASE_URL}/api/cart/items/{cart_item_id}/',
json={'quantity': 3},
headers=headers,
).json()
print(updated['quantity'])
Remove one item
#Add a product to your cart, then remove it with DELETE /api/cart/items/{id}/. Verify the cart is empty by fetching it again and printing the item count.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Authenticate # 2. Add a product # 3. Delete that item # 4. Verify cart is empty
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']
item = requests.post(
f'{BASE_URL}/api/cart/items/',
json={'product_slug': product_slug, 'quantity': 1},
headers=headers,
).json()
requests.delete(f'{BASE_URL}/api/cart/items/{item["id"]}/', headers=headers)
cart = requests.get(f'{BASE_URL}/api/cart/', headers=headers).json()
print(len(cart['items']))
Add two products and clear the cart
#Add two different products to your cart. Print the total. Then clear the cart with DELETE /api/cart/items/ and verify it is empty.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Authenticate # 2. Add two different products # 3. Print total # 4. Clear cart # 5. Confirm cart is empty
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']
product_slugs = [products[0]['slug'], products[1]['slug']]
for slug in product_slugs:
requests.post(
f'{BASE_URL}/api/cart/items/',
json={'product_slug': slug, 'quantity': 1},
headers=headers,
)
cart = requests.get(f'{BASE_URL}/api/cart/', headers=headers).json()
print('total:', cart['total_price'])
requests.delete(f'{BASE_URL}/api/cart/items/', headers=headers)
cart = requests.get(f'{BASE_URL}/api/cart/', headers=headers).json()
print('items after clear:', len(cart['items']))