Laravel Packages

Laravel OpenAPI CLI: Generate Artisan Commands from Your API Spec

Published
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.

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article