API stands for Application Programming Interface. An application is any piece of software with its own job. Programming means that other software uses it, through code. The interface is the line between the two: what you may ask, how you ask, and what comes back.
A web API puts that line on the network. One program sends a request over HTTP, another sends a response. The caller never sees the code, the database or the language behind it. It only sees the contract.
API design is the work of choosing that contract: which things exist, what can be done to them, what can go wrong, and how all of that is written down.
Why it exists
Without an API, every app would reach into the database itself. Each one would carry the password, know the table layout, and break whenever a table changed. An API gives one guarded door with rules behind it.
A contract is also hard to take back. Once strangers have built on it, every name you chose is in their code. That is why the design deserves thought before the code does.
A short history
1991
HTTP, as the World Wide Web software first uses it, has a one-line request: the word GET and a document address. The reply is a page of HTML.
May 1996
RFC 1945 describes HTTP/1.0, with methods, headers and status codes.
January 1997
RFC 2068 standardises HTTP/1.1, including PUT, DELETE and cache rules.
2000
Roy Fielding, one of the HTTP authors, describes REST in a doctoral thesis: the design rules behind the web, written down as an architectural style.
June 2022
RFC 9110 gathers the meaning of methods, headers and status codes into one document shared by HTTP/1.1, HTTP/2 and HTTP/3.
Words to know
Client and server
The program that asks and the program that answers.
Endpoint
One method on one address, such as GET /products.
Resource
A thing with an address: a product, a cart, an order.
Representation
The form a resource takes in a message, for example JSON.
Stateless
Each request carries everything the server needs to understand it.
Safe
The client asks for no change. GET is safe.
Idempotent
Sending it twice is meant to have the same effect as sending it once.
Payload
The body of a request or response.
How it works
Step through the idea, or press Play.
Step 1 of 3
Name the resources. Nouns with addresses: products, carts, orders
Use HTTP for the actions. Method, headers, body in; status code, headers, body out
Keep the promise. Stateless requests, clear errors, changes that do not break callers
Understand it
Start here
The waiter
Imagine a restaurant. You are hungry, but you cannot walk into the kitchen and cook. A waiter takes your order, gives it to the kitchen and brings back your meal. In software, that waiter is called an API.
Ordering at a restaurant
The kitchen can buy a new oven or hire another cook, and the menu stays the same.
Restaurant and API, side by side
Restaurant
API
Meaning
Menu
Documentation
What you can order
Order
HTTP request
What you want
Waiter
Endpoint
Carries messages both ways
Kitchen
Server
Does the actual work
Dish
HTTP response
What you get back
Reservation
Authentication
Who you are
The idea
Why not talk to the database directly?
Without an API
Every copy of every app holds the database password and knows the tables.
With an API
One contract at the edge. Everything inside the dashed line can change without telling the apps.
With an API
Secure: the API checks who is asking and what they may do.
Flexible: tables and code can change inside.
Efficient: the server runs and tunes the queries.
Maintainable: one place to fix a rule.
Without
Insecure: database credentials in every app.
Inflexible: apps are tied to the table layout.
Inefficient: complex queries written in each app.
Unmaintainable: change the database, change every app.
Kinds
Four kinds of API
Web APIs
Called over HTTP: Stripe, Google Maps, GitHub. This topic is about these.
Library APIs
Functions and classes you import: React, NumPy, jQuery.
Operating system APIs
How a program asks for files, memory and windows: Windows API, POSIX, macOS.
Hardware APIs
How code drives a device: graphics cards, sensors, USB.
HTTP
What HTTP gives you
Request and response
The client sends a request, the server sends a response. The server does not speak first.
Stateless
Each request stands alone. HTTP itself does not remember the previous one.
Readable model
Methods, paths and headers are words. HTTP/1.1 sends them as text; HTTP/2 and HTTP/3 send the same words in binary frames.
Reliable transport
Runs over TCP, or over QUIC for HTTP/3. Bytes arrive complete and in order, or the connection fails. It may fail after the server did the work.
REST means REpresentational State Transfer. It is an architectural style, not a protocol: a set of constraints that make web systems scale and evolve. Most APIs called REST follow some of them, usually not all.
1
Client and server are separate
The user interface and the data live apart and can change apart. Example: a phone app and an API server.
2
Stateless
Each request holds all the information needed. Example: the token travels with every request.
3
Cacheable
Responses say whether they may be stored. Example: Cache-Control: max-age=3600. See API caching.
4
Uniform interface
Resources have addresses, methods mean the same everywhere, messages describe themselves and may link to the next step. Example: GET /products, POST /orders.
5
Layered system
The client cannot tell whether it talks to the server or to something in between. Example: client, gateway, server, database.
6
Code on demand
The server may send code to run. Example: JavaScript for a browser. This is the only optional one.
Why statelessness helps scaling
flowchart LR
C["Client: request + token"] --> L["Load balancer"]
L --> A["Server A"]
L --> B["Server B"]
A --> D["Shared data"]
B --> D
Any server can answer any request, because nothing about the client is kept inside one server.
Design
Think in nouns
REST thinks in resources (nouns), not actions (verbs). The method is the verb, the URL is the noun.
Avoid
Prefer
Why
/getAllProducts
GET /products
GET already says get
/createNewProduct
POST /products
POST to a collection creates an item
/updateProductById
PUT /products/{id}
PUT on one address replaces it
/deleteProductFromCatalog
DELETE /products/{id}
DELETE removes it
Resources of a shop
Resource
What it is
/products
The catalogue
/products/{id}
One product
/categories
Product categories
/users
User accounts
/users/{id}/cart
One user's cart
/users/{id}/orders
One user's order history
/orders
All orders, for staff only
/orders/{id}
One order
The address never grants access. The server must check that this caller may see this order: API authorization.
From a requirement to an endpoint
flowchart TD
A["Requirement: shoppers order products"] --> B["Find the nouns"]
B --> C["Give each noun an address"]
C --> D{"Read, create, change or remove?"}
D -- read --> E["GET"]
D -- create --> F["POST"]
D -- change --> G["PUT or PATCH"]
D -- remove --> H["DELETE"]
E & F & G & H --> I["Errors, limits, paging"]
Process
One request, from tap to screen
A shopper types "wireless headphones" and taps search. A moment later, 47 matching products appear, 20 of them on the first page.
1
User action
The shopper taps the search button in the app.
2
Request formation
The app builds method, URL, headers and body: GET /api/v1/products?search=wireless%20headphones&limit=20. The space becomes %20.
3
Network transit
DNS lookup, TCP connection, TLS handshake. A connection that is already open skips all three.
4
Server processing
The server authenticates, authorizes, validates and queries the database.
5
Response formation
Rows become JSON, headers are added, the response is sent.
6
UI rendered
The app parses the JSON and draws the product cards.
Search products
sequenceDiagram
participant U as Shopper
participant A as Mobile app
participant G as API gateway
participant S as API server
participant D as Database
U->>A: search headphones
A->>G: GET /products?q=headphones + token
G->>G: check token and rate limit
G->>S: forward request
S->>D: SELECT matching products
D-->>S: rows
S-->>G: 200 OK + JSON
G-->>A: response + limit headers
A-->>U: product cards
Some systems check the token in the gateway, some in the server. What a user may see is checked by the server that owns the data.
Inside the server
flowchart TD
A["1 Receive: parse HTTP"] --> B["2 Authenticate: who"]
B --> C["3 Authorize: may they"]
C --> D["4 Validate: input"]
D --> E["5 Process: the work"]
E --> F["6 Respond: format"]
Cheap checks first. A bad token costs nothing but the check.
The full exchange. The list is shortened to one item here; a real first page holds 20.
Where the time goes
Tnew=TCPRTT+TLS 1.3RTT+requestRTT+Tserver=3×100+45=345 ms
Assumptions: a 100 ms round trip on mobile, 45 ms of server work, DNS already known. With an open connection: 100+45=145 ms. Round trips cost more than the server here.
Traffic for one endpoint
86,4001,000,000≈12/s,×3≈35/s at peak,35×8 KB=280 KB/s
Assumptions: one million searches a day, peak three times the average, 8 KB per response. One server handles this. The contract needs more care than the capacity.
Two more flows
A write, and a refusal
Create an order
Step
Action
Result
1
POST /orders
The app asks for an order
2
Check the cart
Every item is in stock
3
Process the payment
The payment provider confirms the charge
4
Create the record
INSERT INTO orders
5
201 Created
Confirmation and a Location header
If the connection drops after step 3, the app cannot know whether it paid. A client-made key makes the retry safe: Idempotency.
No token: 401
sequenceDiagram
participant A as App
participant S as API
A->>S: GET /orders, no token
S->>S: Authorization header missing
S-->>A: 401 + WWW-Authenticate
A->>A: read the error
A->>A: show the login screen
401 means we do not know who you are. 403 means we know, and the answer is no. See API authentication.
Trade-offs
REST: good and bad
Strengths
Simple to understand and to try
Uses HTTP caching as it is
Stateless, so easy to scale out
Tools and frameworks everywhere
Readable URLs and payloads
Weak spots
Over-fetching: more data than the screen needs
Under-fetching: several requests for one screen
No built-in push from the server
Versioning needs a plan
No single error format that everybody uses
Other styles answer some of these: REST, GraphQL and gRPC. Changing a published API: API versioning. For error bodies there is now a standard, RFC 9457, that you can choose to follow.
Checklist
Seven questions for any endpoint
1
Who calls it?
Our app, partners, the public? Strangers need more stability.
An API is the contract between programs. It hides the kitchen: callers see the menu, send an order and receive a dish, while storage, code and servers can change behind it.
With HTTP, the URL names a resource, the method says what to do, the headers describe the message and the status code says how it ended. REST adds design rules, above all statelessness and a uniform interface. Design the resources first, then methods, then errors, then limits, and treat everything you publish as a promise.
API means Application Programming Interface: the waiter between client and server.
REST uses resources (nouns) and HTTP methods (verbs).
HTTP is stateless: each request stands alone.
2xx success, 4xx the request, 5xx the server.
GET reads, POST creates; the Authorization header carries credentials.
A published contract is a promise. Design it before the code.