andreaselia/laravel-api-to-postman
Generate a Postman collection automatically from your Laravel API routes. Export to storage/app with an artisan command, configurable output, optional FormRequest scaffolding and rules, and support for Bearer tokens or Basic Auth for routes behind auth middleware.
Installation:
composer require andreaselia/laravel-api-to-postman
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider"
api-postman.php config file to config/.First Run:
php artisan export:postman
postman.json) in storage/app.First Use Case:
API Documentation Sync:
export:postman in your CI pipeline (e.g., post-deploy) to auto-update Postman collections.# GitHub Actions
- name: Export Postman Collection
run: php artisan export:postman --bearer="${{ secrets.API_TOKEN }}"
Protected Endpoint Testing:
php artisan export:postman --bearer="1|abc123" --basic="user:pass"
FormRequest Scaffolding:
enable_formdata in api-postman.php to auto-generate request bodies from validation rules.
'enable_formdata' => true,
'formdata_format' => 'raw', // or 'human'
{
"body": {
"mode": "raw",
"raw": {
"language": "json",
"data": {
"email": "required|email",
"password": "required|min:8"
}
}
}
}
CRUD Folder Structuring:
/users, /orders) using group_by_resource:
'group_by_resource' => true,
/Users
- GET /users
- POST /users
- GET /users/{id}
/Orders
- GET /orders
...
Environment-Specific Exports:
config/api-postman-staging.php) and use:
php artisan export:postman --config=api-postman-staging
Laravel Events:
// In a service provider
Route::macro('exportPostman', function () {
Artisan::call('export:postman');
});
Custom Metadata:
x-api-version):
// In a service provider
PostmanGenerator::extend(function ($collection) {
$collection->setInfo('version', 'v2.0.0');
});
Exclude Routes:
ignore_routes in config:
'ignore_routes' => [
'admin.*',
'webhook.*',
],
Postman Variables:
{{base_url}} in Postman and set it dynamically:
php artisan export:postman --base-url="https://api.staging.example.com"
Auth Precedence:
--bearer and --basic are provided, Bearer auth takes precedence.--basic alone for Basic Auth-only collections.FormRequest Limitations:
POST/PUT routes using Laravel’s FormRequest class.enable_formdata: false.Route Caching:
php artisan route:cache) may cause stale exports.php artisan route:clear before exporting or disable caching temporarily.Nested Resources:
/users/{user}/posts/{post}) may generate unclear folder structures.group_by_resource: false or customize the folder_name callback in config.Special Characters in URIs:
/user/profile/{name}) may break Postman imports.uri_encoding: 'strict' in config.Large APIs:
split_by_folder: true.Verbose Output:
api-postman.php:
'debug' => true,
storage/logs/laravel.log.Validate JSON:
Test with curl:
curl -X GET http://your-api.com/users -H "Authorization: Bearer 1|abc123"
Custom Collection Info:
info object:
// In a service provider
PostmanGenerator::extend(function ($collection) {
$collection->setInfo('name', 'My API - Staging');
$collection->setInfo('description', 'Staging environment for My API');
});
Dynamic Auth Tokens:
php artisan export:postman --bearer="$STAGING_API_TOKEN"
Post-Processing:
$collection = json_decode(file_get_contents(storage_path('app/postman.json')), true);
// Add custom fields...
file_put_contents(storage_path('app/postman.json'), json_encode($collection));
Custom Folder Names:
'folder_name' => function ($route) {
return str_replace('api/', '', $route->getPrefix());
},
Event Listeners:
postman.export event to modify the collection:
Event::listen('postman.export', function ($collection) {
$collection->addFolder('Custom', [
// Add custom requests...
]);
});
Versioned Collections:
'filename' => 'postman-' . date('Y-m-d') . '.json',
Postman Environment Variables:
{
"url": "{{base_url}}/users",
"header": [
{ "key": "Authorization", "value": "{{token}}" }
]
}
API Contract Testing:
public function test_postman_collection()
{
$collection = Artisan::call('export:postman', [], $output);
$this->assertJson($output);
}
Git LFS for Large Collections:
Postman Monitor Integration:
php artisan export:postman --base-url="https://api.production.example.com"
How can I help you explore Laravel packages today?