Copied!
Laravel

How to Generate Postman Collection Automatically in Laravel

generate-postman-collection-automatically-in-laravel
Shahroz Javed
Nov 06, 2025 . 15 views

Table Of Contents

 

Managing API documentation manually in Postman can be a repetitive task. With the yasin_tgh/laravel-postman package, you can automate Postman collection generation directly from your Laravel routes — saving time and ensuring accuracy with every code update.

Introduction

This guide shows how to install, configure, and generate a Postman collection automatically in Laravel. It’s a perfect solution for teams working with REST APIs, microservices, or client integrations.

⚠️ Note: Make sure your API routes are properly defined before running the generator. Only registered routes will be included in the collection.

Installation

First, ensure your Laravel project includes the API scaffolding. If you haven’t set it up yet, run:

php artisan install:api

Next, install the Laravel Postman package (development dependency only):

composer require --dev yasin_tgh/laravel-postman

Publish the configuration file:

php artisan vendor:publish --provider="YasinTgh\LaravelPostman\PostmanServiceProvider" --tag="postman-config"

Generate Postman Collection

After setup, simply run the artisan command to generate your Postman collection file:

php artisan postman:generate

The generated Postman collection file will be saved in:

/storage/postman/api_collection.json

Configuration Options

All configuration options are defined in config/postman.php. You can customize how routes are grouped, filtered, and authenticated in your generated documentation.

Route Organization

Define how your API routes appear inside Postman. You can group them by URI prefix, nested path, or controller name.

Route Filtering

Control which routes are included or excluded from the generated collection. Useful when you want to skip web routes or private endpoints.

Authentication Setup

Configure authentication for your Postman documentation. The package supports bearer, basic, and api_key types.

💡 Related: Learn more about Laravel API Authentication with Sanctum.

Output Configuration

Define where to store the generated Postman file and how to name it. You can use environment variables to set storage driver, directory, and filename.

Example Configuration File

Here’s the default configuration file published at config/postman.php. You can adjust it as needed:

<?php

return [
    'name' => env('APP_NAME', 'Laravel API'),
    'description' => env('API_DESCRIPTION', 'API Documentation'),
    'base_url' => env('APP_URL', 'http://localhost'),

    'routes' => [
        'prefix' => 'api',
        'include' => [
            'patterns' => [],
            'middleware' => [],
            'controllers' => [],
        ],
        'exclude' => [
            'patterns' => [],
            'middleware' => [],
            'controllers' => [],
        ],
    ],

    'structure' => [
        'folders' => [
            'strategy' => 'nested_path',
            'max_depth' => 5,
            'mapping' => [],
        ],
        'naming_format' => '[{method}] {uri}',
        'requests' => [
            'default_body_type' => 'raw',
        ]
    ],

    'auth' => [
        'enabled' => true,
        'type' => 'bearer',
        'location' => 'header',
        'default' => [
            'token' => 'your-access-token',
            'username' => 'user@example.com',
            'password' => 'password',
            'key_name' => 'X-API-KEY',
            'key_value' => 'your-api-key-here',
        ],
        'protected_middleware' => ['auth:api'],
    ],

    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ],

    'output' => [
        'driver' => env('POSTMAN_STORAGE_DISK', 'local'),
        'path' => env('POSTMAN_STORAGE_DIR', storage_path('postman')),
        'filename' => env('POSTMAN_STORAGE_FILE', 'api_collection'),
    ],
];

Result

After generation, import the file storage/postman/api_collection.json into Postman. You’ll see all your Laravel API routes organized, documented, and ready for testing — no manual setup required.

Conclusion

With yasin_tgh/laravel-postman, you can effortlessly keep your Postman collections up-to-date and consistent with your Laravel API routes. It’s a lightweight yet powerful tool for teams focusing on productivity and clean documentation.

14 Shares

Similar Posts