Search code examples
restrestful-architecturerestful-url

How specific does an API's routing need to be?


I'm creating an API, I have a user that I can request by calling:

/api/request/user/{id}

This works fine.

Say each user had a set of skills, could be one skill or 100.

I want to get all the skills for a specific user.

Would I be super specific and make it so that you'll have to call:

/api/request/user/{id}/skills

Or does this:

/api/request/skills/{id} (where {id} is the user's id)

suffice?

Is there a REST API standard I should be following or is it flexible ?

Any advice is highly appreciated, Thank you.


Solution

  • As skills is user's property, let's consider it as sub-resource. Getting sub-resources is based on nesting URL (hierarchical way), so /api/request/user/{id}/skills would be great!

    In /api/request/skills/{id} case id should be an identifier of skill to make your API intuitive.

    Also remember about naming convention. Be consistent, your skills resource is plural so let's rename user to users.

    So, to sum up

    /api/request/users/{user_id}

    /api/request/users/{user_id}/skills

    /api/request/skills/{skill_id}

    would be nice.