Skip to main content

Overview

The Laravel OpenAPI Generator provides a clean programmatic API for generating OpenAPI specifications, Postman collections, and Insomnia workspaces. You can use either the facade or inject the service directly.

Facade

The package registers a facade for convenient access:

Facade Accessor

The facade resolves to the Ronu\OpenApiGenerator\OpenApiGenerator service class. Location: src/Facades/OpenApiGenerator.php:10

Service Class: OpenApiGenerator

The main service class provides four public methods for generating specifications. Location: src/OpenApiGenerator.php

generate()

Generate a specification in any supported format. Signature:
Parameters:
bool
default:"true"
Enable or disable cache for generation. When true, results are cached based on the cache configuration.
array|null
default:"null"
Filter by specific API types. Pass an array of type keys (e.g., ['api', 'mobile']). When null, all enabled API types are included.
string|null
default:"null"
Environment name to apply (e.g., 'artisan', 'local', 'production'). When null, uses the default environment from configuration.
string
default:"'openapi'"
Output format: 'openapi', 'postman', or 'insomnia'.
Returns: array - The generated specification structure. Example:

generateOpenApi()

Convenience method for generating OpenAPI 3.0.3 specifications. Signature:
Parameters:
bool
default:"true"
Enable or disable cache for generation.
array|null
default:"null"
Filter by specific API types.
string|null
default:"null"
Environment name to apply.
Returns: array - OpenAPI 3.0.3 specification. Implementation: This method internally calls generate() with format = 'openapi'. Location: src/OpenApiGenerator.php:22-28 Example:

generatePostman()

Generate a Postman Collection (v2.1 format). Signature:
Parameters:
bool
default:"true"
Enable or disable cache for generation.
array|null
default:"null"
Filter by specific API types.
string|null
default:"null"
Environment name to apply for base URL and variables.
Returns: array - Postman Collection JSON structure. Implementation: This method internally calls generate() with format = 'postman'. Location: src/OpenApiGenerator.php:30-36 Example:

generateInsomnia()

Generate an Insomnia Workspace (v4 format). Signature:
Parameters:
bool
default:"true"
Enable or disable cache for generation.
array|null
default:"null"
Filter by specific API types.
string|null
default:"null"
Environment name to apply for base URL and variables.
Returns: array - Insomnia Workspace JSON structure. Implementation: This method internally calls generate() with format = 'insomnia'. Location: src/OpenApiGenerator.php:38-44 Example:

Dependency Injection

You can also inject the service directly into your controllers or services:

Service Provider

The package is auto-discovered via Laravel’s package discovery. Provider: Ronu\OpenApiGenerator\Providers\OpenApiGeneratorServiceProvider Responsibilities:
  • Registers OpenApiServices and OpenApiGenerator as singletons
  • Publishes configuration files
  • Registers Artisan commands
  • Registers HTTP routes (if enabled)
Configuration Publishing:

HTTP Controller (Optional)

If HTTP routes are enabled (openapi.routes.enabled = true), the package provides a controller for web access. Controller: Ronu\OpenApiGenerator\Controllers\OpenApiController Available HTTP Endpoints: Query Parameters:
  • api_type - Comma-separated list of API types
  • environment - Environment name
Example HTTP Usage:

Return Value Structure

OpenAPI Format

Returns an OpenAPI 3.0.3 compliant specification:
string
OpenAPI version (3.0.3)
object
API metadata (title, version, description, contact, license)
array
Array of server objects with URLs and descriptions
object
All API endpoints with operations (GET, POST, etc.)
array
Resource grouping tags
object
Reusable schemas and security definitions

Postman Format

Returns a Postman Collection v2.1 structure with:
  • Collection metadata
  • Folders grouped by module
  • Pre-request scripts for token management
  • Tests for response validation
  • Variable references

Insomnia Format

Returns an Insomnia v4 workspace with:
  • Workspace metadata
  • Request groups
  • Multiple environments (base + artisan + local + production)
  • Request chaining via response variables
  • Automated tests

Error Handling

All methods may throw the following exceptions:
See the Exceptions page for detailed error handling documentation.
Common Exceptions:
  • InvalidArgumentException - Invalid API type or environment name
  • Exception - General generation errors (invalid configuration, missing templates)
Example with Error Handling:

Cache Management

The service respects the cache configuration in config/openapi.php:
Clearing Cache Programmatically: The OpenApiServices class provides a cache clearing method:
Clearing Cache via CLI:

Artisan Commands

CLI command reference

Configuration

Configure API types and environments

Exceptions

Error handling guide

HTTP Routes

Configure web endpoints