π Base URL: https://api.example.com/v1
π Authentication & Authorization
- π Auth Type: Bearer Token (JWT or OAuth 2.0)
- π₯ Header Required:
Authorization: Bearer <token>
- π« 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
π₯ Request Body:
None
π€ Response: 200 OK
HTTP/1.1 200 OK
Content-Type: application/json
X-Request-ID: 98219cda-21e8
[
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"createdAt": "2023-01-01T10:00:00Z"
}
]
2οΈβ£ GET /users/{id} β Get User by ID
β
Request
GET /users/1 HTTP/1.1
Authorization: Bearer eyJhbGci...
Accept: application/json
π₯ 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"
}
β Response: 404 Not Found
{
"error": "User with ID 99 not found"
}
3οΈβ£ POST /users β Create a New User
β
Request
POST /users HTTP/1.1
Authorization: Bearer eyJhbGci...
Content-Type: application/json
Accept: application/json
π₯ Request Body
{
"name": "Alice Smith",
"email": "alice@example.com",
"password": "SecureP@ss123"
}
π Password must be encrypted at backend before storing in DB
π€ Response: 201 Created
Location: /users/2
{
"id": 2,
"name": "Alice Smith",
"email": "alice@example.com"
}
β Response: 400 Bad Request
{
"error": "Email already in use"
}
4οΈβ£ PUT /users/{id} β Update a User
β
Request
PUT /users/1 HTTP/1.1
Authorization: Bearer eyJhbGci...
Content-Type: application/json
π₯ Request Body
{
"name": "Alice M. Smith",
"email": "alice.m@example.com"
}
π€ Response: 200 OK
{
"id": 1,
"name": "Alice M. Smith",
"email": "alice.m@example.com"
}
5οΈβ£ DELETE /users/{id} β Delete a User
β
Request
DELETE /users/1 HTTP/1.1
Authorization: Bearer eyJhbGci...
π€ Response: 204 No Content
No response body.
β 404 Not Found
{
"error": "User not found"
}
β οΈ Standard Error Format (used in all endpoints)
{
"timestamp": "2024-04-05T12:34:56Z",
"status": 400,
"error": "Bad Request",
"message": "Email already exists",
"path": "/users"
}
π 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)