Hasina Razafintsalama

Hasina RAZAFINTSALAMA

← Back to Blog
Backend

Designing a Robust and Scalable REST API with Laravel

Resource controllers, API resources, authentication, versioning, rate limiting,the building blocks of a production-grade Laravel API.

2026-05-28·7 min

Laravel is one of the best frameworks for building REST APIs quickly. But "quick" does not mean "production-ready" by default. A few structural decisions made early will determine whether your API scales gracefully or becomes a maintenance burden.

Structure: API Resources over raw arrays

Never return Eloquent models directly from your controllers. Use API Resources to control exactly what your API exposes,and decouple your database schema from your API contract.

php
// Bad: exposes everything, including internal fields
return response()->json($user);

// Good: controlled, versioned API contract
class UserResource extends JsonResource {
    public function toArray(Request $request): array {
        return [
            'id'         => $this->id,
            'name'       => $this->name,
            'email'      => $this->email,
            'created_at' => $this->created_at->toISOString(),
        ];
    }
}

return new UserResource($user);

Versioning from day one

Version your API from the start, even if you only have v1. It costs nothing upfront and saves you from breaking clients later. Use URI versioning: `/api/v1/users`, `/api/v2/users`.

Authentication with Sanctum or Passport

Use Laravel Sanctum for SPA/mobile token auth. Use Laravel Passport only if you need a full OAuth2 server (third-party clients). Sanctum covers 90% of use cases with far less overhead.

Rate limiting and throttling

php
// routes/api.php
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
    Route::apiResource('users', UserController::class);
});

// Custom throttle: 10 requests/minute for expensive endpoints
Route::middleware('throttle:10,1')->post('/reports/generate', [ReportController::class, 'generate']);
  • Always validate input at the Form Request level,never in the controller
  • Return consistent error responses with RFC 7807 Problem Details format
  • Log all 4xx/5xx responses for monitoring
  • Use ETag headers for cache-friendly GET endpoints

Need help with this topic? REST API Design

Discover this service