Image by Author

REST API Best Practices

Architecting Robust and Scalable RESTful APIs

Yeran Kods
Nerd For Tech
Published in
3 min readFeb 11, 2024

--

1. Use Descriptive and Meaningful URIs

Bad Practice

/getUsers

Best Practice

/users

2. Use HTTP Methods Appropriately

GET: Retrieve a resource (e.g., GET /users/123 ->fetches user with ID 123).

POST: Create a new resource (e.g., POST /products ->creates a new product).

PUT: Update an existing resource (e.g., PUT /users/123 ->updates user 123).

DELETE: Delete a resource (e.g., DELETE /orders/456 ->deletes order 456).

Bad Practice

GET /createUser

Best Practice

POST /users

3. Resource Naming Conventions

Bad Practice

/getMyData

Best Practice

/my-data

4. Use Plural Nouns for Collections

Clear and meaningful names: Replace generic terms like “get” with specific resource names.

Plural nouns for collections: Use “users” instead of “user” for a list of users.

Consistent structure: Maintain a consistent format throughout the API.

GET /api/v1/users/{id}                // Retrieve user with ID {id}
POST /api/v1/products // Create a new product
PUT /api/v1/orders/{id}/payment // Update payment status for order {id}
DELETE /api/v1/posts/{id}/comments/{commentId} // Delete comment {commentId} from post {id}

Bad Practice

/user

Best Practice

/users

5. Versioning

Bad Practice

/api/v1/getData

Best Practice

/v1/data

6. Use HTTP Status Codes Appropriately

Bad Practice

  • Returning 200 OK for an error.

Best Practice

  • Use 404 Not Found for resources not found.
  • Use 201 Created for successful resource creation.

7. Request and Response Formats

Bad Practice

  • Not specifying response format in headers.

Best Practice

  • Use Accept and Content-Type headers to specify JSON or XML.

8. Use Proper Pagination

Bad Practice

/getAllUsers?page=1&pageSize=10

Best Practice

/users?offset=0&limit=10

9. Handle Errors Properly

Standardized data formats: Use common formats like JSON for consistency and ease of integration.

Descriptive error messages: Provide clear and actionable error messages with HTTP status codes (e.g., 404 Not Found, 400 Bad Request).

Pagination and filtering: Allow clients to request data in manageable chunks and filter results based on specific criteria.

{
"status": 400,
"error": "Validation failed",
"message": "Product name is required and must be between 3 and 50 characters."
}

Bad Practice

  • Returning generic error messages.

Best Practice

  • Provide detailed error messages with status codes.

10. Use HTTP Authentication

Bad Practice

/getData?apiKey=xyz

Best Practice

Authorization: Bearer <token>

11. Security

Bad Practice

  • Storing sensitive data in URL parameters.

Best Practice

  • Use HTTPS for secure communication.
  • Store sensitive data in the request body or headers.

12. Use Consistent Naming Conventions

Bad Practice

/getUser, /fetchUserData, /retrieve_user

Best Practice

/users

13. HATEOAS (Hypermedia as the Engine of Application State)

Bad Practice

  • Not providing links to related resources.

Best Practice

  • Include links in the response for easy navigation.

14. Request Payload for Updates

Bad Practice

  • Sending the entire resource for a partial update.

Best Practice

  • Use PATCH method or send only the fields that need updating.

15. Documentation

Bad Practice

  • Lack of API documentation.

Best Practice

  • Provide clear and comprehensive API documentation (e.g., Swagger, OpenAPI).

Additional Best Practices:

  • Statelessness: REST APIs should be stateless, meaning each request should contain all necessary information.
  • Hateoas (Hypermedia as the Engine of Application State): Provide links in responses to help clients discover other related resources.
  • Testing: Thoroughly test your API with various tools and scenarios.

Hope you learned something new.❤️

Connect with me via;

--

--

Yeran Kods
Nerd For Tech

Interest is what inspires.🌍 | New articles every week !