Skip to main content

Overview

The Laravel OpenAPI Generator does not include custom middleware classes. Instead, it provides configuration options to apply your existing Laravel middleware to the HTTP documentation routes.
HTTP routes are only available when openapi.routes.enabled is set to true in the configuration.

Configuration

Route Middleware

Configure middleware for the documentation routes in config/openapi.php:
Location: config/openapi.php

Common Middleware Patterns

Public Access (Default)

No authentication required - suitable for public APIs:

Authenticated Access (Sanctum)

Require authentication via Laravel Sanctum:
Usage: Users must provide a valid token to access documentation:

Session Authentication

Require authenticated session (for web-based access):

Admin-Only Access

Restrict access to admin users:
Example Admin Middleware:
Register in app/Http/Kernel.php:

IP Whitelist

Restrict access to specific IP addresses:
Example IP Whitelist Middleware:

Environment-Based Access

Allow access only in development environments:
Or use middleware:
Example Development-Only Middleware:

API Key Authentication

Require an API key in request headers:
Example API Key Middleware:
Usage:

Available Routes

All these routes respect the configured middleware: Route Configuration Location: Routes are registered in the service provider when routes.enabled is true.

Security Middleware Mapping

OpenAPI Security Schemes

The package also allows you to map middleware to OpenAPI security schemes for route documentation:
How It Works: When generating OpenAPI specs, routes with these middleware automatically include the appropriate security requirements in the documentation. Example: If a route has auth:sanctum middleware:
The OpenAPI spec will include:
Security Scheme Definitions: Define the schemes in your configuration:
Location: src/Services/OpenApiServices.php:1310-1327

Rate Limiting

Apply rate limiting to documentation routes:
Custom Rate Limiter:
Then in configuration:

CORS Configuration

If you need to allow cross-origin requests to documentation routes:
CORS Configuration:

Middleware Groups

Create a custom middleware group for documentation:
Use in configuration:

Disabling HTTP Routes

If you prefer to generate specifications only via Artisan commands and not expose HTTP endpoints:
This completely disables route registration. Use the Artisan command or Public API instead.

Testing Middleware

Feature Test Example


Best Practices

Production Environments:
  • Always require authentication for documentation routes in production
  • Consider IP whitelisting for internal tools
  • Apply rate limiting to prevent abuse
  • Use HTTPS for all documentation endpoints
Development Environments:
  • Use minimal middleware for easy access during development
  • Consider enabling routes only in non-production environments
  • Use .env variables to control route availability

HTTP Routes

Configure HTTP documentation endpoints

Artisan Commands

Generate docs via CLI

Security

Security best practices

Configuration

Full configuration reference