Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Laravel OpenAPI CLI: Generate Artisan Commands from Your API Spec

Published on by

Laravel OpenAPI CLI: Generate Artisan Commands from Your API Spec image

Spatie has released Laravel OpenAPI CLI, a package that converts OpenAPI specifications into dedicated Laravel Artisan commands. Each API endpoint in your spec gets its own command, with typed options for path and query parameters, and request bodies.

How It Works

After installing, you register an OpenAPI spec URL using the OpenApiCli facade — typically in your AppServiceProvider:

namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
use Spatie\OpenApiCli\Facades\OpenApiCli;
 
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
OpenApiCli::register('https://api.acme.com/openapi.yaml', 'acme')
->baseUrl('https://api.acme.com')
->bearer(env('ACME_API_TOKEN'));
}
}

Given a spec that defines GET /orders, POST /orders, and DELETE /orders/{order_id}, the package generates the following Artisan commands:

  • acme:get-orders
  • acme:post-orders
  • acme:delete-orders
  • acme:list

The acme:list command displays all available endpoints and their descriptions.

Commands follow the pattern {prefix}:{http-method}-{path}, where the prefix is the name given during registration (the 2nd parameter of the register method), and the path is converted to kebab-case with path parameters stripped out. So GET /orders/{order_id}/items becomes acme:get-orders-items. If your spec defines operation IDs, you can use those instead via ->useOperationIds().

Running Commands

You can call any generated command directly from the terminal:

php artisan acme:get-orders --limit=10

Responses render as human-readable tables by default. Pass the --yaml flag for YAML output instead:

php artisan acme:get-orders --limit=10 --yaml

Path and Query Parameters

Path parameters are required options, and query parameters are optional. Both are converted to kebab-case — so order_id and orderId both become --order-id:

# Path parameter
php artisan acme:get-orders-items --order-id=42
 
# Multiple path parameters
php artisan acme:delete-customers-orders --customer-id=1 --order-id=7
 
# Query parameters
php artisan acme:get-orders --status=pending --limit=10

Bracket notation in query strings is also flattened to kebab-case, so filter[status] becomes --filter-status.

Sending Data

For endpoints that accept a request body, you can send fields individually using --field:

php artisan acme:post-orders --field customer_id=1 --field status="pending"

Fields are sent as JSON by default, unless the spec specifies application/x-www-form-urlencoded. If you need to send raw JSON, use --input instead:

php artisan acme:post-orders --input '{"customer_id":1,"shipping":{"address":"123 Main St","city":"New York"}}'

Note that --field and --input cannot be combined. For file uploads, prefix the field value with @:

php artisan acme:post-orders-attachments --order-id=67 --field file=@/path/to/invoice.pdf

When any field contains a file, the request is sent as multipart/form-data.

Configuration Options

The registration API also supports several options through a fluent interface:

OpenApiCli::register('https://api.acme.com/openapi.yaml', 'acme')
->baseUrl('https://api.acme.com')
->bearer(env('ACME_API_TOKEN'))
->banner('Acme Orders API v2')
->cache(ttl: 600)
->followRedirects()
->yamlOutput()
->showHtmlBody()
->useOperationIds()
->onError(function (Response $response, Command $command) {
return match ($response->status()) {
429 => $command->warn('Rate limited...'),
default => false,
};
});

A few notable options:

  • cache(ttl: 600) — Caches responses for the specified number of seconds
  • followRedirects() — Automatically follows HTTP redirects
  • useOperationIds() — Names commands using the operation IDs from your spec rather than the HTTP method and path
  • onError() — Accepts a callback for handling specific HTTP error status codes

Installation

Install via Composer:

composer require spatie/laravel-openapi-cli

This package also integrates well with Laravel Zero, a framework for building standalone CLI applications.

Full documentation is available on Spatie's docs site, and you can view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Improved Skill and Guideline Detection in Laravel Boost v2.2.0 image

Improved Skill and Guideline Detection in Laravel Boost v2.2.0

Read article
"The Vibes" — NativePHP Hosts a Day 3 after Laracon US image

"The Vibes" — NativePHP Hosts a Day 3 after Laracon US

Read article
What We Know About Laravel 13 image

What We Know About Laravel 13

Read article
New Colors Added in Tailwind CSS v4.2 image

New Colors Added in Tailwind CSS v4.2

Read article
Factory makeMany() Method in Laravel 12.52.0 image

Factory makeMany() Method in Laravel 12.52.0

Read article
Laravel Adds an Official Svelte + Inertia Starter Kit image

Laravel Adds an Official Svelte + Inertia Starter Kit

Read article