Safeguard
Security Guides•

What should an API error response include and hide?

Return the right status code and a generic message; keep stack traces and internals in logs. A table of status codes and what each should reveal.

Safeguard Security Team
3 min read

An API error should carry the correct HTTP status code, a short generic message and a request ID. It should never include stack traces, SQL, file paths, library versions or internal hostnames. Keep detail in server logs, where your team can use it and attackers cannot.

What the guidance says

OWASP's REST Security Cheat Sheet says to return generic error messages that do not reveal unnecessary detail, never to send stack traces or internal hints to clients, and to use semantically correct codes rather than defaulting to 200 or 404. OWASP API8 adds that teams should define and enforce response schemas, including error responses, so sensitive details never reach clients.

Status codes that matter for security

CodeUse it whenWhat to reveal
400Request is malformedWhich field failed, not how the parser failed
401Credentials are missing or wrongNothing about whether the account exists
403Caller is authenticated but not permittedNothing about the resource
405Method is not allowedThe allowed methods, if you wish
406No acceptable response typeNothing extra
413Payload too largeThe limit
415Unsupported content typeThe supported types
429Too many requestsWhen to retry
500Unexpected server faultNothing internal, only a request ID

The 401 and 403 pair is defined in the cheat sheet as missing or wrong credentials versus authenticated but not permitted.

Mistakes to look for

  1. Stack traces in 500 responses. Common when debug mode ships to production.
  2. Different messages for unknown user and wrong password. This lets attackers list valid accounts.
  3. Database errors echoed back. They teach attackers your schema.
  4. Success status with an error body. Monitoring and clients cannot tell.
  5. Inconsistent errors across versions or hosts. An old version may still leak detail.

A simple pattern

Return a small error object with a stable code, a message written for humans and a request ID. Log the full exception with that same ID. Support can find it, and the client sees nothing internal.

How to test it

Send malformed JSON, wrong types, oversized bodies, unsupported content types and unexpected methods. Read every response body. Anything that describes your internals is a finding. A spec driven scanner does much of this automatically on documented routes.

Quick check

Should I return 404 instead of 403 for other people's records?

It is a design choice. 404 avoids confirming that a record exists. Whichever you choose, apply it consistently and cover it in authorization tests.

Is it fine to show validation details?

Yes, field level messages help legitimate clients. Avoid exposing parser internals, regular expressions or server paths.

Safeguard's free scan at gold.safeguard.sh/scan covers dependency risk, including libraries that surface debug output.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.