API monitoring and error hygiene
Useful errors without internal details
Error hygiene means returning enough information for legitimate clients to respond without exposing internals or private information. A malformed request can receive HTTP 400; absent or invalid authentication normally receives 401 (with the applicable authentication challenge); an authenticated but forbidden request can receive 403. Some services use 404 to conceal resource existence under a consistent policy. Unexpected failures receive a generic 500 response.
Separate client response and internal event
For a fictional failed order lookup, the client receives a stable error code and request identifier. An internal structured event records the same identifier, route template, method, status, duration and an approved reason category. Record only necessary data, avoid raw tokens, passwords, session identifiers and full bodies. Access and retention for logs need their own policy.
Example: correlate without secrets
A request ID R17 appears in a generic client response and in two internal events: authorization denied and request completed with 403. The investigator can correlate them without the customer’s order contents. Monitor changes in error counts, denial rates, latency and quota rejections against a baseline. A spike is a signal to investigate, not proof of an attack; a deployment or client bug may explain it.
Common mistakes
Do not return stack traces, database errors or credentials to clients. Do not log every input verbatim; structured encoding and validation prevent untrusted text from being confused with event structure. Metrics should avoid high-cardinality or sensitive labels. Error handling must preserve authorization and avoid exposing private record existence inconsistently. Monitoring detects some failures; it does not replace prevention.
Practice: choose safe evidence
Given a fictional error record containing request ID, route template, status, authorization token and full order data, select fields for a routine log. Propose a generic client error and a monitoring question that distinguishes a client bug from a possible security issue. Explain which evidence is still missing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
API monitoring and error hygiene — knowledge check
Apply API security principles to fictional scenarios.
Which field is suitable for a routine correlation event?
What should an unexpected internal failure return to a client?
A denial-rate spike appears after deployment. What does it establish?
How should untrusted event values be recorded?
Why restrict access to logs?