[Fixed] 429: Too Many Attempts When Call API In Laravel 8 9

  • 08-08-2022
  • 5143
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

429: Too Many Attempts, Disable rate limiter in Laravel, Laravel Rate Limiter Not Working; When you are working with API that's time the issue comes many times in development stag or high lever traffic side through the API HTTP request attempt that time you are facing issue like this.

There are many solutions to 429: Too Many Attempts request in your laravel application, If you don't want to limit set HTTP API request you can just remove it. If you want to stop high loading and server down issue your site you, can just set up your attempt limit in your application.

When you receive a response from API { status: 429, responseText: 'Too Many Attempts.' } like this that means your attempt limit is over you you just need to wait until over the limit time. However you can clear once a time using php artisan cache:clear command but that bad practices.

In laravel version 9, 429: too many requests separate from the lower version of the laravel, The concept is still the same but the definition with using a call with the latest version.

Specific Route

If you want to increase attempt request for a specific route you can use like. Use throttle middleware in your Route.

Route::get('myapi/{value}/{anothervalue}', 'MyApiController@getStuff')->middleware('throttle:100,1');

Laravel 8 OR Lower Version

Laravel 9 or lower version the attempt method defined in kernal.php file. You will find in app/Http/Kernel.php

The default apply to the API HTTP request attempt. Means this HTTP request count just for the API routes as default in laravel.

The throttle accepts two separate comma arguments. The first is for the API attempt request limit and the second is after the comma limit that request for the specific time period only.

throttle:{How Many Your Request Attempt},{In Minute}

How To Fix ? Increase your HTTP request attempt limit request more. If you do not like to fix your HTTP request you can just commit the code so you can make an unlimited HTTP request to the application.

app/Http/Kernel.php
'api' => [
    'throttle:1000000000000,1',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

Laravel 9

In laravel 9 little bit different from older version of laravel, The attempt request is now defined in RouteServiceProvider.php from kernel.php

In this version, you will find the app/providers/RouteServiceProvider.php file. It is now handled by the method. The method name is configured rate limiting() method.

Limit::perMinute(60) Define limit per minute, If perMinute(120), You can attempt upto 120 request in a single minute.

The request attempt count based on the authentication user id, or based on your IP address. You can still modify it to a token-based attempt. You just need to put auth token in by() method. If the token match, it will count the same minute till the given minute.

The attempt HTTP rate limit is applied for the API only you can still modify it to the web. By default, it's coming with API routes.

How To Fix ? Increase your HTTP request attempt limit request more. If you do not like to fix your HTTP request you can just commit the code so you can make an unlimited HTTP request to the application.

App/Providers/RouteServiceProvider.php
protected function configureRateLimiting()
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
    });
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :