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
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
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
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
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Shift logo

Shift

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

Shift
Lucky Media logo

Lucky Media

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

Lucky Media
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm

The latest

View all →
Typed Translation Accessors in Laravel 13.15.0 image

Typed Translation Accessors in Laravel 13.15.0

Read article
Refresh Your Laravel Database Without Dropping Every Table image

Refresh Your Laravel Database Without Dropping Every Table

Read article
JSON Schema Deserialization in Laravel 13.14 image

JSON Schema Deserialization in Laravel 13.14

Read article
Generate Short, URL-Safe IDs From Numbers With Sqids image

Generate Short, URL-Safe IDs From Numbers With Sqids

Read article
Scheduler List: A Web Dashboard for Laravel's Scheduled Tasks image

Scheduler List: A Web Dashboard for Laravel's Scheduled Tasks

Read article
Community Laravel Extension for Zed image

Community Laravel Extension for Zed

Read article