What is a REST API?

A REST API exposes data and actions as resources with their own URLs, which clients read and change with standard HTTP methods. It is the most common style for web APIs.

Where does REST come from?

From Roy Fielding's 2000 doctoral dissertation, which described the architectural style behind the web itself: representational state transfer. Its constraints include a client-server split, stateless requests, cacheable responses, a uniform interface and layered systems. Most APIs called RESTful today follow the spirit rather than every constraint, using resource URLs, HTTP methods and JSON bodies.

What is a resource?

Any named thing the API exposes: a customer, an order, a list of orders, a server's status. Each has a URL, usually a plural noun followed by an identifier, such as /v1/orders/8812. Collections and their members form a hierarchy: /v1/customers/57/orders lists one customer's orders. Verbs belong in the HTTP method rather than the path, so POST /v1/orders instead of /v1/createOrder.

How are HTTP methods used?

MethodMeaningSafeIdempotent
GETRead a resource or collectionYesYes
POSTCreate a resource or trigger an actionNoNo
PUTReplace a resource entirelyNoYes
PATCHChange part of a resourceNoNot necessarily
DELETERemove a resourceNoYes

Idempotent methods can be retried safely after a timeout. For POST, accept an idempotency key header so clients can retry a payment or order creation without creating duplicates.

Which status codes should an API return?

Precise ones, so clients can react without parsing messages. 200 for success with a body, 201 with a Location header for a created resource, 204 for success without a body, 400 for malformed input, 401 when authentication is missing, 403 when it is present but insufficient, 404 for unknown resources, 409 for conflicts, 422 for valid syntax with invalid content, 429 for rate limiting, and 5xx only for server faults. Pair errors with a consistent JSON body; the problem details format from RFC 9457 is a sensible standard.

curl -s -X POST https://api.example.net/v1/orders \
  -H "Authorization: Bearer $TOKEN" -H 'Idempotency-Key: 6f1d1c0e-7b2a-4d4e-9a55-31c0' \
  -H 'Content-Type: application/json' -d '{"sku":"rack-rail-2u","quantity":3}' -i | head -12

Why must requests be stateless?

So any server can answer any request. Each request carries everything needed to process it, typically a token for identity, rather than relying on memory of earlier requests on the same server. That lets you run several API servers behind a load balancer and restart any of them without losing sessions. Shared state lives in the database or a cache, not in the API process. Our Load balancing article shows how to spread requests across servers.

How should large collections be paginated?

With cursors for anything that changes or grows. Offset pagination (?page=40) is simple but slows down on deep pages and skips or repeats items when data changes between requests. Cursor pagination returns an opaque token pointing after the last item, and the client passes it back: ?after=eyJpZCI6ODgxMn0. Return a next link in the response so clients never build URLs themselves.

How are REST APIs versioned and secured?

Version in the path (/v1/) or a header, and change versions only for breaking changes; adding fields is not breaking. Secure every endpoint with HTTPS and token-based authentication, check authorisation per resource, validate input against a schema and apply rate limits per client. Publishing an OpenAPI description lets clients generate code and test tools validate behaviour. Our Application security article covers the common flaws, such as one customer reading another's orders by changing an identifier.

What does serving an API well take?

A reverse proxy for TLS, compression and rate limits, stateless application servers, a well-indexed database and caching for hot reads. A single ESSENTIAL-3 with 12 answers thousands of simple requests per second at $61.20 a month; busier APIs add a second application server and move the database to BUSINESS-2 2024. Our API article covers the broader concept and other API styles.

Machines for API servers

ModelProcessorCoresMemoryDrivesPortPer month
ESSENTIAL-1Intel Xeon-E 2386G632 GB2x 512 GB SSD NVMe1 Gbps unmetered$38.40
ESSENTIAL-3AMD Ryzen 9 5900X1232 GB2x 512 GB SSD NVMe1 Gbps unmetered$61.20
ESSENTIAL-MAMD Ryzen 9 9900X1264 GB2x 512 GB SSD NVMe1 Gbps unmetered$70.80
BUSINESS-2 2024AMD EPYC 4344P864 GB2x 960 GB SSD NVMe3 Gbps unmetered$109.80

Related pages

API

Application programming interfaces in general: library, operating system and network APIs, the main web API styles, contracts, limits and running API infrastructure.

Read more

Web application

Software used through a browser: how front end, back end and data layers fit together, common architectures and hosting one on your own server.

Read more

Application security

Keeping the software you deploy safe: common vulnerability classes, secure coding habits, dependency hygiene, testing and runtime defences on your own server.

Read more

Load balancing

Spreading requests over several servers: layer 4 and layer 7 balancers, algorithms, health checks, sticky sessions and self-built designs with HAProxy.

Read more

Questions and answers

What does REST stand for?

Representational state transfer, an architectural style described by Roy Fielding in 2000 and used by most web APIs.

When should an API return 401 or 403?

401 when authentication is missing or invalid; 403 when the caller is authenticated but not allowed to perform the action.

Is PUT or PATCH better for updates?

PUT replaces the whole resource and is idempotent; PATCH changes only the given fields. Many APIs offer PATCH for convenience.

How do I make POST requests safe to retry?

Accept an idempotency key header and store it with the result, so a repeated request with the same key returns the original outcome instead of acting twice.

What is OpenAPI?

A standard format for describing REST APIs: endpoints, parameters, request and response schemas. Tools use it to generate documentation, clients and tests.