March 08, 2026

Bulletproof Laravel APIs: Sanctum, Rate Limiting, and CORS Best Practices

By Paresh Prajapati • Lead Architect

Bulletproof Laravel APIs: Sanctum, Rate Limiting, and CORS Best Practices

Making it Work is Not Enough

When developing an API, the initial focus is always on functionality: routing requests, querying the database, and returning the correct JSON payload. Once the Flutter frontend successfully renders the data, it is tempting to call the job done. But in the world of full-stack development, making it work is only 50% of the job. Making it secure is the other half.

An unsecured API is a massive liability. Malicious actors, automated bots, and even accidental front-end bugs can overwhelm your servers or expose sensitive user data. Today, we are securing our Laravel API endpoints using three critical layers: Authentication, Rate Limiting, and CORS management.

Layer 1: Token-Based Authentication with Sanctum

If you are building a mobile application or a Single Page Application (SPA), traditional session-based authentication (cookies) often creates more problems than it solves. Laravel Sanctum provides a featherweight authentication system specifically designed for these use cases.

When a user logs in via your Flutter app, Sanctum generates a secure API token. The mobile device stores this token securely and attaches it to the Authorization: Bearer header of every subsequent request.


// Protecting a route in routes/api.php
Route::middleware('auth:sanctum')->group(function () {
    Route::get('/user/dashboard', [DashboardController::class, 'index']);
    Route::post('/transactions/create', [TransactionController::class, 'store']);
});

Any request lacking a valid, unexpired token is instantly rejected with a 401 Unauthorized response before it ever reaches your controller logic.

Layer 2: Stopping Abuse with Rate Limiting

Even authenticated users can cause problems. If a user maliciously (or accidentally, due to an infinite loop in the frontend code) fires 500 requests per second at your server, it will quickly exhaust your database connections and crash the application for everyone else.

Laravel's ThrottleRequests middleware is your first line of defense against DDOS attacks and API abuse. You define rate limiters in the RouteServiceProvider or directly in your routing files.


// Defining a strict rate limit for a specific route
Route::middleware(['auth:sanctum', 'throttle:10,1'])->group(function () {
    // This allows a maximum of 10 requests per 1 minute per IP/User
    Route::post('/generate-ai-report', [ReportController::class, 'generate']);
});

When the limit is breached, Laravel automatically returns a 429 Too Many Requests response, protecting your server resources.

Layer 3: Taming the CORS Beast

If you have ever built a frontend application, you have almost certainly encountered a CORS (Cross-Origin Resource Sharing) error in your browser console. CORS is a browser security feature that prevents a website on one domain from making API requests to a different domain without explicit permission.

To ensure your web portal can talk to your Laravel API (while blocking unauthorized domains), you must configure the config/cors.php file accurately.


// config/cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => [
    'https://smarttechdevs.com', 
    'https://admin.smarttechdevs.com',
    // Never use '*' in production if you have authenticated routes!
],
'allowed_headers' => ['*'],
'supports_credentials' => true,

Conclusion

Security should never be an afterthought. By locking down your routes with Sanctum, preventing resource exhaustion with rate limiting, and strictly defining your CORS policies, you transform your Laravel application from a fragile data pipeline into a robust, enterprise-grade backend ready for real-world traffic.

Paresh Prajapati
Lead Architect, Smart Tech Devs