Postman is a commonly used Application Programming Interface (API) development Graphical User Interface (GUI) to help developers build and test APIs.
This package enables you to generate a Postman collection automatically on the basis of your API routes. It also provides basic configuration and support for bearer auth tokens for routes behind an auth middleware.
The generator works for the latest version of the Postman Schema at the time of publication (v2.1.0).
1. Installation
Install the package:
composer require andreaselia/laravel-api-to-postman
2. Publishing
Then Publish the config file:
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider" --tag="config"
3. Configuration
You can modify the api-postman.php
 config values:
structured
 – If you want folders to be generated based on route names.base_url
 – The base URL for all of your endpoints.auth_middleware
 – The middleware which wraps your authenticated API routes.
4. Usage
The output of the command being ran is your storage/app directory.
To use the command simply run:
php artisan export:postman
The following usage will generate routes with the bearer token specified.
php artisan export:postman --bearer=1|XXNKXXqJjfzG8XXSvXX1Q4pxxnkXmp8tT8TXXKXX
5. Examples
php artisan export:postman --bearer=123456789
Here is the code for example Routes.
use App\Http\Controllers\HomeController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Route::get('/', HomeController::class)->name('home');
Route::apiResource('posts', 'PostController');
Route::middleware('auth:api')->group(function () {
Route::post('/user', UserController::class)->name('user');
});
and here is the default output.
{
"variable": [{
"key": "base_url",
"value": "https:\/\/api.example.com\/"
}, {
"key": "token",
"value": "123456789"
}],
"info": {
"name": "2021_02_04_151948_postman",
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.1.0\/collection.json"
},
"item": [{
"name": "api",
"request": {
"method": "GET",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api",
"host": "{{base_url}}\/api"
}
}
}, {
"name": "api\/posts",
"request": {
"method": "GET",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts",
"host": "{{base_url}}\/api\/posts"
}
}
}, {
"name": "api\/posts",
"request": {
"method": "POST",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts",
"host": "{{base_url}}\/api\/posts"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "GET",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "PUT",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "PATCH",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "DELETE",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/user",
"request": {
"method": "POST",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}, {
"key": "Authorization",
"value": "Bearer {{token}}"
}],
"url": {
"raw": "{{base_url}}\/api\/user",
"host": "{{base_url}}\/api\/user"
}
}
}]
}
and here is the example of structured output.
{
"variable": [{
"key": "base_url",
"value": "https:\/\/api.example.com\/"
}, {
"key": "token",
"value": "123456789"
}],
"info": {
"name": "2021_02_04_155327_postman",
"schema": "https:\/\/schema.getpostman.com\/json\/collection\/v2.1.0\/collection.json"
},
"item": [{
"name": "home",
"item": [{
"name": "api",
"request": {
"method": "GET",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api",
"host": "{{base_url}}\/api"
}
}
}]
}, {
"name": "posts",
"item": [{
"name": "api\/posts",
"request": {
"method": "GET",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts",
"host": "{{base_url}}\/api\/posts"
}
}
}, {
"name": "api\/posts",
"request": {
"method": "POST",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts",
"host": "{{base_url}}\/api\/posts"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "GET",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "PUT",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "PATCH",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}, {
"name": "api\/posts\/{post}",
"request": {
"method": "DELETE",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}],
"url": {
"raw": "{{base_url}}\/api\/posts\/{post}",
"host": "{{base_url}}\/api\/posts\/{post}"
}
}
}]
}, {
"name": "user",
"item": [{
"name": "api\/user",
"request": {
"method": "POST",
"header": [{
"key": "Content-Type",
"value": "application\/json"
}, {
"key": "Authorization",
"value": "Bearer {{token}}"
}],
"url": {
"raw": "{{base_url}}\/api\/user",
"host": "{{base_url}}\/api\/user"
}
}
}]
}]
}
Leave a Review