Proper route for checking resource existence in a RESTful API

x1a0 picture x1a0 · Jan 13, 2014 · Viewed 20.5k times · Source

What's the best/restful way to design an API endpoint for checking existence of resources?

For example there is a user database. While new user tries to sign up I want to check if email has been used on-the-fly.

My idea is: POST /user/exists and payload would be something like {"email": "[email protected]"}. The response would be either 200 OK or 409 Conflict.

Is this a proper way?

Thanks!

Answer

Baz picture Baz · Nov 28, 2016

HEAD is the most effecient for existence checks:

HEAD /users/{username}

Request a user's path, and return a 200 if they exist, or a 404 if they don't.

Mind you, you probably don't want to be exposing endpoints that check email addresses. It opens a security and privacy hole. Usernames that are already publicly displayed around a site, like on reddit, could be ok.