andreaselia/laravel-api-to-postman
Auto-generate a Postman collection from your Laravel API routes. Supports Postman schema v2.1, configurable output, bearer token or basic auth for protected routes, and optional scaffolding of FormRequest rules for POST/PUT endpoints.
Installation:
composer require andreaselia/laravel-api-to-postman
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider"
api-postman.php config file to config/.First Export:
php artisan export:postman
storage/app/ (default: YYYY_MM_DD_HHMMSS_postman.json).Auth-Enabled Export (for protected APIs):
php artisan export:postman --bearer="your_token_here"
config/api-postman.php
export_path, group_routes, enable_formdata, validation_format, continue_on_errors.group_routes to organize routes by middleware (e.g., auth, api).POST/PUT routes, ensure they extend Illuminate\Foundation\Http\FormRequest to auto-generate request bodies.export_route_parameters to include route parameters (e.g., {id}) as path variables.Generate a Postman collection to:
--bearer).POST/PUT routes using FormRequest validation rules.auth:sanctum, api) for clarity.Develop API:
// routes/api.php
Route::get('/users', [UserController::class, 'index'])->middleware('auth:sanctum');
Route::post('/users', [UserController::class, 'store']);
FormRequest for validation:
// app/Http/Requests/StoreUserRequest.php
public function rules() {
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|string|min:8',
];
}
Configure Export:
// config/api-postman.php
return [
'export_path' => storage_path('app/postman'),
'group_routes' => true, // Group by middleware (e.g., "auth:sanctum")
'enable_formdata' => true, // Scaffold request bodies
'validation_format' => 'human', // Display rules in readable format
'continue_on_errors' => false, // Stop on first error (default)
'export_route_parameters' => true, // Include {id} as path variables
];
Generate Collection:
php artisan export:postman --bearer="1|abc123xyz" --basic="admin:password123"
Integrate with CI/CD:
Add to composer.json scripts:
"scripts": {
"post-install-cmd": [
"php artisan export:postman --bearer=\"${BEARER_TOKEN}\""
]
}
storage/app/postman).Generate Collection:
php artisan export:postman --bearer="test_token_123"
Import into Postman:
Automate with Newman:
npm install -g newman
newman run storage/app/postman/YYYY_MM_DD_HHMMSS_postman.json --reporters cli,json
Customize Request Bodies:
// app/Providers/PostmanGeneratorServiceProvider.php
use AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider as BaseProvider;
class PostmanGeneratorServiceProvider extends BaseProvider {
public function register() {
$this->app->singleton('postman-generator', function ($app) {
$generator = new \AndreasElia\PostmanGenerator\PostmanGenerator();
$generator->setCustomRequestBodyFormatter(
function ($request, $rules) {
// Custom logic to format request bodies
return ['custom' => 'formatted', 'data' => $rules];
}
);
return $generator;
});
}
}
Add Environment Variables:
env config option to dynamically set variables:
// config/api-postman.php
'env' => [
'base_url' => env('API_BASE_URL', 'http://localhost:8000'),
],
Handle Errors Gracefully:
continue_on_errors to true to skip problematic routes:
// config/api-postman.php
'continue_on_errors' => true,
Laravel Sanctum:
php artisan export:postman --bearer="1|$(php artisan sanctum:token)"
Laravel Passport:
TOKEN=$(curl -X POST http://localhost:8000/oauth/token -d "grant_type=password&client_id=1&client_secret=secret&username=user&password=pass" | jq -r '.access_token')
php artisan export:postman --bearer="$TOKEN"
API Resources:
apiResources config to document API responses:
// config/api-postman.php
'api_resources' => [
'users' => [
'description' => 'User resource',
'schema' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
],
],
],
],
Webhooks:
// config/api-postman.php
'exclude_routes' => [
'webhook.*',
],
Route Parameter Conflicts:
export_route_parameters is true, ensure your Postman collection uses path variables (e.g., /users/{id}) instead of query parameters.export_route_parameters if your API uses query params for dynamic segments.FormRequest Issues:
enable_formdata is true but request bodies aren’t generated, check:
FormRequest class extends Illuminate\Foundation\Http\FormRequest.rules() method is properly defined.FormRequest class is correctly imported and used in your controller.Auth Overrides:
Middleware Grouping:
group_routes is true, routes are grouped by middleware (e.g., auth:sanctum). This may create unexpected folder structures in Postman.group_routes or customize the grouping logic in the service provider.Serialized Closures:
containsSerializedClosure workaround from the package’s source.Validation Format:
validation_format (human or raw) affects how rules are displayed in Postman descriptions.human for readability in documentation; use raw for programmatic use.Check Route Collection:
php artisan route:list
Enable Verbose Output:
-v for debugging:
php artisan export:post
How can I help you explore Laravel packages today?