Naming convention for RESTful API specs

When defining API specs for RESTful APIs, there are some naming conventions that are best practices that you should follow to make these API specs consistent, intuitive, and easy to maintain.

These principles are as follows:

  • Use nouns instead of verbs when naming resources

Instead of /getStudents, you should name /students

  • Use plural nouns for collections of resources, singular for a resource

Instead of /student, you should name /students for collections, /students/{id} for a resource.

  • Use the HTTP method to define actions
    • GET /students → Get list of students
    • GET /students/{id} → Get information of a student
    • POST /students → Add new student
    • PUT /students/{id} → Update student information
    • PATCH /students/{id} → Update partial of student information
    • DELETE /students/{id} → Delete information of a student
  • Use lowercase with hyphens; do not use camelCase or snake_case

Instead of /studentProfiles or /student_profiles, we should name it as /student-profiles

  • If your resources are related to each other, name them hierarchically with slashes

Instead of /getStudentSubjects to get subject information for students, you can name /students/{id}/subjects or /students/{id}/subjects/{id}

  • Use query parameters to filter, sort, paginate, search, …

For example: /students?classId=1A&sort=createdAt&limit=10&page=2

These are the basic principles. Please try to follow them!

Add Comment