Rate Limiting is only available with CoCart Plus.
Rate limiting is opt-in and is intended for advanced merchants and platforms.
What it does?
Rate limiting is to prevent abuse on endpoints from excessive calls and performance degradation on the machine running the store. Limiting is based on user ID for registered users (logged in) and IP address for guest users (unauthenticated requests). It also offers standard support for running behind a proxy, load balancer, etc. This is also optional and is disabled by default. The rate limiting uses a modifiedwc_rate_limit table with an additional remaining column for tracking the request count in any given request window.
Limit information
A default maximum of 25 requests can be made within a 10-second time frame. These can be changed throughcocart_api_rate_limit_options filter.
Methods restricted by Rate Limiting
GET, POST, PUT, PATCH, and DELETE
Enable Rate Limiting
Developers can enable rate limiting in two ways.Constants
Set the following constants in yourwp-config.php.
Using the cocart_api_rate_limit_options filter.
Filtering provides more defined control and takes precedence over any constants set in your
wp-config.php file.Supporting Proxies
Like any mechanism that restricts usage to counter potential abuse of an API, this is a sensitive feature that should be used carefully. In a scenario where a store is behind another service layer (a proxy, load balancer, etc.), the developer should enable standard proxy support through the Otherwise rate limiting might be wrongly triggered and group-limit requests.Proxy support
For theproxy_support option to work properly, service layers (load balancer, cache service, CDNs, etc.) must be passing the originating IP supported through standard IP forwarding headers, namely:
X_REAL_IP|CLIENT_IPCustom popular implementations that simplify obtaining the origin IP for the requestX_FORWARDED_FORDe-facto standard header for identifying the originating IP, DocumentationX_FORWARDEDDocumentation, RFC 7239
This is disabled by default and may not be needed if you decide to provide your own fingerprint ID.
Limit usage information observability
Current limit information can be observed via custom response headers:RateLimit-LimitMaximum requests per time frame.RateLimit-RemainingRequests available during current time frame.RateLimit-Retry-AfterSeconds until requests are unblocked again. Only shown when the limit is reached.RateLimit-ResetUnix timestamp of next time frame reset.
Response headers example
Tracking Abuses
Developers can use thecocart_api_rate_limit_exceeded action to track and handle instances of API abuse:
Fingerprint ID
Fingerprint ID is supported since v2.0.0
cocart_api_rate_limit_id.
Testing Guide
Without proxy support
- Enable Rate Limiting by using the options filter.
- In a short window, keep making API requests.
- Check that RateLimit-xxx headers change on each request.
- Check that once you’ve hit the limit, an error response is returned. You can modify the limits using the options filter to make it easier to test.
With proxy support, do the same as before and
- Enable proxy support
- Make your requests with one of the following request headers containing always the same IP address:
- X-Real-IP or Client-IP
- X-Forwarded-For
- Forwarded
User Facing Testing
- Enable Rate Limiting by using the options filter.
- Try to apply a coupon or access
/wp-json/cocart/v2/couponbeyond current limits (currently 25 requests under 10 seconds) - Ensure you get presented with an error “Too many requests. Please wait xx seconds before trying again.”