REST API
Everything the shop can do is available as JSON over HTTP under /api. Every shop request must carry an API key as a bearer token: Authorization: Bearer <key>. Requests with a missing or invalid key get HTTP 401. A key acts as the user who created it.
1. Your API keys
Loading…
2. Example request
curl "/api/products?category=Books" \
-H "Authorization: Bearer YOUR_API_KEY"
curl -X POST /api/checkout \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"items":[{"productId":"p001","quantity":2}],"shipping":{"fullName":"A","address":"1 St","city":"X","postalCode":"1","country":"US"},"payment":{"cardNumber":"4242424242424242","cardHolder":"A","expiry":"12/30","cvc":"123"}}'3. Endpoints
GET /api/categories -> ["Electronics", ...]
GET /api/products -> Product[] # at most 50; optional ?category=Books
GET /api/products/:id -> Product # 404 if the id is unknown
GET /api/me -> { id, username } # the owner of the API key
GET /api/orders -> Order[]
GET /api/orders/:id -> Order # 404 if not yours / unknown
POST /api/checkout -> 201 Order # fake payment, see test cards below
POST /api/register { username, password } # website session: sets a cookie, no API key needed
POST /api/login { username, password }
POST /api/logout
POST /api/change-password { currentPassword, newPassword } -> 204 # session only
GET /api/api-keys -> ApiKey[] # session only (log in first)
POST /api/api-keys { name? } -> 201 ApiKey
DELETE /api/api-keys/:key -> 204Product { id, name, description, priceCents, category, imageUrl }
Order { id, status, items: [{ productId, name, priceCents, quantity }], totalCents, shipping, cardLast4, createdAt }
POST /api/checkout body:
{ items: [{ productId, quantity }], # quantity 1-99
shipping: { fullName, address, city, postalCode, country },
payment: { cardNumber, cardHolder, expiry, cvc } } # expiry is MM/YY
Errors: { "error": { "code": "...", "message": "..." } }Test cards: any 16 digits are approved; 4000 0000 0000 0002 is declined; 4000 0000 0000 9995 is insufficient funds. Error codes: UNAUTHENTICATED (401), BAD_USER_INPUT (400), PAYMENT_DECLINED (402), NOT_FOUND (404), INVALID_CREDENTIALS (401), USERNAME_TAKEN (409). You can also create an account with POST /api/register, or on the website; the API key then identifies you.
4. Sample tests
Create or paste an API key first.
– List categories — Returns every product category.
Expect: HTTP 200 and more than one category
GET /api/categories
– List all products — The catalogue has at most 50 products, each with a positive price and an image.
Expect: 1-50 products, all with priceCents > 0 and an imageUrl
GET /api/products
– Filter products by category — Uses the category query parameter to ask for one category only.
Expect: a non-empty list where every product is in Books
GET /api/products?category=Books
– Get one product — Fetch a single product by id.
Expect: product p001 with a name
GET /api/products/p001
– Unknown product returns 404 — A missing id is a 404 with code NOT_FOUND.
Expect: HTTP 404 with code NOT_FOUND
GET /api/products/does-not-exist
– Who am I? — The API key identifies the user it belongs to.
Expect: your username
GET /api/me
– No API key is rejected — Calls without an Authorization header fail with 401.
Expect: HTTP 401 with code UNAUTHENTICATED
GET /api/categories
– Invalid API key is rejected — A made-up key fails with 401.
Expect: HTTP 401 with code UNAUTHENTICATED
GET /api/categories
– Checkout: declined card — Card 4000 0000 0000 0002 is always declined.
Expect: HTTP 402 with code PAYMENT_DECLINED
POST /api/checkout
{
"items": [
{
"productId": "p001",
"quantity": 2
}
],
"shipping": {
"fullName": "Test User",
"address": "1 Test St",
"city": "Testville",
"postalCode": "12345",
"country": "US"
},
"payment": {
"cardNumber": "4000000000000002",
"cardHolder": "Test User",
"expiry": "12/30",
"cvc": "123"
}
}– Checkout: validation error — An expiry date in the past is rejected before payment.
Expect: HTTP 400 with code BAD_USER_INPUT
POST /api/checkout
{
"items": [
{
"productId": "p001",
"quantity": 2
}
],
"shipping": {
"fullName": "Test User",
"address": "1 Test St",
"city": "Testville",
"postalCode": "12345",
"country": "US"
},
"payment": {
"cardNumber": "4242424242424242",
"cardHolder": "Test User",
"expiry": "01/20",
"cvc": "123"
}
}– Checkout: successful order — Two of product p001 paid with 4242 4242 4242 4242. Total is computed on the server.
Expect: HTTP 201, status PAID, cardLast4 4242, totalCents = 2 x price of p001
POST /api/checkout
{
"items": [
{
"productId": "p001",
"quantity": 2
}
],
"shipping": {
"fullName": "Test User",
"address": "1 Test St",
"city": "Testville",
"postalCode": "12345",
"country": "US"
},
"payment": {
"cardNumber": "4242424242424242",
"cardHolder": "Test User",
"expiry": "12/30",
"cvc": "123"
}
}– List my orders — Orders placed with this key's user. Run the successful checkout first to have one.
Expect: an array of orders
GET /api/orders