Errors#

CasaPay uses conventional HTTP response codes to indicate the success or failure of an API request. In general: codes in the 2xx range indicate success, codes in the 4xx range indicate an error that failed given the information provided (e.g., a required parameter was omitted), and codes in the 5xx range indicate an error with CasaPay's servers.

HTTP Status Codes#

CodeDescription
200 - OKEverything worked as expected
201 - CreatedA new resource was successfully created
400 - Bad RequestThe request was unacceptable, often due to missing a required parameter
401 - UnauthorizedNo valid API key provided
403 - ForbiddenThe API key doesn't have permissions to perform the request
404 - Not FoundThe requested resource doesn't exist
409 - ConflictThe request conflicts with another request (idempotent replay)
422 - Unprocessable EntityThe request was well-formed but contained semantic errors
429 - Too Many RequestsToo many requests hit the API too quickly
500 - Server ErrorSomething went wrong on CasaPay's end

Error Response Format#

All errors return a JSON body with the following structure:

{
  "error": {
    "type": "invalid_request_error",
    "code": "parameter_missing",
    "message": "The 'amount' parameter is required.",
    "param": "amount",
    "doc_url": "https://docs.casapay.com/docs/getting-started/errors#parameter_missing"
  }
}

Error Types#

TypeDescription
api_errorAn error occurred internally (these are rare)
authentication_errorFailure to properly authenticate
invalid_request_errorYour request has invalid parameters
rate_limit_errorToo many requests
validation_errorErrors triggered by field validation
idempotency_errorThe idempotency key was used for a different request

Error Codes#

CodeDescription
parameter_missingA required parameter was not provided
parameter_invalidA parameter value is invalid
resource_not_foundThe specified resource does not exist
resource_already_existsA resource with this identifier already exists
state_invalidThe resource is not in a valid state for this operation
amount_too_smallThe amount is below the minimum allowed
amount_too_largeThe amount exceeds the maximum allowed
currency_unsupportedThe currency is not supported
rate_limit_exceededAPI rate limit exceeded

Handling Errors#

We recommend writing code that gracefully handles all possible API exceptions:

try {
  const paymentIntent = await casapay.paymentIntents.create({
    amount: 120000,
    currency: 'eur',
  });
} catch (error) {
  switch (error.type) {
    case 'invalid_request_error':
      // Handle invalid parameters
      console.log(`Invalid parameter: ${error.param}`);
      break;
    case 'authentication_error':
      // Handle authentication failure
      break;
    case 'rate_limit_error':
      // Wait and retry
      break;
    default:
      // Handle unexpected errors
      break;
  }
}