Wednesday, May 28, 2025

REST Resource naming convention

 n REST API design, the naming of resources (URLs) should follow consistent and intuitive conventions. When dealing with pluralization, the general best practice is to use plural nouns for resource names, as they represent collections of entities.

Should you use training or trainings in the URL?

  • Use /trainings (plural) when referring to a collection of training resources.

    • ✅ Correct: GET /trainings (retrieve all trainings)

    • ✅ Correct: POST /trainings (create a new training)

    • ✅ Correct: GET /trainings/{id} (retrieve a specific training)

  • Avoid /training (singular) for collections, as it suggests a single resource rather than a collection.

    • ❌ Incorrect (for collections): GET /training

    • ❌ Incorrect (for collections): POST /training

Why Plural (/trainings)?

  1. Consistency with REST conventions – Most REST APIs use plural nouns (e.g., /users/products).

  2. Clarity – GET /trainings clearly indicates you're fetching a list, whereas GET /training could imply a singleton resource.

  3. Predictability – Easier for developers to understand and follow standard patterns.

When to Use Singular (/training)?

Only if you're referring to a singleton resource (a single, global entity). For example:

  • GET /training/config (a single configuration resource for training)

  • PUT /training/settings (global training settings)

But in most cases, /trainings is the correct choice for typical CRUD operations on multiple training records.

Final Recommendation

✅ Use /trainings for standard REST endpoints dealing with multiple training resources.
✅ Use /training/{id} (singular) only when referring to a single training instance.

Example RESTful structure:

Copy
Download
GET    /trainings        → List all trainings  
POST   /trainings        → Create a new training  
GET    /trainings/{id}   → Get a specific training  
PUT    /trainings/{id}   → Update a training  
DELETE /trainings/{id}   → Delete a training  

This follows REST best practices and keeps your API intuitive and consistent.

No comments:

Post a Comment