Building RESTful APIs
REST (Representational State Transfer) is the architectural style most Laravel APIs follow. It maps naturally onto Laravel's routing and resource controllers, giving each type of data a consistent, predictable set of endpoints.
Core REST Conventions
A RESTful resource is typically exposed through a small, standard set of HTTP verbs and URIs rather than one-off custom endpoints for every action.
| Verb | URI | Action |
|---|---|---|
| GET | /posts | List all posts |
| POST | /posts | Create a new post |
| GET | /posts/{id} | Show a single post |
| PUT/PATCH | /posts/{id} | Update a post |
| DELETE | /posts/{id} | Delete a post |
Wiring It Up in routes/api.php
Laravel maps this whole set with a single line using apiResource, which registers every route above except the ones that return HTML views.
Route::apiResource('posts', PostController::class);
Keeping Responses Consistent
- Always return JSON with an appropriate status code (200, 201, 204, 404, 422, etc.)
- Use a consistent response envelope across endpoints
- Version the API (e.g. /api/v1/) so future changes don't break existing clients
Once the routes and controllers are in place, the next step is controlling exactly what shape of JSON gets sent back — which is where API Resources come in.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
