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 :{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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article
PIE 1.5 Changes PHP Extension Installs in CI image

PIE 1.5 Changes PHP Extension Installs in CI

Read article
Delete Expired Sessions in Laravel With a Scheduled Command image

Delete Expired Sessions in Laravel With a Scheduled Command

Read article