API Error Handling
What is API error handling?
API error handling is the way software detects, describes, and responds to failed API requests. A server returns a clear result, and the client app decides what to show or do next.
HTTP API error codes: 400, 404, 429, and 500
Status codes identify the failure category.
| Code | Meaning | Client action |
|---|---|---|
| 2xx | Success | Use the response. |
| 3xx | Redirect | Follow the new location when appropriate. |
| 400 | Invalid request | Correct missing or invalid input. |
| 401 / 403 | Not signed in / not permitted | Authenticate or request access. |
| 404 | Resource not found | Check the endpoint or item ID. |
| 429 | Too many requests | Wait, then retry as directed. |
| 500 / 503 | Server failure / unavailable | Retry cautiously or report it. |
How to handle API errors
- Detect a failed HTTP response or network failure.
- Read the standard error payload.
- Show a plain-language, user-safe message.
- Retry only temporary failures such as 429 or 503.
- Log the request ID and diagnostic context.
- Escalate unexpected failures for investigation.
What a useful API error response includes
Use a consistent schema containing an HTTP status, stable application code, short message, validation details, and request ID. RFC 9457 Problem Details is one optional format. Keep stack traces and secrets in private logs, not user responses.
API error-handling best practices
Reliable APIs make failure states predictable.
- Validate input before processing an API request.
- Document errors in API documentation.
- Return retry guidance, including Retry-After for rate limits.
- Protect credentials during API authentication.
- Test known failure paths and idempotent retries.
Practical limits and common mistakes
Not every API error can be fixed by the client.
- Repeated retries can worsen an overloaded service.
- A generic 500 hides useful diagnosis.
- Stack traces can expose sensitive internals.
- A 200 response with an error body confuses standard tools.
API error handling in Emergent
When building full-stack apps and integrations with Emergent, define and test user-facing failure states alongside successful flows. Build and test API responses with Emergent.
on Emergent today

