Search code examples
restrestful-urlnested-routes

Name or ID as url for restful api


I currently build a restful api with a users route. Is it more idioatic to use the username or his id as parameter if the username is unique?

Either

example.com/api/users/johndoe

or

example.com/api/users/123456

Furthermore I planed routes for a project resource. Would it be an inconsistent design, if I require the project-id here?


Solution

  • Usually a username can be remembered more easily but I would recommend you to use the ID as in a username there may be spaces or other characters (for example german umlaute) which are difficult to get into good format for an url. It may also be, that a user changes his name. Then all the links would be invalid in opposite to the ID.