How to Handle API Errors Gracefully: HTTP Status Codes Every Developer Should Know
By Nimana · Last updated November 18, 2026 · 3 min read
Learn what 400, 401, 403, 409, 422, 429, and 500 errors mean for a messaging API, which ones to retry, and how to show users clear messages instead of failures.
Why This Matters
Every API call can fail. The network drops, a key is wrong, a phone number is invalid, you send too fast. What matters is what your app does next. An app that crashes, retries forever, or shows "Something went wrong" loses messages and frustrates users. An app that reads the error and responds sensibly keeps working.
Good error handling starts with understanding the status codes.
The Three Families of Status Codes
2xxmeans success. The request worked.4xxmeans a problem with the request. Something you sent, or how you sent it, needs to change.5xxmeans a problem on the server's side. These are usually rare and often temporary.
This one distinction tells you what to do: fix the request, or try again later.
The Codes You Will See Most
| Code | Meaning | What to do |
|---|---|---|
| 200 / 201 | Success, or something was created | Carry on |
| 400 | Bad request, often a missing or invalid parameter | Fix the request. Retrying will not help |
| 401 | No valid API key | Check your key and the Authorization header |
| 403 | The key is not allowed to do this | Check the key's permissions |
| 404 | Not found | Check the ID or the URL |
| 409 | Conflict with another request | Check what you sent, for example a reused idempotency key |
| 422 | The request was understood but could not be completed | Read the message, for example an invalid phone number |
| 429 | Too many requests | Wait, then retry with backoff |
| 500 | A problem on the server | Retry a few times, then report it |
Which Errors Are Worth Retrying?
Only some errors can fix themselves:
- Retry:
429and5xx, with exponential backoff, and network timeouts. - Do not retry:
400,401,403,404,409, and422. The same request will fail the same way. Fix the cause first.
Six Tips for Clean Error Handling
- Read the error body, not just the status code. Good APIs explain what went wrong.
- Handle each family differently. Retry temporary problems, fix permanent ones, and alert yourself to the rest.
- Make retries safe. Use an idempotency key so a retried send never delivers twice.
- Log every error with its request ID. When you need help, the ID lets support find the exact request.
- Validate before you send. Check required fields and phone number formats yourself, so you catch problems early.
- Show users plain language. "We could not send your code. Please check your number and try again" beats "Error 422".
Common Mistakes
- Retrying every error the same way, including ones that can never succeed
- Swallowing errors silently, so a failing send goes unnoticed
- Showing raw error messages to users
- Not logging enough detail to debug later
How SendAPI Helps
SendAPI uses these conventional status codes, and every failed request returns the same structured response:
{
"success": false,
"error": {
"type": "validation_error",
"code": 422,
"message": "The given data was invalid.",
"errors": {
"to": ["The to field is required."]
}
},
"meta": {
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": "2026-03-16T15:00:00.000000Z"
}
}
A few details that match the tips above:
- A
typeand a numericcodeon every error, such asvalidation_error,authentication_error,rate_limit_exceeded, andemail_quota_exceeded. The numeric codes match SendAPI's error codes reference, so they are easy to handle in code. - Field-level messages on
422validation errors, with a map of each field name to its problems. - A
request_idin themetasection of every response, so you can log it and quote it when you ask for help. - Clear meanings for the common codes: a
403means the API key lacks permission for the request (for example, sending an email with an SMS-only scoped key), a409means a conflict such as reusing an idempotency key for a different payload, and a422can mean the recipient number is invalid. 429means back off and retry, and the response includes aRetry-Afterheader.
All of this comes with WhatsApp, SMS, OTP, and email under one API from $9.99 a month.