Updated 24 July 2026
Laravel Swagger Integration is one of the best ways to generate interactive API documentation for your Laravel applications.
When building REST APIs with Laravel, maintaining accurate and up-to-date documentation is essential for developers and API consumers.
Use Swagger (OpenAPI) with the L5-Swagger package to automatically generate and maintain your API documentation.
In this tutorial, Learn to integrate L5-Swagger with Laravel, configure authentication, document APIs, and generate Swagger UI.
Before starting, make sure you have:
First, install the Swagger package along with JWT Authentication.
|
1 |
composer require darkaonline/l5-swagger tymon/jwt-auth |
Publish the JWT configuration file.
|
1 |
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" |
Laravel creates a new configuration file:
|
1 |
config/jwt.php |
Next, generate the JWT secret key.
|
1 |
php artisan jwt:secret |
This command automatically updates your .env file with a JWT secret.
Now publish the L5-Swagger configuration.
|
1 |
php artisan vendor:publish --provider="L5Swagger\L5SwaggerServiceProvider" |
After publishing, Laravel creates:
|
1 |
config/l5-swagger.php |
This configuration file allows you to customize:
Create a controller if it doesn’t already exist.
|
1 |
php artisan make:controller UserController |
Inside the controller, create a register() method.
Add Swagger annotations above the method.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * @OA\Post( * path="/api/register", * summary="Register User", * tags={"Authentication"}, * @OA\Response( * response=201, * description="User registered successfully" * ) * ) */ |
The method should:
Once Swagger scans this annotation, the endpoint automatically appears in the documentation.
Generate a login controller.
|
1 |
php artisan make:controller LoginController |
This controller will authenticate users and return a JWT token after successful login.
Document the login endpoint.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * @OA\Post( * path="/api/login", * summary="User Login", * tags={"Authentication"}, * @OA\Response( * response=200, * description="Login Successful" * ) * ) */ |
Inside the login method:
Example response:
|
1 2 3 |
{ "token":"your_access_token" } |
Add another method to return authenticated user details.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * @OA\Get( * path="/api/user", * summary="Get Logged-in User", * tags={"User"}, * security={{"bearerAuth":{}}}, * @OA\Response( * response=200, * description="Success" * ) * ) */ |
This endpoint requires a valid Bearer Token.
When users authorize in Swagger UI, they can directly test this API.
Open:
|
1 2 |
routes/api.php |
Register your routes.
|
1 2 3 4 5 |
Route::post('/register',[UserController::class,'register']); Route::post('/login',[LoginController::class,'login']); Route::middleware('auth:sanctum')->get('/user',[UserController::class,'getUserDetails']); |
Note: Choose one authentication system (JWT or Sanctum) and configure your middleware consistently.
Open your base controller or create a dedicated annotations file.
Add global API information.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * @OA\Info( * title="Laravel Swagger API", * version="1.0.0", * description="API Documentation" * ) * * @OA\SecurityScheme( * securityScheme="bearerAuth", * type="http", * scheme="bearer", * bearerFormat="JWT" * ) */ |
This information appears at the top of Swagger UI.
Generate the documentation by running:
|
1 |
php artisan l5-swagger:generate |
The package scans your controllers for OpenAPI annotations and generates the API documentation automatically.
Start your Laravel server.
|
1 |
php artisan serve |
Now open the following URL:
|
1 2 |
http://127.0.0.1:8000/api/documentation |
You will see an interactive Swagger dashboard containing:
You can even test APIs directly from the browser without using Postman.
For authenticated routes:
/login endpoint.|
1 2 |
Bearer YOUR_ACCESS_TOKEN |
Install packages:
|
1 |
composer require darkaonline/l5-swagger tymon/jwt-auth |
Publish Swagger config:
|
1 |
php artisan vendor:publish --provider="L5Swagger\L5SwaggerServiceProvider" |
Publish JWT config:
|
1 |
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider" |
Generate JWT secret:
|
1 |
php artisan jwt:secret |
Generate Swagger documentation:
|
1 |
php artisan l5-swagger:generate |
Run the application:
|
1 |
php artisan serve |
Integrating Swagger into Laravel makes API documentation simple, interactive, and always up to date.
L5-Swagger documents API endpoints using OpenAPI annotations. It also generates interactive API docs and lets you test APIs from Swagger UI.
This approach reduces manual documentation effort and improves collaboration across development teams.
Also you can hire laravel developers to customize this feature for your business needs.
Kindly explore our extensions.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.