2

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?

user3147268
  • 1,814
  • 7
  • 26
  • 39

1 Answers1

8

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.

pBuch
  • 986
  • 1
  • 6
  • 20
  • 2
    If you want, you can also create a route to a search, such as `example.com/api/users/search?name=johndoe`. – Scott Offen Apr 24 '15 at 16:21
  • @pbuch This is a good reason to stick with a static user id. Mostly developer want be in the situation, where they need to call an api manually. So this should not be a big problem. – user3147268 Apr 24 '15 at 17:26
  • 1
    I was having the same problem. The combination of pBuch's answer (use the ID) and ScottOffen's suggestion to add a search for the name seem to work well. I've tried it out and it seems quite intuitive as far as the URL's go. – Simon Elms May 18 '16 at 05:52