Skip to main content

HTTP status codes

Handling errors properly is crucial in API design. APIs should use standard HTTP status codes and provide clear error messages.

Overview of HTTP status codes

Status CodeStatusDescriptionSet by GatewaySet by Backend
2xx Success
200 OKSuccessThe API call was successful.NoYes
201 CreatedSuccessA new entity was successfully created.NoYes
202 AcceptedSuccessThe request was accepted but has not yet been processed.NoYes
204 No ContentSuccessThe request was successful, but no content is returned.NoYes
4xx Client Error
400 Bad RequestErrorThe request could not be processed due to a client error.YesYes
401 UnauthorisedErrorInvalid API key or authentication token.YesYes
403 ForbiddenErrorThe client does not have permission to access the resource.YesYes
404 Not FoundErrorThe requested resource does not exist.YesYes
405 Method Not AllowedErrorThe HTTP method used is not supported for this endpoint.YesYes
415 Unsupported Media TypeErrorThe Content-Type header is incorrect.YesYes
429 Too Many RequestsErrorThe request quota was exceeded (rate limiting).YesNo
5xx Server Error
500 Internal Server ErrorErrorAn unexpected backend failure occurred.NoYes
502 Bad GatewayErrorThe backend received an invalid response.YesYes
503 Service UnavailableErrorThe backend is temporarily unavailable. A retry may be required.NoYes
504 Gateway TimeoutErrorThe backend did not respond within the required timeframe.YesYes

Error messages

APIs should return clear and descriptive error messages in JSON format when an error occurs.

Convention: Return error messages in JSON format.

Example (401 Unauthorized)

{
"errors": [
{
"errorCode": "xyz",
"errorMessage": "Sorry, this is not working",
"correlationId": "12sdfkjsdkjjsdf",
"link": "https://developer.wpp.com/api/ambee/errorcodes/xyz"
}
]
}

Do not return stack traces, as they may contain sensitive information.

Example scenarios

GET request identifying a set of resources

GET /v1/senders?first_name=Peter&last_name=Brown
Response (200 OK)
{
"data": [
{
"id": "1",
"firstName": "Peter",
"lastName": "Brown",
"city": "London"
},
{
"id": "2",
"firstName": "Peter",
"lastName": "Brown",
"city": "New York"
}
]
}
Response (Empty List, 200 OK)
{
"data": []
}
Response (400 Bad Request)
{
"errors": [
{
"status_code": 400,
"detail": "'id' value is not a number"
}
]
}