Skip to the page

Microservices and APIs · Design

Design a REST API for orders

The 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

  1. Which method for cancel, and why?
  2. The app's network drops after it sends "create order", and it retries. What stops a second order?
  3. How do you paginate a list that keeps changing?
  4. What does an error response look like?
  5. Which status codes for: not found, someone else's order, invalid input, already cancelled, rate limited?
  6. How do you filter by status and date?
  7. How do you make sure one user can't read another user's order?
  8. What changes if partners also call this API?

The answer

What a strong answer covers

  • POST /orders (201 with Location), GET /orders/{id}, GET /orders?status=&from=&cursor=&limit=, and cancel as POST /orders/{id}/cancel (an action with rules) or a validated PATCH of the status.
  • Idempotency: an Idempotency-Key header 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 voice

Practise 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.

Start your free interview