API REST - How to write good APIs
When developing APIs from scratch, you can do it in the mode I get behind the PC and start programming or I learn about the standards first and then program in compliance with these standards.
And if we lose sight of these norms, we quickly come back to writing endpoints like /articles/insert
that are semantically incorrect (and even stupid). In fact, we'll never GET
here.
The purpose of the article below is to list best practices by way of examples.
Main conceptsโ
Resourcesโ
Don't use verbs in URLsโ
HTTP has verbs like GET
, POST
, DELETE
, ... so don't use calls like:
/articles/gettitle
,/articles/insert
,/articles/1/delete
But use the adequate HTTP verb:
GET /articles/title
,PUT /articles
,DELETE /articles/1
Prefer plural nounโ
Always use the plural like GET /employees/1
to return the first employee and not GET /employee/1
.
Using the plural make it easier to understand that employees
is a collection and we can use verbs like GET
to get all, one or a range, POST
to add a new employee, PUT
to update a employee's information.
Send the HTTP Accept headerโ
It's really recommended to inform the server about what you expect for: json
, csv
, plain-text
, xml
, ... To do this, use the Accept
header like in the example here above. This is safer because the server can change his default format from JSON to CSV f.i. and if you expect JSON, your code will be broken.
Don't do this:
const employee = axios.create({
baseURL: 'http://localhost:3000/employees/123'
})
But well:
const employee = axios.create({
baseURL: 'http://localhost:3000/employees/123',
headers: {
'Accept': 'application/json'
}
})
Now, in the second example, you tell the web server that you want a JSON representation and nothing else.
Responsesโ
200 OKโ
A server MUST respond to a successful request to fetch an individual resource or resource collection with a 200 OK
response.
When DELETE
was used, we can also return 204 No Content
to inform the calling application that deletion was successful.
201 Createdโ
If the requested resource has been created successfully and the server changes the resource in any way (for example, by assigning an id), the server MUST return a 201 Created
response and the new added resource in body.
202 Acceptedโ
If a request to create/update or delete a resource has been accepted for processing, but the processing has not been completed by the time the server responds, the server MUST return a 202 Accepted
status code.
204 No Contentโ
If the requested resource has been created successfully and the server does not change the resource in any way (for example, by assigning an id or createdAt
attribute), the server MUST return either a 201 Created
status code and the resource in response or a 204 No Content
status code with no response document.
If the deletion was done successfully, 204 No Content
can be used too.
For an update, it's the same: we can send code 200 OK
with the updated resource in the body or, code 204 No Content
without body; just headers.
400 Not Foundโ
You should return a 400 Bad Request
response when the call was incorrect like an unknown value is used for a parameter (like &lang=it
when Italian isn't supported).
403 Forbiddenโ
A server MUST return 403 Forbidden
in response to an unsupported request to, for instance, create a resource (with POST
verb with a client-generated ID
since the ID
has to be generated by the server, not the client).