Microservices and APIs · Design
Design a REST API for orders
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Design the REST API for orders in our e-commerce backend: create an order, fetch one, list a user's orders, and cancel one. Tell me the URLs, methods, status codes, and how pagination and errors work.
Then
Follow-ups the interviewer asks next
- Which method for cancel, and why?
- The app's network drops after it sends "create order", and it retries. What stops a second order?
- How do you paginate a list that keeps changing?
- What does an error response look like?
- Which status codes for: not found, someone else's order, invalid input, already cancelled, rate limited?
- How do you filter by status and date?
- How do you make sure one user can't read another user's order?
- What changes if partners also call this API?
The answer
What a strong answer covers
POST /orders(201 withLocation),GET /orders/{id},GET /orders?status=&from=&cursor=&limit=, and cancel asPOST /orders/{id}/cancel(an action with rules) or a validatedPATCHof the status.- Idempotency: an
Idempotency-Keyheader on POST, stored with the result, so a retry returns the same order. The same key with a different body is rejected (422 in the IETF draft). - Cursor pagination with an opaque cursor over (created_at, id) and a capped
limit; not offset for large, changing lists. - Errors in one consistent JSON shape (for example RFC 9457 problem details: type, title, status, detail), plus an error code and the request id. No stack traces.
- Status codes: 400 or 422 for invalid input, 401 when not signed in, 404 for a missing order and also for someone else's (so existence doesn't leak), 409 for a state conflict such as already cancelled, 429 for rate limits.
- Authorisation: every query filtered by the caller's user id; hard-to-guess ids don't replace the check.
- Partners: API keys or OAuth client credentials, versioning, per-key rate limits, and webhooks for status changes.
The red pen
Common traps
Avoid:GET /cancelOrder?id=; offset pagination over a huge table; 200 with an error inside the body; trusting a user id sent in the request body.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.