Api Rate Limiting Out of the box in Laravel 5.2
This is a series of posts that showcase new features and aha moments in Laravel 5.2
1. Implicit Route Model Binding
2. Simplified Eloquent Global Scopes
3. Append Scheduled Tasks Output
7. API Rate Limiting
Laravel makes web development so easy that it hurts!
Laravel 5.2 development is underway and it might be released this year or in January 2016.
There are so many new features that have been developed like easy multi-auth handling, implicit route model binding and some others I’ll talk about later but the one that really got me excited is the API Rate Limiting functionality.
When building APIs, most times you’ll have to take enough time to sit down and implement a rate limiter for your API to avoid users from pulling down your servers from too many requests.
Most times it’s a headache because it involves a lot of logic to get that right.
With Laravel 5.2, even my MAMA can build an API with rate limiting enabled in 5 minutes. How awesome is that?
Let’s Get Started
1. Run composer create-project laravel/laravel api-rate-limiting dev-develop
in your terminal.
The dev-develop
branch is where we are getting the latest development version of laravel which also fetches the 5.2-dev branch of laravel/framework
repository.
2. Go to routes.php and add a route like so:
1 2 3 4 5 6 7 8 9 |
Route::get('api/v1/songs', function () { return [ [ 'name' => 'Api rate Limiting', 'genre' => 'rock', 'producer' => 'don-jazzy' ] ]; }); |
Now this is the simplest API you will ever write in your lifetime.
By default, Laravel returns a JSON response.
Fire up your server and check the response.
3. Let’s hit this API route from our terminal. You can use curl
or a tool like httpie
. You can install that by doing brew install httpie
4. Run http get http://localhost:8000/api/v1/songs
on your terminal and check out the response.
You can see the response here and also see the headers.
Now, you can hit this route as many times as you want and it still displays the appropriate JSON response.
In production, except you are either google, facebook or Microsoft that possess endless server resources, you will have to give a rate limit to your APIs to ensure that users consume reasonably.
5. Add the throttle middleware like so:
1 2 3 4 5 6 7 8 9 |
Route::get('api/v1/songs', function () { return [ [ 'name' => 'Api rate Limiting', 'genre' => 'rock', 'producer' => 'don-jazzy' ] ]; })->middleware('throttle'); |
By default, Laravel provides 60 requests per minute. hit that route again from your terminal and see the result
Aha!, Look at those headers X-RateLimit-Limit
and X-RateLimit-Remaining
They signify the amount of requests assigned to a user and the amount of requests remaining for a user respectively. The more you hit the route, the more X-RateLimit-Remaining
reduces.
Now, let’s decrease our API rate limit to just 5 requests per minute like so:
1 2 3 4 5 6 7 8 9 |
Route::get('api/v1/songs', function () { return [ [ 'name' => 'Api rate Limiting', 'genre' => 'rock', 'producer' => 'don-jazzy' ] ]; })->middleware('throttle:5'); |
Hit the route again from the terminal and you’ll see that the X-RateLimit-Limit
is now 5. Now, hit that route multiple times, at least 6 times.
At the 6th time, you will discover you get a message Too Many attempts
This is because you have exceeded the no of requests granted to you. So, Laravel stops the results from displaying and returns this message. Awesome!
If you want to increase the time limit for the number of requests, you can just add that as the second argument to the throttle middleware like so:
1 2 3 4 5 6 7 8 9 |
Route::get('api/v1/songs', function () { return [ [ 'name' => 'Api rate Limiting', 'genre' => 'rock', 'producer' => 'don-jazzy' ] ]; })->middleware('throttle:5,3'); |
This means you can only perform 5 requests in 3 minutes.
Conclusion: Laravel makes use of a user’s IP address and hashes it with a unique ID and stores it in a cache. If you get into the source code, you will see how it performs this operation but like I said earlier my MAMA doesn’t need to worry about that.
As I write, my MAMA can build an API with a rate limiter in 5 minutes.
Please, if you have any questions or observations, let me know in the comments section.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016