Email address as resource identifier in a REST API

2.1k views Asked by At

Say my application has users, and each user is uniquely identified by their email address. In that case, it makes sense to use the natural key of email rather than using an auto incrementing ID as the primary key, which is common in most systems.

Then, in my REST API, would it be okay to access a particular user like this?

https://api.whatever.com/v1/users/[email protected]

Traditionally, you'd use the user ID in place of [email protected], but in my application the email address is the unique identifier for user accounts.

What would you pick? Using email as a natural key in REST URLs or create a user ID field to use instead?

2

There are 2 answers

2
Hans Kilian On BEST ANSWER

I would use a user id. A person and their email address don't have a 1 to 1 relationship and they don't have the same life cycle, so using one as the identifier for the other is a bad idea.

People may have more than one email. Maybe they don't have an email account (it happens). Maybe they change their email. If you use the email address as the key, you might run into problems later on.

3
overflowed On

Should be url encoded https://api.whatever.com/v1/users/john%40smith%2Ecom

Else fetching by id could by faster than by mail, and maybe you want to open the API to another application person, that should get the knowledge of the E-Mail address