Should I use nested routes or not?

Viewed 69

The endpoints I'm planning to use in my document taking app are listed below. I'm currently debating with myself whether I should include /users/{id}/documents or only /documents. /users/{id}/documents would mean that my routes are very nested and it would be harder to maintain.

So what do you think, does my api design look good, or is there anything you would change?

GET /documents
POST /documents/new
PUT /documents/{id}/save
DELETE /documents/{id}/delete
PUT /documents/{id}/share

POST /auth/signup
POST /auth/login
GET /auth/loggedin
POST /auth/logout

PUT /users/{id}/password/update
PUT /users/{id}/email/update
PUT /users/{id}/username/update
1 Answers

The HTTP verbs also have some meaning.

For Example when you use POST for /documents it means you want to create one. so you don't need the /documents/new. so we can rewrite the routes as bellow:

GET /documents
POST /documents
PUT /documents/{id}
DELETE /documents/{id}
PUT /documents/share/{id}

POST /auth/signup
POST /auth/login
GET /auth/loggedin
POST /auth/logout

PUT /users/{id}/password
PUT /users/{id}/email
PUT /users/{id}/username

But it's totally up to you have to choose your endpoints. Some programmers rarely use PUT or DELETE method.

For example they use POST for delete as /documents/delete/{id}

I personally like to use all kind of HTTP methods

Related