LogoMasst Docs

REST

Understanding RESTful API design principles.

What is REST?

REST (Representational State Transfer) is an architectural style for designing networked applications. RESTful APIs use HTTP methods to perform CRUD operations on resources.


REST Principles

PrincipleDescription
StatelessEach request contains all info needed
Client-ServerSeparation of concerns
CacheableResponses can be cached
Uniform InterfaceConsistent resource-based URLs
Layered SystemClient doesn't know if talking to server or intermediary

Resource-Based URLs

Good (resource-based):
GET    /users           # List users
GET    /users/123       # Get user 123
POST   /users           # Create user
PUT    /users/123       # Update user 123
DELETE /users/123       # Delete user 123

Bad (action-based):
GET /getUser?id=123
POST /createUser
POST /deleteUser

HTTP Methods in REST

OperationMethodExample
CreatePOSTPOST /users
ReadGETGET /users/123
Update (full)PUTPUT /users/123
Update (partial)PATCHPATCH /users/123
DeleteDELETEDELETE /users/123

Request/Response Example

Request

POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json

{
  "name": "John",
  "email": "john@example.com"
}

Response

HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/users/123

{
  "id": 123,
  "name": "John",
  "email": "john@example.com"
}

Best Practices

PracticeExample
Use nouns, not verbs/users not /getUsers
Use plural nouns/users not /user
Nest for relationships/users/123/orders
Use query params for filtering/users?role=admin
Version your API/v1/users or header

Pagination

GET /users?page=2&limit=20

Response:
{
  "data": [...],
  "pagination": {
    "page": 2,
    "limit": 20,
    "total": 100,
    "next": "/users?page=3&limit=20"
  }
}

Interview Tips

  • Explain REST principles (stateless, uniform interface)
  • Design resource-based URLs properly
  • Know when to use each HTTP method
  • Discuss pagination, filtering, versioning
  • Compare with GraphQL and gRPC