DEV Community

Dev Cookies
Dev Cookies

Posted on

πŸ“˜User Management REST API Documentation

πŸ“ Base URL: https://api.example.com/v1


πŸ”’ Authentication & Authorization

  • πŸ”‘ Auth Type: Bearer Token (JWT or OAuth 2.0)
  • πŸ“₯ Header Required:
  Authorization: Bearer <token>
Enter fullscreen mode Exit fullscreen mode
  • 🚫 401 Unauthorized: Missing/invalid token
  • 🚫 403 Forbidden: Insufficient permission for resource

πŸ› οΈ Common Request Headers

Header Required Value Description
Authorization βœ… Bearer <token> Auth header
Content-Type βœ… application/json Format of request body
Accept βœ… application/json Expected response format
X-Request-ID ❌ <uuid> Optional for tracing/debugging

πŸ“€ Common Response Headers

Header Description
Content-Type application/json
X-Request-ID Echoed back from request (for traceability)
X-Rate-Limit Requests allowed in a time window
Retry-After Returned on 429 Too Many Requests

πŸ“‚ Resource: /users


1️⃣ GET /users β€” Get All Users

βœ… Request

GET /users HTTP/1.1
Authorization: Bearer eyJhbGci...
Accept: application/json
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ Request Body:

None

πŸ“€ Response: 200 OK

HTTP/1.1 200 OK
Content-Type: application/json
X-Request-ID: 98219cda-21e8
Enter fullscreen mode Exit fullscreen mode
[
  {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com",
    "createdAt": "2023-01-01T10:00:00Z"
  }
]
Enter fullscreen mode Exit fullscreen mode

2️⃣ GET /users/{id} β€” Get User by ID

βœ… Request

GET /users/1 HTTP/1.1
Authorization: Bearer eyJhbGci...
Accept: application/json
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ Path Parameter:

Param Type Description
id int User’s unique ID

πŸ“€ Response: 200 OK

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "createdAt": "2023-01-01T10:00:00Z"
}
Enter fullscreen mode Exit fullscreen mode

❌ Response: 404 Not Found

{
  "error": "User with ID 99 not found"
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ POST /users β€” Create a New User

βœ… Request

POST /users HTTP/1.1
Authorization: Bearer eyJhbGci...
Content-Type: application/json
Accept: application/json
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ Request Body

{
  "name": "Alice Smith",
  "email": "alice@example.com",
  "password": "SecureP@ss123"
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Password must be encrypted at backend before storing in DB

πŸ“€ Response: 201 Created

Location: /users/2
Enter fullscreen mode Exit fullscreen mode
{
  "id": 2,
  "name": "Alice Smith",
  "email": "alice@example.com"
}
Enter fullscreen mode Exit fullscreen mode

❌ Response: 400 Bad Request

{
  "error": "Email already in use"
}
Enter fullscreen mode Exit fullscreen mode

4️⃣ PUT /users/{id} β€” Update a User

βœ… Request

PUT /users/1 HTTP/1.1
Authorization: Bearer eyJhbGci...
Content-Type: application/json
Enter fullscreen mode Exit fullscreen mode

πŸ“₯ Request Body

{
  "name": "Alice M. Smith",
  "email": "alice.m@example.com"
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Response: 200 OK

{
  "id": 1,
  "name": "Alice M. Smith",
  "email": "alice.m@example.com"
}
Enter fullscreen mode Exit fullscreen mode

5️⃣ DELETE /users/{id} β€” Delete a User

βœ… Request

DELETE /users/1 HTTP/1.1
Authorization: Bearer eyJhbGci...
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Response: 204 No Content

No response body.

❌ 404 Not Found

{
  "error": "User not found"
}
Enter fullscreen mode Exit fullscreen mode

⚠️ Standard Error Format (used in all endpoints)

{
  "timestamp": "2024-04-05T12:34:56Z",
  "status": 400,
  "error": "Bad Request",
  "message": "Email already exists",
  "path": "/users"
}
Enter fullscreen mode Exit fullscreen mode

πŸ“š Versioning

  • Best practice is to version via URI: /v1/users
  • Alternatively, use headers: Accept: application/vnd.api+json;version=1.0

πŸ§ͺ Rate Limiting

Header Description
X-Rate-Limit Requests allowed per minute
Retry-After Seconds to wait before retrying

πŸ›‘οΈ Security Best Practices

  • Use HTTPS for all requests
  • Validate and sanitize all inputs
  • Do not expose internal IDs directly (consider UUIDs)
  • Hash passwords using bcrypt or similar
  • Use CSRF protection if needed for browser-based clients

πŸ’¬ Common Interview Questions to Prepare

Area Sample Question
REST What are idempotent methods?
Auth How would you secure these APIs?
Headers What's the purpose of X-Request-ID or ETag?
Versioning How do you support multiple versions of an API?
Pagination How do you return paginated responses?
Error Handling How do you standardize error responses?
Rate Limiting How do you prevent abuse of APIs?

Top comments (0)