Error Codes

This page provides a comprehensive list of error codes that may be returned by the WhatsApp API service, along with explanations and troubleshooting tips.

Error Response Format

When an error occurs, the API returns a JSON response with the following structure:

{
  "success": false,
  "error": {
    "code": "error_code_here",
    "message": "Human-readable error message",
    "details": {  // Optional additional information
      "field": "specific_field_with_error",
      "reason": "specific_reason_for_error"
    }
  }
}

HTTP Status Codes

The API uses standard HTTP status codes to indicate the success or failure of requests:

Status Code Description
200 OK The request was successful
201 Created A resource was successfully created
400 Bad Request The request was invalid or cannot be served
401 Unauthorized Authentication failed or user doesn't have permissions
403 Forbidden The request is understood but it has been refused or access is not allowed
404 Not Found The requested resource could not be found
409 Conflict The request conflicts with the current state of the server
422 Unprocessable Entity The request was well-formed but was unable to be followed due to semantic errors
429 Too Many Requests Rate limit exceeded
500 Internal Server Error An error occurred on the server
503 Service Unavailable The server is temporarily unavailable

Authentication Errors

Error Code HTTP Status Description Troubleshooting
invalid_api_key 401 The API key provided is invalid Check that you're using the correct API key and that it hasn't been revoked
missing_api_key 401 No API key was provided in the request Include your API key in the Authorization header
expired_api_key 401 The API key has expired Generate a new API key from your dashboard
insufficient_permissions 403 The API key doesn't have permission to perform this action Use an API key with the required permissions or contact support to upgrade your plan

Session Errors

Error Code HTTP Status Description Troubleshooting
session_not_found 404 The specified session ID doesn't exist Check that you're using the correct session ID
session_not_connected 400 The session exists but is not connected to WhatsApp Ensure the QR code has been scanned and the session is active
session_limit_reached 403 You've reached the maximum number of sessions allowed for your plan Delete unused sessions or upgrade your plan
qr_code_expired 400 The QR code for session initialization has expired Create a new session to get a fresh QR code
session_disconnected 400 The WhatsApp session has been disconnected Reconnect by creating a new session and scanning the QR code

Message Errors

Error Code HTTP Status Description Troubleshooting
invalid_phone_number 400 The recipient phone number is invalid Ensure the phone number is in international format (e.g., 14155552671)
recipient_not_on_whatsapp 400 The recipient doesn't have WhatsApp installed Verify that the recipient has an active WhatsApp account
message_too_long 400 The message exceeds the maximum allowed length Reduce the message length (max 4096 characters for text messages)
media_size_exceeded 400 The media file exceeds the maximum allowed size Reduce the file size (limits: Images 5MB, Videos 16MB, Documents 100MB)
invalid_media_type 400 The media type is not supported Use a supported media type (image, video, document, audio)
media_url_inaccessible 400 The media URL provided cannot be accessed Ensure the URL is publicly accessible and the file exists
message_not_found 404 The specified message ID doesn't exist Check that you're using the correct message ID
message_sending_failed 500 The message could not be sent due to a WhatsApp error Check the session status and try again later
daily_message_limit_reached 403 You've reached the maximum number of messages allowed for your plan today Wait until the limit resets or upgrade your plan

Webhook Errors

Error Code HTTP Status Description Troubleshooting
invalid_webhook_url 400 The webhook URL provided is invalid Ensure the URL is properly formatted and uses HTTPS (for production)
webhook_url_unreachable 400 The webhook URL cannot be reached Verify that your webhook server is running and accessible from the internet
webhook_response_timeout 400 The webhook URL took too long to respond Ensure your webhook server responds within 5 seconds
invalid_webhook_event 400 One or more of the specified webhook events are invalid Use only supported event types (message.received, message.status, etc.)

Rate Limiting Errors

Error Code HTTP Status Description Troubleshooting
rate_limit_exceeded 429 You've exceeded the rate limit for this endpoint Wait for the rate limit to reset (see Retry-After header) or upgrade your plan
concurrent_request_limit_exceeded 429 Too many concurrent requests are being made Reduce the number of simultaneous requests to the API

General Errors

Error Code HTTP Status Description Troubleshooting
invalid_request 400 The request is malformed or contains invalid parameters Check the request format and parameters against the API documentation
missing_required_parameter 400 A required parameter is missing from the request Check the API documentation for required parameters
invalid_parameter_type 400 A parameter has the wrong data type Check the API documentation for the correct parameter types
resource_not_found 404 The requested resource could not be found Verify that the resource ID or path is correct
method_not_allowed 405 The HTTP method used is not supported for this endpoint Use the correct HTTP method (GET, POST, etc.) as specified in the documentation
internal_server_error 500 An unexpected error occurred on the server Contact support if the issue persists
service_unavailable 503 The service is temporarily unavailable Try again later

Error Handling Best Practices

To effectively handle errors in your application, we recommend the following practices:

  • Check error codes: Always check the error code in the response to understand what went wrong.
  • Implement retry logic: For transient errors (like rate limiting or temporary service unavailability), implement retry logic with exponential backoff.
  • Log errors: Log errors with their full details for debugging purposes.
  • Display user-friendly messages: Translate API error messages into user-friendly messages in your application.
  • Monitor error rates: Keep track of error rates to identify potential issues with your integration.

Example Error Handling

async function sendWhatsAppMessage(sessionId, to, text) {
  try {
    const response = await fetch('https://api.messaging.com/v1/messages/text', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${API_KEY}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ sessionId, to, text })
    });
    
    const data = await response.json();
    
    if (!data.success) {
      // Handle specific error codes
      switch (data.error.code) {
        case 'session_not_connected':
          // Prompt user to reconnect
          break;
          
        case 'invalid_phone_number':
          // Show validation error to user
          break;
          
        case 'rate_limit_exceeded':
          const retryAfter = response.headers.get('Retry-After') || 60;
          // Implement retry logic
          setTimeout(() => sendWhatsAppMessage(sessionId, to, text), retryAfter * 1000);
          break;
          
        default:
          // Show generic error to user
      }
      
      return { success: false, error: data.error };
    }
    
    return data;
  } catch (error) {
    return { 
      success: false, 
      error: { 
        code: 'network_error', 
        message: 'Failed to connect to the API' 
      } 
    };
  }
}