Turn any OpenAPI spec into dedicated Laravel artisan commands. Each endpoint gets its own command with typed options for path parameters, query parameters, and request bodies. Combined with Laravel Zero, this is a great way to build standalone CLI tools for any API that has an OpenAPI spec.
use Spatie\OpenApiCli\Facades\OpenApiCli;
OpenApiCli::register('https://api.bookstore.io/openapi.yaml', 'bookstore')
->baseUrl('https://api.bookstore.io')
->bearer(env('BOOKSTORE_TOKEN'))
->banner('Bookstore API v2')
->cache(ttl: 600)
->followRedirects()
->yamlOutput()
->showHtmlBody()
->useOperationIds()
->onError(function (Response $response, Command $command) {
return match ($response->status()) {
429 => $command->warn('Rate limited. Retry after '.$response->header('Retry-After').'s.'),
default => false,
};
});
For a spec with GET /books, POST /books, GET /books/{book_id}/reviews, and DELETE /books/{book_id}, you get these commands:
bookstore:get-books
bookstore:post-books
bookstore:get-books-reviews
bookstore:delete-books
bookstore:list
List all endpoints:
php artisan bookstore:list
Bookstore API v2
GET bookstore:get-books List all books
POST bookstore:post-books Add a new book
GET bookstore:get-books-reviews List reviews for a book
DELETE bookstore:delete-books Delete a book
Human-readable output (default):
php artisan bookstore:get-books --limit=2
# Data
| id | title | author |
|----|--------------------------|-----------------|
| 1 | The Great Gatsby | F. Fitzgerald |
| 2 | To Kill a Mockingbird | Harper Lee |
# Meta
total: 2
YAML output:
php artisan bookstore:get-books --limit=2 --yaml
data:
-
id: 1
title: 'The Great Gatsby'
author: 'F. Fitzgerald'
-
id: 2
title: 'To Kill a Mockingbird'
author: 'Harper Lee'
meta:
total: 2
##We got badges
