---
title: Swagger Authenticate the API in Laravel – Jaeves Codelab
description: In previous article we Create Swagger Post Method in Laravel and Create Swagger GET Method in Laravel. Now we will do Swagger Authenticate the API in Larav
---

[Start Coding with Codelab | Jaeves AI](https://jaeves.com/codelab)

# [Swagger Authenticate the API in Laravel – Jaeves Codelab](https://jaeves.com/codelab/swagger-authenticate-the-api-in-laravel)

 Written by [Admin](https://jaeves.com/codelab/author/admin) | Oct 2, 2023, 10:53:42 PM

In previous article we [Create Swagger Post Method in Laravel](https://jaeves.com/codelab/swagger-post-method-in-laravel/) and [Create Swagger GET Method in Laravel](https://jaeves.com/codelab/swagger-get-method-in-laravel/). Now we will do Swagger Authenticate the API in Laravel.

Open config/l5-swagger.php file and add securityDefinitions and securitySchemes. Check [Create Swagger Configuration in Laravel](https://jaeves.com/codelab/create-swagger-configuration-in-laravel/).

```
'securityDefinitions' => [
            'securitySchemes' => [
                /*
                 * Examples of Security schemes
                */
                'Authentication_Token' => [ // Unique name of security
                    'type' => 'apiKey', // The type of the security scheme. Valid values are "basic", "apiKey" or "oauth2".
                    'description' => 'An authorization header. Example: Token',
                    'name' => 'Authorization', // The name of the header or query parameter to be used.
                    'in' => 'header', // The location of the API key. Valid values are "query" or "header".
                ],
            ]
        ]
```

Now, add security=} comment in functions. If required We can also access the token in header Authorization.

```
/**
     * @OA\Get(
     *      path="/get-user/{id}",
     *      operationId="getUser",
     *      tags={"User"},
     *      summary="Get User",
     *      description="Get User",
     *      security=},
     *      @OA\Parameter(
     *      name="id",
     *      in="path",
     *      required=true,
     *      description= "user id",
     *      example="10",
     *      @OA\Schema(
     *           type="integer"
     *      )
     *      ),
     *       @OA\Response(
     *      response=200,
     *      description="Success response",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="200"),
     *      @OA\Property(property="user", type="string", example="{'full_name':'Chetan','email_id':'chetan1234@gmail.com','created_at':'2022-05-27T07:16:57.000000Z','updated_at':'2022-05-27T07:16:57.000000Z'}"),
     *        )
     *     ),
     *        @OA\Response(
     *      response=400,
     *      description="Bad Request",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="400"),
     *      @OA\Property(property="message", type="string", example="Error in processing request")
     *        )
     *     )
     * )
     *      
     * )
     */

    public function getUser($id)
    {
    	try {
	    	$user = User::where('id', $id)->get();

	    	if($user){
	    		return response()->json(['status' => 200, 'user' => $user], 200);
	    	}
    	}catch (\Exception $e) {
            return response()->json(['status' => 400, 'message' => 'Error in processing request'], 400);
        }
    }
```

In routes/api.php add middleware auth:api.

```
Route::group(['middleware' => ['auth:api']], function () {

});
```

Now, run php artisan l5-swagger:generate and check api at https://mywebsite.com/api/documentation.

For more [code examples click here](https://jaeves.com/codelab/create-swagger-configuration-in-laravel/). Read [laravel swagger documentation here](https://github.com/DarkaOnLine/L5-Swagger).

[View full post](https://jaeves.com/codelab/swagger-authenticate-the-api-in-laravel)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Admin"
  },
  "dateModified" : "2023-12-20T12:18:58.483Z",
  "datePublished" : "2023-10-02T22:53:42Z",
  "headline" : "Swagger Authenticate the API in Laravel",
  "image" : {
    "@type" : "ImageObject",
    "height" : 60,
    "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
    "width" : 60
  },
  "mainEntityOfPage" : "https://jaeves.com/codelab/swagger-authenticate-the-api-in-laravel",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60.0,
      "url" : "https://139767802.fs1.hubspotusercontent-eu1.net/hubfs/139767802/logos/jaeves-logo-230907.png",
      "width" : 185.625
    },
    "name" : "Codelab"
  }
}
```