Skip to main content
This guide demonstrates practical scenarios for using the Laravel OpenAPI Generator in production environments.

Scenario 1: Generate Admin + Mobile Docs from CLI

Goal: Produce OpenAPI + Postman + Insomnia outputs only for admin and mobile APIs.

Setup

Define API types in config/openapi.php:
Ensure the output directory is writable:

Execution

1

Publish configuration

This creates config/openapi.php where you can customize API types, routes, and environments.
2

Run the generator

Expected output:

Notes

  • Filenames include the filtered API type suffix (admin-mobile)
  • The generator validates that API types are enabled in config
  • Both Postman and Insomnia files include embedded environment variables

Common Mistakes

Using disabled or missing API typesIf you request an API type that’s disabled or not defined:
You’ll get an error:
Solution: Check config/openapi.php and ensure the API type is defined and enabled.
Forgetting to publish configWithout publishing config, the package uses defaults which may not match your route structure. Always publish config files:

Scenario 2: Serve Docs Over HTTP for Internal Tooling

Goal: Allow internal consumers (mobile apps, frontend teams, partner APIs) to fetch documentation via HTTP routes.

Setup

Enable routes in config/openapi.php:

Execution

Returns the complete OpenAPI 3.0.3 specification.

Notes

  • HTTP endpoints use the same generation logic as the CLI
  • Documentation is generated on-demand (not served from static files)
  • Query parameters apply the same filters as CLI flags

Common Mistakes

Middleware blocking accessIf you add authentication middleware:
Unauthenticated requests return 401:
Solution: Either remove the middleware for public docs or include authentication headers:
Invalid format parameterThe {format} parameter only accepts: json, yaml, yml, postman, insomnia.

Scenario 3: Custom Endpoint Documentation

Goal: Document a non-CRUD endpoint like api-apps.rotate with custom descriptions and request fields.

Setup

Add custom endpoint configuration in config/openapi-docs.php:

Execution

1

Define route with name

Ensure your route has a name matching the custom endpoint key:
2

Regenerate documentation

The generator matches route names to custom endpoint keys and applies the custom documentation.
3

Verify in OpenAPI output

Open storage/app/public/openapi/openapi.json and find your endpoint:

Notes

  • Use the entity.action pattern for keys
  • Custom endpoints override template-generated documentation
  • request_fields are merged with FormRequest validation rules

Common Mistakes

Key doesn’t match route nameIf you use a key that doesn’t match the route name:
The custom documentation won’t be applied. Always use the exact route name:

Scenario 4: Template-Driven CRUD Documentation

Goal: Standardize list/show/create/update/delete descriptions using JSON templates to avoid repetitive manual documentation.

Setup

1

Publish templates

This creates:
  • resources/openapi/templates/generic/list.json
  • resources/openapi/templates/generic/show.json
  • resources/openapi/templates/generic/create.json
  • resources/openapi/templates/generic/update.json
  • resources/openapi/templates/generic/delete.json
2

Enable template system

In config/openapi-templates.php:
3

Customize templates

Edit resources/openapi/templates/generic/list.json:

Execution

Generate documentation:
The generator applies templates to all CRUD routes:
  • GET /users → uses list.json
  • GET /users/{id} → uses show.json
  • POST /users → uses create.json
  • PUT /users/{id} → uses update.json
  • DELETE /users/{id} → uses delete.json
Placeholders like {{entity_plural}} are replaced with users, {{entity_singular}} with user.

Notes

  • Templates reduce repetitive documentation
  • The generator falls back to metadata extraction when templates are disabled
  • Custom endpoint documentation overrides template output

Common Mistakes

JSON templates not foundIf template files are missing or paths are wrong:
Solution: Verify template paths in config/openapi-templates.php match actual file locations.
Invalid JSON syntaxInvalid JSON in templates causes parsing errors:
Solution: Validate JSON using jsonlint.com or your IDE.

Scenario 5: Generate Postman Environments

Goal: Export Postman environments for local development, staging, and production to use in API testing.

Setup

Define environments in config/openapi.php:

Execution

Creates:
  • postman-all.json - Main collection
  • postman-env-artisan.json
  • postman-env-local.json
  • postman-env-production.json

Import to Postman

1

Import collection

  1. Open Postman
  2. Click File > Import
  3. Select postman-all.json
2

Import environments

  1. Click Environments in left sidebar
  2. Click Import
  3. Select all postman-env-*.json files
3

Select environment

  1. Click the environment dropdown (top right)
  2. Select artisan, local, or production
  3. All requests now use that environment’s base URL and variables

Notes

  • The environment generator merges base + tracking variables into each sub-environment
  • Tracking variables appear in all environments with empty default values
  • You can set actual token values in Postman’s environment editor

Common Mistakes

Defining tracking variables in sub-environmentsDon’t repeat tracking variables in each sub-environment:
Tracking variables are automatically merged into all sub-environments.

Scenario 6: CI/CD Pipeline Integration

Goal: Automatically generate and publish documentation on every deployment.

GitHub Actions Example

GitLab CI Example

Notes

  • Always use --no-cache in CI/CD to ensure fresh output
  • Consider using --all to generate all formats
  • Upload to S3, CDN, or documentation hosting platform

Scenario 7: Team Workflow for API Development

Goal: Establish a workflow where backend developers generate docs, and frontend/mobile teams consume them.

Workflow

1

Developer adds new endpoint

Backend developer creates a new route with FormRequest validation:
2

Generate documentation locally

Developer reviews the generated docs to ensure accuracy.
3

Commit generated files

Add documentation files to version control:
4

CI/CD publishes docs

On merge to main, CI pipeline generates and publishes docs to a shared location:
5

Frontend/mobile teams consume docs

Teams import the latest Postman collection or view the OpenAPI spec in Swagger UI.

Best Practices

Tips for teams:
  • Keep docs in version control: Commit generated files so teams can see changes in PRs
  • Automate generation: Run generation in CI/CD to ensure docs are always up-to-date
  • Use custom endpoints: Document complex actions that don’t fit CRUD templates
  • Review generated output: Check that FormRequest rules and model properties are accurately reflected

Next Steps

Edge Cases

Learn how to handle config caching issues, concurrent generation, large route sets, and multi-tenant deployments.

API Reference

Explore the programmatic API for advanced integration scenarios.