AlgoViz

API design introduction

easy 24 min

An API is a contract: it says what a program may ask for and what it gets back, and it hides how the work is done.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When to bring it up

In a system design interview, the API comes after scope and numbers and takes about five minutes. Three to five endpoints are enough.

You hearSayBecause
Users can create and viewTwo endpoints, with method and addressIt fixes the scope in writing
Mobile clientsFew round trips, paging, small payloadsEach round trip is slow
Third-party developersStable, documented, versionedYou cannot update their code
Payments or ordersAn idempotency key on POSTReplies get lost
Many serversStateless requestsAny server can answer
A long listA limit and a cursorNo endless responses

Options

Compare your moves

A shopper places an order. What does the endpoint look like?

BreaksGET /createOrder?product=1&qty=2
How
One link that creates an order.
Limit
GET is meant to be safe. Crawlers, previews and retries may call it, and each call may create an order.
Choose when
Never.
WorksPOST /orders with a JSON body
How
Creates an order, returns 201 and Location.
Limit
A lost reply leaves the client unsure. Retrying may order twice.
Choose when
Low-risk creations.
StrongPOST /orders with an idempotency key
How
The client sends a unique key; the server stores key and result and repeats the same answer.
Limit
Needs storage and an expiry rule.
Choose when
Anything with money or side effects.