How Use Postman Api Laravel Passport
Integrating Laravel Passport with Postman for API Testing
Postman is a powerful tool for API testing and development. Laravel Passport is a robust OAuth2 server implementation that adds authentication to your Laravel applications. Combining these two can streamline your API testing process.
1. Setting up Laravel Passport
-
Install Laravel Passport:
Terminal window composer require laravel/passport -
Publish Passport configuration:
Terminal window php artisan vendor:publish --provider="Laravel\Passport\PassportServiceProvider" -
Generate Passport keys:
Terminal window php artisan passport:install -
Configure your authentication middleware: In your
App\Http\Kernel.php
, add the following to the$middleware
array:protected $middleware = [// ...\Illuminate\Session\Middleware\StartSession::class,\Illuminate\View\Middleware\ShareErrorsFromSession::class,\Illuminate\Routing\Middleware\SubstituteBindings::class,\Illuminate\Auth\Middleware\Authenticate::class,\Illuminate\Session\Middleware\AuthenticateSession::class,\Illuminate\Routing\Middleware\ThrottleRequests::class,\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,]; -
Create a user (if you don’t have one):
Terminal window php artisan tinker>>> App\User::create(['name' => 'Test User','email' => 'test@example.com','password' => bcrypt('password'),]);>>> exit
2. Configuring Postman
-
Create a new Postman Collection: This will help organize your API requests.
-
Add a new request:
- Name: Give your request a descriptive name.
- Method: Choose the HTTP method (GET, POST, PUT, DELETE, etc.)
- URL: Enter your API endpoint URL (e.g.,
http://localhost:8000/api/users
)
-
Authorization Tab:
- Select OAuth 2.0 from the drop-down menu.
-
OAuth 2.0 Configuration:
- Grant Type:
password
- Token Name:
access_token
- Token URL:
http://localhost:8000/oauth/token
(replace with your app’s URL) - Client ID: The client ID you created in your Laravel Passport setup
- Client Secret: The client secret you created in your Laravel Passport setup
- Scope:
*
(for all scopes) or a specific scope if you’ve defined them in your Laravel Passport configuration.
- Grant Type:
-
Request Headers:
- Authorization:
Bearer {{access_token}}
- Authorization:
-
Body:
- If your API endpoint requires data, add the necessary parameters in the Body section.
3. Obtaining an Access Token
-
Create a new request to
http://localhost:8000/oauth/token
(your token URL):- Method:
POST
- Headers:
Content-Type
:application/x-www-form-urlencoded
- Body:
- grant_type:
password
- client_id: Your Laravel Passport client ID
- client_secret: Your Laravel Passport client secret
- username:
test@example.com
(your test user’s email) - password:
password
(your test user’s password)
- grant_type:
- Method:
-
Send the request: You’ll receive a response containing the access token.
-
Save the access token:
- Click on the Authorization tab of your request.
- Select Get New Access Token.
- In the Token field, paste your retrieved access token.
- Save the token: Click the save button.
4. Testing API Endpoints with your Access Token
Now you can use the saved access token to test your protected API endpoints:
-
Set the Authorization header: In your Postman request, ensure the Authorization header is set to
Bearer {{access_token}}
. -
Send requests: Send requests to your API endpoints as usual. Your access token will authenticate them.
Example: Fetching User Data
API Endpoint (Laravel):
Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user();});
Postman Request:
- Method:
GET
- URL:
http://localhost:8000/api/user
- Authorization:
Bearer {{access_token}}
Postman Response:
{ "id": 1, "name": "Test User", "email": "test@example.com", "email_verified_at": null, "created_at": "2023-04-20T14:17:30.000000Z", "updated_at": "2023-04-20T14:17:30.000000Z"}
5. Using Environment Variables for Secure Credentials
To keep your sensitive information (client ID, client secret) secure, consider using Postman’s environment variables:
-
Create an environment:
- Go to the Postman Environments section.
- Click on Add Environment.
- Name your environment (e.g.,
LaravelAPI
).
-
Add variables:
- Add variables for your
client_id
andclient_secret
. - Set the variable values to match your Laravel Passport credentials.
- Add variables for your
-
Use environment variables in your requests:
- Instead of hardcoding the credentials in your Postman requests, replace them with the environment variable names (e.g.,
{{client_id}}
,{{client_secret}}
).
- Instead of hardcoding the credentials in your Postman requests, replace them with the environment variable names (e.g.,
-
Select the environment:
- Before sending requests, ensure you’ve selected the
LaravelAPI
Environment in the top-right corner of Postman.
- Before sending requests, ensure you’ve selected the
Conclusion
By combining the power of Postman and Laravel Passport, you can seamlessly test your API endpoints while maintaining secure authentication. Follow these steps to get started and create a smooth testing workflow.