Backend13 min read11,234 views

REST API Design Best Practices

Learn how to design clean, consistent REST APIs. Covering naming conventions, error handling, versioning, authentication, and documentation.

Youssef Boubli

Youssef Boubli

January 30, 2024

Share:
REST API Design Best Practices

Introduction

A well-designed API is a joy to use. It's intuitive, consistent, and follows established conventions. Let's explore the best practices for designing REST APIs that developers love.

Resource Naming

Use nouns, not verbs. Resources are things, not actions.

bash

1# ✅ Good - nouns
2GET /users
3GET /users/123
4POST /users
5PUT /users/123
6DELETE /users/123
7
8# ❌ Bad - verbs
9GET /getUsers
10POST /createUser
11PUT /updateUser/123
12DELETE /deleteUser/123

#

Plural vs Singular

Use plural nouns consistently:

bash

1GET /users # Collection
2GET /users/123 # Single resource

#

Nested Resources

For related resources:

bash

1GET /users/123/posts # All posts by user 123
2GET /users/123/posts/456 # Specific post by user
3POST /users/123/posts # Create post for user

HTTP Methods

Use the right method for each action:

| Method | Purpose | Idempotent |

|--------|---------|------------|

| GET | Read resource | Yes |

| POST | Create resource | No |

| PUT | Replace resource | Yes |

| PATCH | Partial update | Yes |

| DELETE | Delete resource | Yes |

HTTP Status Codes

Return appropriate status codes:

javascript

1// Success
2200 OK // General success
3201 Created // Resource created
4204 No Content // Successful delete
5
6// Client Errors
7400 Bad Request // Invalid input
8401 Unauthorized // No/invalid auth
9403 Forbidden // Not allowed
10404 Not Found // Resource not found
11422 Unprocessable // Validation failed
12
13// Server Errors
14500 Internal Error // Generic server error
15503 Service Unavailable

Error Handling

Return consistent error responses:

json

1{
2 "error": {
3 "code": "VALIDATION_ERROR",
4 "message": "The request body contains invalid data",
5 "details": [
6 {
7 "field": "email",
8 "message": "Must be a valid email address"
9 },
10 {
11 "field": "password",
12 "message": "Must be at least 8 characters"
13 }
14 ]
15 }
16}

Pagination

For list endpoints, always paginate:

bash

1GET /users?page=2&limit=20

Response:

json

1{
2 "data": [...],
3 "pagination": {
4 "page": 2,
5 "limit": 20,
6 "total": 156,
7 "totalPages": 8
8 }
9}

Filtering and Sorting

Support query parameters:

bash

1# Filtering
2GET /users?status=active&role=admin
3
4# Sorting
5GET /users?sort=createdAt&order=desc
6
7# Searching
8GET /users?search=john

API Versioning

Version your API to avoid breaking changes:

bash

1# URL versioning (recommended)
2GET /v1/users
3GET /v2/users
4
5# Header versioning
6GET /users
7Accept: application/vnd.myapi.v1+json

Authentication

Use appropriate auth methods:

bash

1# Bearer token (JWT)
2Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
3
4# API Key (for service-to-service)
5X-API-Key: your-api-key

Rate Limiting

Protect your API with rate limits:

bash

1HTTP/1.1 429 Too Many Requests
2X-RateLimit-Limit: 100
3X-RateLimit-Remaining: 0
4X-RateLimit-Reset: 1609459200

Documentation

Document your API with OpenAPI/Swagger:

yaml

1openapi: 3.0.0
2info:
3 title: User API
4 version: 1.0.0
5paths:
6 /users:
7 get:
8 summary: List all users
9 parameters:
10 - name: page
11 in: query
12 type: integer
13 responses:
14 200:
15 description: List of users

Conclusion

Well-designed APIs are predictable, consistent, and self-documenting. Follow these conventions and your API consumers will thank you.

Tags

APIRESTBackendWeb DevelopmentBest Practices
Youssef Boubli

About the Author

Youssef Boubli

Front End Developer & UI/UX Designer from Morocco

View Full Profile →