JavaScript · 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 the cart, then POST to `/api/orders/` with a phone number. Print the order `id` and `status`.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// 1. authenticate
// 2. add a product to cart
// 3. POST /api/orders/ and print id + status
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const slug = await fetch(`${BASE_URL}/api/products/`).then(r => r.json())
.then(d => d.results[0].slug);
await fetch(`${BASE_URL}/api/cart/items/`, {
method: 'POST', headers,
body: JSON.stringify({ product_slug: slug, quantity: 1 }),
});
const order = await fetch(`${BASE_URL}/api/orders/`, {
method: 'POST', headers,
body: JSON.stringify({ phone_number: '+1234567890' }),
}).then(r => r.json());
console.log(order.id);
console.log(order.status);
}
main();
List orders
#Fetch your order history from GET `/api/orders/`. Print the total count and each order's `id` and `status`.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// GET /api/orders/ and print count + each order's id and status
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = { 'Authorization': `Token ${token}` };
const data = await fetch(`${BASE_URL}/api/orders/`, { headers }).then(r => r.json());
console.log(`${data.count} orders`);
for (const order of data.results) {
console.log(`#${order.id} — ${order.status}`);
}
}
main();
Order details
#Place an order, then fetch its details from GET `/api/orders/{id}/`. Print the `name`, `quantity`, and `price` for each item.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// place an order then fetch its detail
// print each item's name, quantity, price
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const products = await fetch(`${BASE_URL}/api/products/`).then(r => r.json());
for (const p of products.results.slice(0, 2)) {
await fetch(`${BASE_URL}/api/cart/items/`, {
method: 'POST', headers,
body: JSON.stringify({ product_slug: p.slug, quantity: 1 }),
});
}
const order = await fetch(`${BASE_URL}/api/orders/`, {
method: 'POST', headers,
body: JSON.stringify({ phone_number: '+1234567890' }),
}).then(r => r.json());
const detail = await fetch(`${BASE_URL}/api/orders/${order.id}/`, { headers }).then(r => r.json());
for (const item of detail.items) {
console.log(item.name, item.quantity, item.price);
}
}
main();
Full order flow
#In one script: authenticate, add 3 products to the cart, place an order, confirm it appears in the order list. Print the order id and item count.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// full flow: auth → 3 products → order → confirm in list
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const products = await fetch(`${BASE_URL}/api/products/`).then(r => r.json());
for (const p of products.results.slice(0, 3)) {
await fetch(`${BASE_URL}/api/cart/items/`, {
method: 'POST', headers,
body: JSON.stringify({ product_slug: p.slug, quantity: 1 }),
});
}
const order = await fetch(`${BASE_URL}/api/orders/`, {
method: 'POST', headers,
body: JSON.stringify({ phone_number: '+1234567890' }),
}).then(r => r.json());
const history = await fetch(`${BASE_URL}/api/orders/`, { headers }).then(r => r.json());
const ids = history.results.map(o => o.id);
console.log(`Order ${order.id} in list: ${ids.includes(order.id)}`);
console.log(`Items: ${order.items.length}`);
}
main();
Calculate total spend
#Place two separate orders (add products → order → add more → order). Then fetch all orders and calculate the total spend across all items. Print the total.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// place 2 orders, then sum all item prices
}
main();
Solution
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const products = await fetch(`${BASE_URL}/api/products/`).then(r => r.json());
// First order
await fetch(`${BASE_URL}/api/cart/items/`, {
method: 'POST', headers,
body: JSON.stringify({ product_slug: products.results[0].slug, quantity: 1 }),
});
await fetch(`${BASE_URL}/api/orders/`, {
method: 'POST', headers,
body: JSON.stringify({ phone_number: '+1234567890' }),
});
// Second order
await fetch(`${BASE_URL}/api/cart/items/`, {
method: 'POST', headers,
body: JSON.stringify({ product_slug: products.results[1].slug, quantity: 2 }),
});
await fetch(`${BASE_URL}/api/orders/`, {
method: 'POST', headers,
body: JSON.stringify({ phone_number: '+1234567890' }),
});
const allOrders = await fetch(`${BASE_URL}/api/orders/`, { headers }).then(r => r.json());
const totalSpend = allOrders.results.reduce((sum, order) =>
sum + order.items.reduce((s, item) =>
s + parseFloat(item.price) * item.quantity, 0
), 0
);
console.log(`Total spend: ${totalSpend.toFixed(2)}`);
}
main();