Download the PHP package osid/laravel-api-rate-limiter without Composer
On this page you can find all versions of the php package osid/laravel-api-rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download osid/laravel-api-rate-limiter
More information about osid/laravel-api-rate-limiter
Files in osid/laravel-api-rate-limiter
Package laravel-api-rate-limiter
Short Description A robust API rate limiter and throttling package for Laravel.
License MIT
Informations about the package laravel-api-rate-limiter
Laravel API Rate Limiter
A robust API rate limiter and throttling package for Laravel, designed to manage and control the flow of requests to your APIs, protecting against abuse and ensuring fair usage.
Installation
-
Require the Package:
Run the following command to install the package via Composer:
-
Publish Configuration:
Publish the configuration file using the artisan command:
This will create a configuration file at
config/api-rate-limiter.php
.
Configuration
The api-rate-limiter.php
configuration file allows you to define rate limits and burst settings for your API routes. Here’s an example configuration:
-
Global Settings:
limit
: The maximum number of requests allowed per hour globally.burst
: Additional requests allowed in short bursts.
-
Route-Specific Settings:
- Define limits and burst settings for specific routes.
-
User-Based and IP-Based Limits:
user_based
: Enable rate limits based on authenticated users.ip_based
: Enable rate limits based on client IP addresses.
-
Cache Driver:
cache_driver
: Specify the cache driver to use (e.g.,redis
,memcached
).
- Response Messages:
- Customize the response message when the rate limit is exceeded.
Applying Middleware
To apply the rate limiting middleware to your API routes, add it to the routes in your routes/api.php
file.
Example Usage
-
Global Rate Limiting:
To apply global rate limits, ensure the
global
configuration is set inconfig/api-rate-limiter.php
. This will apply to all routes not specifically configured. -
Route-Specific Rate Limiting:
To apply rate limits to specific routes, define them under the
routes
key inconfig/api-rate-limiter.php
. -
Custom Response Messages:
Customize the message returned when the rate limit is exceeded by modifying the
response_messages
key in the configuration.
Testing
Use tools like Postman or cURL to test the rate limiting functionality. Make several requests to your API endpoints to ensure the rate limits and burst settings are working as expected.
Conclusion
This package provides a flexible and powerful way to manage API request rates in Laravel applications, ensuring fair usage and protecting against abuse. For any issues or contributions, please refer to the package repository.