This guide walks you through building a secure, RESTful API in Laravel 12.
Key Features
-
JWT Authentication → Token-based login system
-
Role & Permission Management → Using Spatie Laravel Permission
-
Clean RESTful Endpoints → Following Laravel conventions
API Endpoints
| Method | Endpoint | Purpose |
|---|---|---|
| GET | /api/roles | List all roles |
| POST | /api/roles | Create role |
| GET | /api/roles/{id} | Show role details |
| PUT | /api/roles/{id} | Update role |
| DELETE | /api/roles/{id} | Delete role |
| GET | /api/permissions | List permissions |
| POST | /api/permissions | Create permission |
Step 1: Install Spatie Laravel Permission
Publish the config & migration:
Step 2: Update User Model
app/Models/User.php
This enables users to be assigned roles and permissions.
Step 3: Create Controllers
Step 4: RoleController
app/Http/Controllers/Api/RoleController.php
Step 5: PermissionController
app/Http/Controllers/Api/PermissionController.php
Step 6: Define Routes
routes/api.php
๐ Using Route::apiResource ensures RESTful Laravel conventions.
Step 7: Test with Postman
Always send the JWT token in the header:
1. List Roles
2. Create Role
3. Show Role
4. Update Role
5. Delete Role
6. List Permissions
7. Create Permission
✅ Conclusion
You now have a Laravel 12 API with:
-
๐ JWT authentication
-
๐ฅ Spatie Permission roles & permissions
-
๐ ️ Clean RESTful controllers & routes
-
๐งช Postman-tested endpoints
This structure is scalable, secure, and production-ready.
Want the full source code?
Download the complete Laravel 12 JWT API Authentication example on my GitHub repo here.
Happy Coding!

