Building robust, high-throughput RESTful JSON APIs is one of Laravel's greatest strengths. Whether you are powering native iOS/Android mobile applications, frontend Single Page Applications (Next.js, React, Vue), or third-party webhooks, Laravel provides a dedicated stateless routing file (routes/api.php), API resource controllers, and standardized JSON exception handling.
Unlike traditional web controllers that return HTML views (and thus need create and edit form actions), API controllers only require 5 actions: index, store, show, update, and destroy:
php artisan make:controller Api/V1/ArticleController --api --model=Article
Notice the --api flag! It automatically omits the unnecessary HTML form actions. Register the API route in routes/api.php with versioning:
use App\Http\Controllers\Api\V1\ArticleController;
Route::prefix('v1')->group(function () {
Route::apiResource('articles', ArticleController::class);
});
A professional REST API must never return a generic 200 OK for errors. Adhere strictly to industry standard HTTP response codes:
| Status Code | Meaning | Standard API Scenario |
|---|---|---|
200 OK |
Success | Successful GET retrieval or record update |
201 Created |
Resource Created | Successful POST creation of a new database record |
204 No Content |
Success with Empty Body | Successful DELETE removal of a record |
400 Bad Request |
Malformed Request | Missing mandatory headers or malformed JSON syntax |
401 Unauthorized |
Unauthenticated | Missing or expired Bearer API token |
403 Forbidden |
Unauthorized Access | Authenticated user lacks permissions to edit resource |
404 Not Found |
Resource Not Found | Queried model ID or slug does not exist |
422 Unprocessable |
Validation Failure | Input fields failed business validation rules |
429 Too Many Requests |
Rate Limited | Client exceeded their API throttle threshold |
500 Server Error |
Internal Error | Uncaught server exception or database failure |
Here is a complete, production-grade API controller action:
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Http\Requests\StoreArticleRequest;
use App\Http\Resources\ArticleResource;
use Illuminate\Http\JsonResponse;
class ArticleController extends Controller
{
public function store(StoreArticleRequest $request): JsonResponse
{
$article = $request->user()->articles()->create($request->validated());
return (new ArticleResource($article))
->response()
->setStatusCode(201);
}
public function destroy(Article $article): JsonResponse
{
$this->authorize('delete', $article);
$article->delete();
return response()->json(null, 204);
}
}
Broaden your backend architecture, REST API design, and enterprise production engineering expertise:
Transform Eloquent models into clean, decoupled JSON representations.
Secure REST endpoints with personal access tokens and SPA cookie auth.
Implement robust query parameter parsing and performant cursor pagination.
Generate automatic interactive OpenAPI documentation for your endpoints.