Error Handling

Error Handling Guide

Learn how to handle errors gracefully and build resilient integrations.

Implement Retries

Use exponential backoff for transient errors (5xx, 429).

Validate Inputs

Validate all inputs client-side before making API calls.

Handle Rate Limits

Respect rate limit headers and implement queuing.

Log Errors

Log all API errors with request IDs for debugging.

HTTP Status Codes

Code
Status
Description
Solution
400
Bad Request
The request was malformed or missing required parameters.
Check your request body and ensure all required fields are present.
401
Unauthorized
Invalid or missing API key.
Verify your API key is correct and included in the Authorization header.
403
Forbidden
API key lacks permission for this action.
Check your API key permissions in the dashboard.
404
Not Found
The requested resource does not exist.
Verify the endpoint URL and resource ID.
429
Rate Limited
Too many requests. You've exceeded your rate limit.
Implement exponential backoff and respect rate limit headers.
500
Server Error
An internal server error occurred.
Retry the request. If persistent, contact support.
503
Service Unavailable
The service is temporarily unavailable.
Retry after a short delay. Check status.smsly.cloud for updates.

Implementation Example

error-handling.js
// Error handling example (JavaScript)
async function sendSMS(to, message) {
  const maxRetries = 3;
  let attempt = 0;
  
  while (attempt < maxRetries) {
    try {
      const response = await fetch('https://api.smsly.cloud/v1/messages', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${API_KEY}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ to, message })
      });
      
      if (!response.ok) {
        const error = await response.json();
        
        // Handle specific error codes
        switch (response.status) {
          case 400:
            throw new Error(`Invalid request: ${error.message}`);
          case 401:
            throw new Error('Invalid API key');
          case 429:
            // Rate limited - wait and retry
            const retryAfter = response.headers.get('Retry-After') || 1;
            await sleep(retryAfter * 1000);
            attempt++;
            continue;
          case 500:
          case 503:
            // Server error - retry with backoff
            await sleep(Math.pow(2, attempt) * 1000);
            attempt++;
            continue;
          default:
            throw new Error(error.message);
        }
      }
      
      return await response.json();
    } catch (error) {
      if (attempt === maxRetries - 1) throw error;
      attempt++;
    }
  }
}

Need Help?

Our support team is here to help you troubleshoot any integration issues.