An HTTP status code tells a client whether a request succeeded and gives a broad reason when it did not. It does not always explain which input needs fixing or how a client should handle a particular application error. If every endpoint invents a different JSON error shape, clients must learn and maintain several formats.
RFC 9457, Problem Details for HTTP APIs, defines a common JSON representation for these errors. It gives your API a consistent structure without replacing HTTP status codes or turning error messages into a debugging interface. RFC 9457 obsoletes RFC 7807, so use the newer specification when documenting an implementation.
What Problem Details adds
The response keeps its ordinary HTTP status and uses the application/problem+json media type for the body. A client can use the status for general behavior and inspect the JSON for details about the API-specific problem.
For example, an API that rejects a profile with an invalid email address could respond like this:
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/validation-error",
"title": "The request contains invalid fields.",
"status": 422,
"detail": "Correct the fields listed in the errors member.",
"errors": [
{
"detail": "must be a valid email address",
"pointer": "#/email"
}
]
}
The HTTP response is still a 422. The body describes the problem in a format that clients can recognize and extend.
Understand the standard members
RFC 9457 defines five standard members. They are optional, but each has a specific role:
typeidentifies the kind of problem with a URI. If omitted, it defaults toabout:blank, which means the problem has no additional meaning beyond the HTTP status. For custom problem types, use a stable URI you control and document what clients should do with it.titleis a short summary of the problem type. Keep it consistent for that type; localization is the main reason it might vary.statusrepeats the HTTP response status as a number. It is advisory, and a server generating the response must use the same status in the HTTP response itself. HTTP clients, proxies, and caches rely on that actual status.detailgives a human-readable explanation for this occurrence. Use it to help the client correct the request, not to expose debugging information. Clients should not parse this text to decide what action to take.instanceidentifies this particular occurrence. It can be an opaque URI that your support team can correlate with server-side logs.
An API does not need to invent a custom type for every ordinary HTTP response. Use a standard status alone, or the default about:blank type, when there is no useful application-specific problem to describe. A custom type is most useful when it communicates a distinct condition that clients can handle.
Add validation details as an extension
The standard does not prescribe one universal schema for field validation errors. It allows a problem type to define extension members, which means your API can add structured data without changing the standard members.
In the example, errors is an extension defined by the API. Each entry has a short explanation and a pointer to the invalid part of the JSON request. The #/email value follows the URI-fragment form of JSON Pointer. An API with a different request format or validation model can define a different extension, as long as it documents the shape.
Keep extension members machine-readable. For example, a client can highlight a field using its pointer, while detail remains available for a human-readable summary. Do not make clients infer a field name by searching an English error sentence. Clients must ignore extensions they do not recognize so the API can add information without breaking older consumers.
Return useful errors without leaking internals
Centralize the mapping from expected application errors to problem types. That makes it easier to keep the media type, HTTP status, and response fields consistent across routes.
For expected client errors, describe what the client can change: a missing field, an invalid value, an expired credential, or a resource that is not available. Choose the HTTP status that matches the request's meaning, then use a stable type and a documented extension if clients need structured details.
For unexpected server failures, return a generic problem and a request identifier rather than an exception message. Keep stack traces, SQL errors, file paths, internal hostnames, tokens, and private user data in appropriately protected server-side diagnostics—not in detail or an extension. If you include an instance or request ID, make it opaque and ensure that correlating it to logs does not expose those logs to the public.
The RFC 9457 security considerations specifically warn that error details and occurrence links can disclose information that helps compromise a system or violates user privacy. Treat every value in the response as public API output.
Handle Problem Details in a client
With the browser Fetch API, a 4xx or 5xx response does not by itself reject the fetch() promise. Check response.ok and inspect the response media type before treating the body as Problem Details:
const response = await fetch("/api/profile", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json, application/problem+json"
},
body: JSON.stringify({ email: "not-an-email" })
});
if (!response.ok) {
const mediaType = (response.headers.get("content-type") ?? "")
.split(";", 1)[0]
.trim()
.toLowerCase();
if (mediaType === "application/problem+json") {
const problem = await response.json();
if (problem.type === "https://api.example.com/problems/validation-error") {
renderFieldErrors(problem.errors ?? []);
return;
}
}
showMessage("We could not save your profile.");
return;
}
const profile = await response.json();
In production code, handle network failures and invalid or empty JSON in the same request boundary. A proxy or older endpoint may return a different body even for an error status. Fall back to a safe generic message instead of assuming every error response follows RFC 9457. Render any server-provided text as text, not executable HTML.
Branch on the actual HTTP status and the stable type URI for behavior. Treat detail and title as display text, not a machine protocol. A client that recognizes a validation type can show field-level feedback; one that does not can still present a generic error.
Adopt it without breaking existing clients
If you are adding Problem Details to an existing API, document which endpoints return it and whether older error formats remain supported. Changing an error body can break clients even when the status code is unchanged, so treat the response shape as part of your API contract. Our guide to API versioning and backward compatibility explains how to assess that kind of change.
Add focused tests for the contract:
- The HTTP status matches the
statusmember when one is present. - The response has
Content-Type: application/problem+json. - Each custom
typeand extension has the documented meaning and shape. - Unexpected failures do not return stack traces, raw exception messages, secrets, or private data.
- Clients handle a recognized problem type, an unknown type, and an error response with a different media type.
Keep the examples and assertions alongside your API contract and test workflow. The existing guide to API testing and documentation for small teams covers a lightweight way to keep those artifacts aligned.