SafePays API

Error Handling

Understanding and handling SafePays API V2 errors

Error Response Format

All error responses from the SafePays API V2 follow a consistent format:

{
  "error": "Descriptive error message"
}

HTTP Status Codes

Success Codes

CodeNameDescription
200OKRequest successful
201CreatedResource created successfully

Client Error Codes

CodeNameDescription
400Bad RequestInvalid request parameters or missing required fields
401UnauthorizedInvalid or missing API key
404Not FoundResource not found
409ConflictResource already exists (e.g., duplicate customer email)

Server Error Codes

CodeNameDescription
500Internal Server ErrorServer error — retry or contact support

Common Error Messages

Authentication Errors

Invalid API Key401

{
  "error": "Invalid API Key"
}

Cause: API key is incorrect, missing, or has been revoked. Verify your API key in the dashboard.

Validation Errors

Missing Required Fields400

{
  "error": "name and email are required"
}

Cause: Required fields not provided in the request body.

Missing Webhook400

{
  "error": "webhook parameter is required"
}

Cause: The webhook query parameter was not provided when creating an invoice.

Missing API Key (GET)400

{
  "error": "api_key parameter is required"
}

Cause: The api_key query parameter was not provided on a GET request.

Resource Errors

Customer Not Found404

{
  "error": "Customer not found"
}

Cause: The provided customer_id does not exist. Verify the ID or create the customer first.

Invoice Not Found404

{
  "error": "Invoice not found"
}

Cause: The provided invoice_id does not exist.

Duplicate Customer409

{
  "error": "Customer with this email already exists"
}

Cause: A customer with the same email address already exists in your account.

Error Handling by Endpoint

POST /api/v2/customer

StatusErrorCause
400name and email are requiredMissing name or email in request body
401Invalid API KeyInvalid or missing api_key
409Customer with this email already existsDuplicate email address

POST /api/v2/invoice

StatusErrorCause
400email or customer_id is requiredNeither customer_id nor email provided
400amount or items are requiredNeither amount nor items provided
400Invoice amount must be greater than 0Zero or negative amount
400Invalid redirect_url / Invalid webhook_urlMalformed URL
400Invalid due_date format. Use YYYY-MM-DDBad date format
401api_key is required / Invalid API KeyMissing or invalid api_key
403This vendor account has been banned.Account suspended
404Customer not foundInvalid customer_id
502Payment provider error (status)Upstream provider failure

GET /api/v2/invoice/{id}

StatusErrorCause
400api_key parameter is requiredMissing api_key query parameter
401Invalid API KeyInvalid api_key
404Invoice not foundInvalid invoice_id

GET /api/v2/customer/{id}

StatusErrorCause
400api_key parameter is requiredMissing api_key query parameter
401Invalid API KeyInvalid api_key
404Customer not foundInvalid customer_id

Best Practices

Check HTTP Status Codes

Always check the HTTP status code before parsing the response body. A 2xx status indicates success, while 4xx and 5xx indicate errors.

Handle Errors Gracefully

async function safepaysRequest(url, options) {
  const response = await fetch(url, options);
  const data = await response.json();

  if (!response.ok) {
    throw new Error(data.error || `HTTP ${response.status}`);
  }

  return data;
}

try {
  const result = await safepaysRequest(
    'https://app.safepays.com/api/v2/customer',
    {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        api_key: 'your_api_key_here',
        name: 'John Doe',
        email: 'john@example.com'
      })
    }
  );
  console.log('Success:', result);
} catch (error) {
  console.error('API Error:', error.message);
}

Retry on Server Errors

For 500 errors, implement retry logic with exponential backoff. Do not retry 4xx errors — these indicate issues with your request that need to be fixed.

async function requestWithRetry(url, options, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    const response = await fetch(url, options);

    if (response.ok) {
      return response.json();
    }

    if (response.status >= 400 && response.status < 500) {
      const error = await response.json();
      throw new Error(error.error);
    }

    const delay = Math.min(1000 * Math.pow(2, i), 10000);
    await new Promise(resolve => setTimeout(resolve, delay));
  }

  throw new Error('Request failed after retries');
}

Getting Help

If you continue experiencing issues:

  1. Double-check your request against the API documentation
  2. Verify your API key is correct and active
  3. Contact support@safepays.com with the error message and request details

On this page