devnotes.

Building APIs with Laravel

Laravel is a popular choice for building JSON APIs consumed by mobile apps, SPAs, or third-party integrations. API routes live in routes/api.php and are automatically prefixed with /api.

API Resources shape your Eloquent models into clean, consistent JSON:

php artisan make:resource PostResource
class PostResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'author' => $this->user->name,
            'published_at' => $this->created_at->toDateString(),
        ];
    }
}
Route::apiResource('posts', PostController::class);

// In the controller:
public function index()
{
    return PostResource::collection(Post::with('user')->paginate(15));
}

Protect endpoints with Sanctum token authentication (->middleware('auth:sanctum')), and apply rate limiting with throttle:60,1. Laravel 13 adds native JSON:API-compliant resources, making it straightforward to expose APIs that follow the JSON:API specification (standardized pagination, relationships, and sparse fieldsets) without third-party packages.

Building REST APIs with Laravel | DevNotes