Rate Limiting Rest API in Statamic CMS

Updated: Feb 06, 2025

Rate Limiting Rest API in Statamic CMS

Statamic CMS is a powerful and flexible content management system that offers a REST API for interacting with your site's content programmatically. However, when building applications that consume this API, it's essential to consider rate limiting to prevent abuse and ensure fair usage. In this answer, we'll discuss how to implement rate limiting for Statamic's REST API.

Statamic does not have built-in rate limiting for its REST API. Instead, you can implement rate limiting using various methods, such as:

  1. Middleware: You can write custom middleware in PHP to limit the number of requests per IP address or user. Statamic's middleware system is flexible and can be used to implement rate limiting. You can use libraries like Laravel's rate limiting middleware or build your custom middleware using Statamic's middleware API.
  2. CDN: If you're using a CDN like Cloudflare, you can enable rate limiting at the CDN level. Cloudflare offers various rate limiting options, including IP blocking, challenge pages, and API keys.
  3. Firewall: You can use a firewall like ModSecurity or Nginx to implement rate limiting. Firewalls can limit the number of requests per IP address, user agent, or other criteria.
  4. API Key: You can implement rate limiting using API keys. Statamic supports API keys out of the box, and you can limit the number of requests per API key to prevent abuse.

To implement rate limiting using API keys, follow these steps:

  1. Create API keys: In the Statamic control panel, go to Settings > API Keys and create a new API key for your application.
  2. Rate limiting: Implement rate limiting in your application code. You can use various libraries or build your custom rate limiter. For example, you can use Laravel's rate limiting library to limit the number of requests per API key.
  3. Return error messages: If your application exceeds the rate limit, return an error message to the client. Statamic's API returns a 429 error code when the rate limit is exceeded. You can customize the error message in your application code.

Here's an example of how to implement rate limiting using Laravel's rate limiting middleware:

  1. Install Laravel's rate limiting package: composer require laravel/rate-limiting
  2. Create a middleware: php artisan make:middleware RateLimit
  3. Implement the middleware: In the RateLimit middleware file, use Laravel's RateLimiter facade to limit the number of requests per API key.
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;

class RateLimit
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $throttle = RateLimiter::forUser($request->user());

        if ($throttle->tooManyRequests(60)) {
            return response()->json([
                'error' => 'Too Many Requests',
                'message' => 'Please try again later.',
            ], 429);
        }

        return $next($request);
    }
}
  1. Register the middleware: In the app/Http/Kernel.php file, add the middleware to the $routeMiddleware array and the $middlewareGroups array.
protected $routeMiddleware = [
    // ...
    'rate.limit' => \App\Http\Middleware\RateLimit::class,
];

protected $middlewareGroups = [
    // ...
    'api' => [
        // ...
        'rate.limit',
    ],
];
  1. Apply the middleware: In the routes/api.php file, apply the middleware to the API routes.
Route::get('/api/posts', 'PostController@index')->middleware('rate.limit');

With this implementation, the middleware limits the number of requests per API key to 60 requests per minute. If the rate limit is exceeded, the middleware returns a 429 error code with an error message.

In conclusion, implementing rate limiting for Statamic's REST API is essential to prevent abuse and ensure fair usage. You can use various methods to implement rate limiting, such as middleware, CDN, firewall, or API keys. In this answer, we discussed how to implement rate limiting using API keys and Laravel's rate limiting middleware.