This document covers the best practices on designing a RESTful API (with the exception for versioning).
- A URL (Uniform Resource Locator) identifies a single resource.
- A URL represents a hierarchical relationship between resource's data.
- REST is based on HTTP heavily, using all it's features (verbs, status codes)
- In general, you should not go deep in resource description.
/resource/identifier/related_resource
is enough in general - Default format should be JSON. It may be overriden, though.
- Ideally, the API server should be stateless (and then your endpoints are RESTful)
GET
- read/retrieve a resource
POST
- create a new resource
PUT
- update an existing resource
DELETE
- delete an existing resource
HTTP METHOD | GET | POST | PUT | DELETE |
---|---|---|---|---|
/orders | show all orders | create new order | bulk update | delete all orders |
/orders/123 | show order 123 | BAD REQUEST | if exist - update order, if not - BAD REQUEST | delete order 123 |
Good:
DELETE /cars/1234
Bad:
POST /cars/1234/delete
Good:
/cars/711
Bad:
/getCars?id=711
Good:
/users/1234
/users/1234/cars
Bad:
/user/1234
/users/1234/car
Good:
GET /orders/1234
POST /orders/1234/activate
Bad:
GET /orders/1234?activate=1
GET /orders/1234/activate
NEVER release unversioned api for public usage. Avoid dot-notation:
GOOD:
/api/v1/communities
BAD:
/api/communities
/api/v21.01.2014/communities
Code | Status | Meaning |
---|---|---|
200 | OK | Everything is working as intended |
201 | OK | New resource have been created |
204 | OK | Resource was successfully deleted |
400 | BAD REQUEST | The request was invalid or cannot be served. The exact error should be explained in the error payload. E.g. „The JSON is not valid“ |
401 | UNAUTHORIZED | The request requires an user authentication |
403 | FORBIDDEN | The server understood the request, but is refusing it or the access is not allowed. |
404 | NOT FOUND | There is no resource under the specified URL |
422 | UNPROCESSABLE ENTITY | Should be used if the server cannot process the payload, e.g. if required fields are missing |
500 | INTERNAL SERVER ERROR | Avoid using this code directly. Should be used when server has encountered a crash condition. Note: the stracktrace should be logged and not returned as response. |
Good:
{
"success" : {
"data" : { // payload },
"message" : ""
}
}
{
"errors" : [
{
"developerMessage" : "Request payload cannot be parsed",
"userMessage" : "The data you have provided cannot be parsed",
"more_info": "http://devdocs.myhost.com/payload"
}]
}
Good:
/orders/1234?sort=+customer,-price
/orders/1234?fields=customer,price,vat
/orders?limit=25&offset=50
Bad:
/orders/1234/sorted/asc
/orders/limit/25/offset/50
Good:
/users/1234/orders
/orders/321
Bad:
/users/1234/orders/321
- JSON
/orders
- XML
/orders.xml
- JSONP
/orders?callback=jsonp_callback
This guidelines is based on these resources: