Check the Tips and Tricks to understand how to use this sample API description.
Here you will probably give an overview of the API and add a description for it.
Since the API requires authentication, you might want to add a section to describe the authentication flow as well.
Now this is the important section in this example. In this section, you can list the error responses that appear in every operation with some explanation. It can go sth like:
These errors are returned with the status code 401 whenever the authentication fails or a request is made to an endpoint without providing the authentication information as part of the request. Here are the 2 possible errors that can be returned.
{
"type": "client_error",
"errors": [
{
"code": "authentication_failed",
"detail": "Incorrect authentication credentials.",
"attr": null
}
]
}
{
"type": "client_error",
"errors": [
{
"code": "not_authenticated",
"detail": "Authentication credentials were not provided.",
"attr": null
}
]
}
This is returned when an endpoint is called with an unexpected http method. For example, if updating a user requires a POST request and a PATCH is issued instead, this error is returned. Here's how it looks like:
{
"type": "client_error",
"errors": [
{
"code": "method_not_allowed",
"detail": "Method “patch” not allowed.",
"attr": null
}
]
}
This is returned if the Accept
header is submitted and contains a value other than application/json
. Here's how the response would look:
{
"type": "client_error",
"errors": [
{
"code": "not_acceptable",
"detail": "Could not satisfy the request Accept header.",
"attr": null
}
]
}
This is returned when the request content type is not json. Here's how the response would look:
{
"type": "client_error",
"errors": [
{
"code": "not_acceptable",
"detail": "Unsupported media type “application/xml” in request.",
"attr": null
}
]
}
This is returned when the API server encounters an unexpected error. Here's how the response would look:
{
"type": "server_error",
"errors": [
{
"code": "error",
"detail": "A server error occurred.",
"attr": null
}
]
}