Error Handling

This page provides information about error responses from the Two-Coin API and best practices for handling errors in your integration.

Error Response Format

When an API request fails, the Two-Coin API returns a JSON error response with the following structure:

{
  "message": "Invalid request: from_currency is required"
}

The error response contains:

  • message: A human-readable description of the error

HTTP Status Codes

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

Status CodeDescription
200OK - The request was successful
400Bad Request - The request was invalid
401Unauthorized - Authentication failed
403Forbidden - The request is not allowed
404Not Found - The requested resource does not exist
429Too Many Requests - Rate limit exceeded
500Internal Server Error - Something went wrong on our end

Common Error Messages

Here are some common error messages you might encounter:

Error TypeExample Message
Authentication ErrorInvalid signature or Merchant not found
Invalid RequestInvalid request: from_currency is required
Not FoundNot found: Order with code xyz not found
Bad RequestBad request: Invalid wallet address
Provider ErrorProvider error: Amount below minimum
Internal ErrorInternal server error: ...

Handling Errors

Best Practices for Error Handling

  1. Check HTTP Status Codes: Always check the HTTP status code of the response before attempting to parse the response body.
  2. Parse Error Messages: Extract the error code and message to provide meaningful feedback to your users.
  3. Implement Retries: For certain errors (e.g., rate limiting, temporary server issues), implement a retry mechanism with exponential backoff.
  4. Validate Inputs: Validate all inputs on your side before sending them to the API to catch basic errors early.
  5. Log Errors: Log all API errors for debugging and monitoring purposes.

Example Error Handling

async function createOrder(orderData) {
  try {
    const response = await fetch('https://api.dev2coin.space/api/on_ramp/orders', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-MERCHANT-CODE': merchantCode,
        'X-TIMESTAMP': Date.now().toString(),
        'X-SIGNATURE': generateSignature(...)
      },
      body: JSON.stringify(orderData)
    });

    if (!response.ok) {
      const errorData = await response.json();

      // Handle specific error cases based on status code
      if (response.status === 400) {
        // Invalid request - check the message for details
        console.error('Invalid request:', errorData.message);
      } else if (response.status === 401) {
        // Authentication failed - check signature generation
        console.error('Auth failed:', errorData.message);
      }

      throw new Error(`API Error: ${errorData.message}`);
    }

    return await response.json();
  } catch (error) {
    console.error('Order creation failed:', error);
    throw error;
  }
}

Webhook Error Handling

For webhook notifications, implement proper error handling to ensure you don't miss important updates:

  1. Respond with 200 OK to acknowledge receipt of the webhook, even if you encounter errors in processing it
  2. Log the webhook payload for debugging
  3. Process webhooks asynchronously to avoid timeout issues
  4. Implement idempotency to handle duplicate webhook notifications

Support

If you encounter any issues or have questions not addressed in this documentation, please contact our support team on Telegram at https://t.me/cs_2coin (@cs_2coin).