API Version
Product Catalog
Library

Error handling and rate limits

Chargebee API provides error handling capabilities by using standard HTTP error codes and additional helpful parameters in its error responses. This page provides recommendations on effective error handling and a reference for all custom and standard error codes used by the API.

HTTP status codes are used to indicate success or failure of an API call. The body of the response contains the details of the error in JSON format. The client library wraps the JSON response in an instance of APIException and throws the exception.

Note: Please use latest client library ( greater than 1.1.48 version) to make use of the upgraded error response. It enables simpler error handling. See the change notes for more details.

Sample error response 1:

Given below is a sample error that is returned for the Create a subscription API call when the plan_id is invalid. The returned HTTP status code is 404.

{ "message":"Plan is not present", "type":"invalid_request", "api_error_code":"resource_not_found", "param":"item_id", "error_cause_id": "gateway.general.decline" }

Sample error response 2:

Given below is a sample error response for the Create a subscription API call when a transaction fails at the gateway. The HTTP status code returned is 400.

{ "message": "Subscription cannot be created as we are not able to collect EUR 10,00 from your card on file. Error message: (card_declined) Your card was declined.", "type": "payment", "api_error_code": "payment_processing_failed", "error_cause_id": "gateway.general.decline" }

HTTP Status Codes

The standard http status codes used are

  • 2XX The response code in this range implies the API call has succeeded.
  • 3XX This range is not used currently.
  • 4XX response codes indicate failure due to wrong input (i.e, client) side. As the usable list of http codes in the 4xx range is limited we predominantly use the 400 ( BAD REQUEST ) error code for client-side errors.
  • 5XX response codes indicate API request was valid but failed due to issues on the Chargebee server.

Error Attributes

The following attributes are returned as part of the error content. The java library exposes these as public variables in APIException
message
A descriptive information about the error. This is for developer(/merchant) consumption and should not be used for showing errors to your customers.
type
An optional attribute which groups errors based on the error handling routine that is required. The types are payment, invalid_request and operation_failed. The client libraries throw a corresponding exception for easy error handling.
api_error_code
A more specific code allowing you to handle the specific errors.
param
An optional attribute which is provided if the error is due to a specific parameter. The parameter name is the same as that seen in this documentation when the language chosen is cURL.
Note: For the same API, customer_id is a URL path parameter. If that value is invalid, then an error of type = resource_not_found is thrown without the param attribute.
error_cause_id
A Chargebee-defined optional code that corresponds to the specific error encountered during the request. This code helps in identifying and standardizing the error across different gateway services for consistent error handling.

Note: There may be additional deprecated attributes (like error_code) which are present to support older versions of the libraries.

Exception Classes

The client library throws specific exceptions based on the "type" attribute. You could have common error handling routines based on the exception type. For more specific error handling use the "api_error_code" attribute in the exception.

APIException
This is the root exception class for all the api related errors. It contains all the attributes of the error. Error such as authentication failure are thrown as this exception.
Extends: RuntimeException
PaymentException
All payment related errors including payment method detail validation (such as card number validation) and verification.
Extends: APIException
InvalidRequestException
All errors due to invalid request.
Extends: APIException
OperationFailedException
Thrown if the request parameters were right but the requested operation could not be completed. The reasons might be the api request limit was exceeded or could be due to an issue in ChargeBee side. These should occur very rarely and mostly be of temporary nature.
Extends: APIException

Error handling recommendations

API calls fail at the first error encountered by Chargebee while validating them. To enhance user experience, we recommend that you validate an API call thoroughly to avoid multiple failed attempts from your users, each time for a different error.

There are cases where you have to depend on Chargebee's validation for handling errors like coupon code validation or VAT validations. In such cases, check the specific param and api_error_code combination to show a proper error message to your users.

Handling API Throttling

If you receive an HTTP 429 Too Many Requests error response to an API call, it means that the API requests for your Chargebee site have exceeded acceptable limits. Chargebee may also send a Retry-After header indicating the time duration to wait before sending a new request. Follow these best practices:

  • If you see the Retry-After header, wait for the number of seconds indicated before retrying.
  • Use an exponential backoff algorithm when making further requests.
  • To prevent all your request threads/processes from retrying simultaneously (thundering herd problem), add some randomized jitter to the backoff algorithm to spread the requests out.

See Also: API Rate Limits.

Below is a sample pseudo code for handling errors in 'create subscription' api call. Let's assume that you are getting the card details along with coupon code from the user.

try{
  // The code calling ChargeBee_Subscription::create
  // ...
}catch(PaymentException ex){
  // First check for card parameters entered by the user.
  //We recommend you to validate the input at the client side itself to catch simple mistakes.
  if("card[number]".equals(ex.param)){
      // Ask your user to recheck the card number. A better way is to use 
      // Stripe's https://github.com/stripe/jquery.payment for validating it in the client side itself.   
  
  //}else if(<other card params>.equals(ex.param)){ 
      //Similarly check for card parameters entered by the user.
      //....

  }else{
    // Verfication or processing failures.
    // Provide a standard message to your user to recheck his card details or provide a different card.
    // Like  'Sorry,there was a problem when processing your card, please check the details and try again'. 
  }
}catch(InvalidRequestException ex){  
  // For coupons you could decide to provide specific messages by using 
  // the 'api_error_code' attribute in the error.
  if("coupon".equals(ex.param)){ 
      if("resource_not_found".equals(ex.apiErrorCode)){
          // Inform user to recheck his coupon code.
      }else if("resource_limit_exhausted".equals(ex.apiErrorCode)){
          // Inform user that the coupon code has expired.
      }else if("invalid_request".equals(ex.apiErrorCode)){
          // Inform user that the coupon code is not applicable for his plan(/addons).
      }else{
          // Inform user to recheck his coupon code.
      }
  }else{
      // Since you would have validated all other parameters on your side itself, 
      // this could probably be a bug in your code. Provide a generic message to your users.
  }
}catch(OperationFailedException ex){
  // Indicates that the request parameters were right but the request couldn't be completed.
  // The reasons might be "api_request_limit_exceeded" or could be due to an issue in ChargeBee side.
  // These should occur very rarely and mostly be of temporary nature. 
  // You could ask your user to retry after some time.
}catch(APIException ex){ 
  // Handle the other ChargeBee API errors. Mostly would be setup related 
  // exceptions such as authentication failure.
  // You could ask users contact your support.
}catch(IOException ex){
  // Handle IO exceptions such as connection timeout, request timeout etc.
  // You could give a generic message to the customer retry after some time.
}catch(Exception ex){
  // These are unhandled exceptions (Could be due to a bug in your code or very rarely in client library).
  // You could ask users contact your support.
}

The following are the list of codes grouped based on type.

Note: 'New' codes might get added (though additions are very rare). If you are handling for specific codes also ensure that there is a default block for handling unknown(/new) error codes.

These errors may occur when trying to store the payment method details(such as card, paypal, ACH etc) or when trying to collect a payment. These errors should be handled and appropriate message should be shown to the user. These errors are thrown as PaymentException.

Code
Description
payment_processing_failed
Returned when the payment collection fails.
HTTP Code: 400, Sample message: Subscription cannot be created as the payment collection failed. Gateway Error: Card declined.
payment_method_verification_failed
Returned when validation or verification fails for the provided payment method. For example if the payment method is card, this will include all card parameter validation errors and also verification failures from the gateway.
HTTP Code: 400, Sample message: Problem while adding the card. Error message : AVS check failed.
payment_method_not_present
Returned when the request requires payment collection but the 'payment method' details (such as card) is not present for the customer. This error will not occur if auto-collection is disabled for the customer.
HTTP Code: 400, Sample message: Card is not present when reactivating the subscription
payment_gateway_currency_incompatible
Returned when the payment gateway configured is incompatible with the transactional currency. This error will not occur if auto-collection is disabled for the customer.
HTTP Code: 400, Sample message: The configured gateway account [ PAYPAL_EXPRESS_CHECKOUT ] does not support transactions in INR.
payment_intent_invalid
Returned when validation or verification fails for provided payment intent.For example if payment intent which is passed is in not consumable state.
HTTP Code: 400, Sample message: Problem while processing payment intent. Probable Reason: Invalid state
payment_intent_invalid_amount
Returned when processing amount is different from payment intent amountFor example if payment intent which is passed has authorized 10$ and if the charges initiated is for 12$.
HTTP Code: 400, Sample message: Problem while processing payment intent . Probable Reason: amount mismatch

The following errors are caused due to invalid request. In most cases the errors should occur only during the development phase. These errors are thrown as InvalidRequestException.

Code
Description
resource_not_found
Returned when any of resource(s) referred in the request is not found.
HTTP Code: 404, Sample message: 82sa2Sqa5 not found
resource_limit_exhausted
Returned when any limit constraint is violated by the request. For example this error is thrown when the coupon provided has already expired or its maximum redemption count has been reached.
HTTP Code: 400, Sample message: Coupon has expired
param_wrong_value
Returned when the value does not meet the required specification for the parameter. For example, wrong email format. It is strongly recommended to do the validation at your end before calling Chargebee's API (other than specific cases like VAT number validation).
HTTP Code: 400, Sample message: invalid email format
duplicate_entry
Returned when the request provides a duplicate value for an attribute that is specified as unique for that site. For example in 'create subscription api' if you are passing the subscription id then this error will be thrown if a subscription exists in site with the same id.
HTTP Code: 400, Sample message: The value Hy5r4129u is already present.
db_connection_failure
Returned when db connection fails.
HTTP Code: 503, Sample message: Sorry, unable to complete the operation due to db communication failure. Please try after some time
invalid_state_for_request
Returned when the requested operation is not allowed for current state of the resource. This error will occur if the state of the resource has not been checked for the validity of the request. For example this error is returned when we try to schedule subscription changes at 'end of term' for canceled subscriptions.
HTTP Code: 409, Sample message: Only pending invoices can be closed.
http_method_not_supported
Returned when the 'http method', specified in the request, is not allowed for this URL. It should not occur if you are using one of the standard client library.
HTTP Code: 405, Sample message: Sorry,the http method GET is not supported for this URL.
invalid_request
Returned when the request has incompatible values or does not match the API specification. As it is a generic error, handling this error is recommended only in combination with param attribute.
HTTP Code: 400, Sample message: The plan's billing period and addon's billing period are incompatible.
resource_limit_exceeded

HTTP Code: 400, Sample message:
unable_to_process_request
Returned when the HTTP request body contains a well-formed, but semantically erroneous payload. For example this error is returned when a client attempts to reuse an idempotency key with a different request payload.
HTTP Code: 422, Sample message: Unable to process the request due to semantic errors.

These errors are returned when the request was valid but the requested operation could not be completed. These errors should occur very rarely. You generally need not handle them specifically other than showing a generic error message to your users. These errors are thrown as OperationFailedException.

Code
Description
lock_timeout
Returned when there are multiple concurrent requests to the same resource.
HTTP Code: 429, Sample message: The request cannot be processed right now as another API request or process is currently accessing the same resource. Please retry.
internal_error
Returned when the request parameters were right but the operation couldn't be completed due to a bug in Chargebee side.
HTTP Code: 500, Sample message: Sorry,Something went wrong when trying to process the request.
internal_temporary_error
Returned when temporary occured in Chargebee side. The request can be re-tried, with exponential backoff in case of repeat failures.
HTTP Code: 503, Sample message: Sorry,A temporary error occured when trying to process the request.
request_blocked
Returned when request is blocked for your site. The blocking could be only for a specific set of operation(s) . The reason would be provided as part of the message. You would have to contact support for additional details.
HTTP Code: 403, Sample message: Sorry, The request is blocked
api_request_limit_exceeded
Returned when requests have been blocked temporarily due to request count exceeding acceptable limits.
HTTP Code: 429, Sample message: Sorry, access has been blocked temporarily due to request count exceeding acceptable limits. Please try after some time.
third_party_api_request_limit_exceeded
Returned when your request is blocked temporarily at a third-party service, due to the request count exceeding their acceptable limits.
HTTP Code: 429, Sample message: Sorry, access has been blocked temporarily at a third-party service, due to the request count exceeding their acceptable limits. Please try after some time.
site_not_ready
Returned when your site is temporarily unavailable due to a scheduled maintenance.
HTTP Code: 503, Sample message: Site is not ready. Please retry the request after sometime.
site_read_only_mode
Returned when your site is temporarily unavailable for write operations due to a scheduled maintenance.
HTTP Code: 503, Sample message: Site is in read only mode. Please retry the request after sometime.

Most of these errors occur due to improper API environment setup in your server or changes to the site's configuration. These errors are thrown as APIException.

Code
Description
api_authentication_failed
Returned when authentication failed for the request. The possible reasons could be the api key is invalid or authentication header is not present in the request or the header's format is invalid.
HTTP Code: 401, Sample message: Sorry, authentication failed. Invalid API Key.
basic_authentication_failed
Returned when authentication failed for the request. The possible reasons could be that one or both of the username and password are invalid
HTTP Code: 401, Sample message:
api_authorization_failed
Returned when the API key does not have sufficient privileges to perform the particular operation.
HTTP Code: 403, Sample message: Sorry, authorization failed. The key does not have the required permissions
site_not_found
Returned when the site is not found.
HTTP Code: 404, Sample message: Sorry, we couldn't find the site acme
configuration_incompatible
Returned when the request is not compatible with the configuration for the site or the configuration is incomplete.
HTTP Code: 400, Sample message: Return URL for the hosted page not configured.

Exception Classes

The client library throws specific exceptions based on the "type" attribute. You could have common error handling routines based on the exception type. For more specific error handling use the "api_error_code" attribute in the exception.

APIException
This is the root exception class for all the api related errors. It contains all the attributes of the error. Error such as authentication failure are thrown as this exception.
Extends: RuntimeException
PaymentException
All payment related errors including payment method detail validation (such as card number validation) and verification.
Extends: APIException
InvalidRequestException
All errors due to invalid request.
Extends: APIException
OperationFailedException
Thrown if the request parameters were right but the requested operation could not be completed. The reasons might be the api request limit was exceeded or could be due to an issue in ChargeBee side. These should occur very rarely and mostly be of temporary nature.
Extends: APIException

Chargebee has robust and flexible API limits to serve a wide variety of business needs, when this limit is exceeded, you will see error messages with an HTTP status code of 429. The default limits are captured below.

The default limits for these for the live sites are defined as follows:

Plan Default limit (per minute)
Starter 150
Performance 1000
Enterprise (default) 3500
Enterprise (custom)** No upper limit

For test sites, it is 150 API calls per minute.


Note:
  • While these API limits are the default settings, you can easily request higher limits by contacting Chargebee Support with your requirements.
  • ** For customers with requirements that exceed the Enterprise (default) plan's API limits, our solution experts work closely with you to ensure that API limits are not only well-provisioned but also fully capable of scaling to meet your most demanding traffic and burst needs. Chargebee is built to support your growth, and we are committed to providing the flexibility and capacity your business requires.

Concurrency Limits

In addition to the rate limit on the number of requests per minute, there is also a concurrency limit, which restricts the number of API requests that can be processed simultaneously (similar to the maximum number of lanes on a road). The concurrency limits are set at 50 for GET requests and 100 for POST requests. The lower limit for GET requests is due to their typically faster completion time, often under 50 milliseconds. These limits have been found to be sufficient for most customers. However, if your business requires higher limits, please contact Chargebee Support to determine the best configuration for your needs.

Concurrent updates to a same resource

In Chargebee, simultaneous API update calls to the same resource (such as a subscription) are serialised (i.e, requests are processed one after the other, in sequence). Too many concurrent update calls can lead to a lock_timeout error and may inadvertently cause you to exceed the concurrent API request limit. To prevent this, ensure that your code avoids making multiple simultaneous updates to the same resource.