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.
Install the package:
composer require andreaselia/laravel-api-to-postman
Publish the config:
php artisan vendor:publish --provider="AndreasElia\PostmanGenerator\PostmanGeneratorServiceProvider"
This creates config/api-postman.php in your project.
Run the generator (basic usage):
php artisan export:postman
Outputs a Postman collection JSON file to storage/app/.
Import into Postman:
For a Laravel API with routes like:
Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);
Run:
php artisan export:postman --bearer="your_token_here"
This generates a Postman collection with:
/users (no auth if route is public)./users (with Bearer token for protected routes).# .github/workflows/generate-postman.yml
name: Generate Postman Collection
on: [push]
jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- run: composer install -n --prefer-dist
- run: php artisan export:postman --bearer="${{ secrets.API_TOKEN }}"
- uses: actions/upload-artifact@v3
with:
name: postman-collection
path: storage/app/*.json
Use Case: Auto-generate collections on every main branch push, attach as an artifact for QA teams.
php artisan export:postman --bearer="1|abc123xyz456"
auth_type to bearer in api-postman.php.php artisan export:postman --bearer="$BEARER_TOKEN"
php artisan export:postman --basic="admin:password123"
auth_type to basic.'form_request' => [
'enabled' => true,
'format' => 'human', // or 'raw'
],
Route::post('/users', [UserController::class, 'store'])
->middleware('auth:sanctum');
Output: Postman request body includes:
{
"name": "required|string|max:255",
"email": "required|email|unique:users"
}
Use Case: Reduces manual request body setup for API consumers.
// config/api-postman.php
'group_by_prefix' => true,
Example Output:
π /api/v1
βββ π GET /users
βββ π POST /users
βββ π /admin
βββ π GET /stats
// config/api-postman.php
'exclude' => [
'admin.*', // Regex pattern
'webhook.*',
],
Use Case: Exclude internal routes (e.g., webhooks) from public collections.
Auth Mismatch Errors
auth:sanctum may fail if --bearer is omitted.default_auth in config:
'default_auth' => [
'type' => 'bearer',
'token' => env('POSTMAN_BEARER_TOKEN'),
],
FormRequest Rules Not Showing
FormRequest classes may not appear.form_request.enabled = true in config.FormRequest (not manual validation).Path Parameters Dropped
/users/{id} may lose {id} in Postman.config/api-postman.php:
'path_parameters' => true,
Collection Merging Bugs
export:postman multiple times may duplicate entries.--force to overwrite or set continue_on_errors = true.Inspect Generated JSON
php artisan export:postman --debug
Outputs raw JSON to storage/logs/postman-debug.json.
Validate Postman Schema Use Postmanβs Schema Validator to check for malformed entries.
Check Route Registration Run:
php artisan route:list
Ensure your API routes are registered before export.
Customize Request Descriptions
Override the description field in config/api-postman.php:
'description_template' => 'API Endpoint: {{ route }} | Method: {{ method }}',
Add Headers/Variables
Extend the PostmanGenerator class to inject custom headers:
// app/Providers/PostmanServiceProvider.php
public function boot()
{
$generator = app(AndreasElia\PostmanGenerator\PostmanGenerator::class);
$generator->addHeader('X-Custom-Header', 'value');
}
Support for OpenAPI Workaround: Use the generated Postman collection as a reference to build OpenAPI specs manually or via tools like Postman to OpenAPI converters.
Dynamic Environment Variables
Use Laravelβs env() in config:
'auth' => [
'type' => 'bearer',
'token' => env('POSTMAN_DYNAMIC_TOKEN', 'default_token'),
],
storage/app/postman-collection.json) to track API changes over time.--bearer flag with environment variables to switch between dev/prod tokens.laravel-shift/doctrine to auto-generate changelogs for API consumers.form_request and group_by_prefix to speed up generation.How can I help you explore Laravel packages today?