REST API Design Basics
Learn how to design clean, consistent REST APIs. Covers resource naming, HTTP methods, status codes, pagination, and versioning.
Core REST Principles
REST (Representational State Transfer) is an architectural style for APIs built on HTTP. Key principles:
- Resources β everything is a resource identified by a URL
- HTTP methods β use GET, POST, PUT, PATCH, DELETE with their intended semantics
- Stateless β each request contains all the information needed; no server-side sessions
- Uniform interface β consistent URL patterns and response formats across endpoints
URL Structure and Naming
Use nouns for resources, not verbs. Keep URLs predictable:
GET /usersβ list all usersGET /users/42β get user 42POST /usersβ create a new userPUT /users/42β replace user 42PATCH /users/42β partially update user 42DELETE /users/42β delete user 42
For nested resources: GET /users/42/orders β list orders for user 42.
Avoid deep nesting beyond two levels.
Pagination and Filtering
For collections, always paginate. Common patterns:
# Offset-based pagination
GET /articles?page=2&per_page=25
# Cursor-based pagination (better for large datasets)
GET /articles?after=eyJpZCI6MTAwfQ&limit=25
# Filtering and sorting
GET /articles?status=published&sort=-created_at
GET /users?role=admin&search=aliceVersioning Your API
APIs evolve, and breaking changes need a versioning strategy:
- URL path β
GET /v2/usersβ simple, explicit, easy to route - Header β
Accept: application/vnd.api+json;version=2β cleaner URLs but harder to test - Query param β
GET /users?version=2β easy to use but pollutes the URL
URL path versioning is the most widely adopted approach. Only increment the major version for breaking changes.
Related Tutorials
HTTP Status Codes Explained
A complete guide to HTTP response status codes. Learn what 200, 301, 404, 500, and other codes mean with real-world examples.
How DNS Works
Understand the Domain Name System from root servers to your browser. Learn about DNS resolution, record types, caching, and troubleshooting.