Introduction | laravel-openapi-cli | Spatie

 SPATIE

  Laravel OpenAPI CLI
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-openapi-cli](https://spatie.be/docs/laravel-openapi-cli/v1)  Introduction

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-openapi-cli/v1)

- [ Introduction ](https://spatie.be/docs/laravel-openapi-cli/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-openapi-cli/v1/support-us)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-openapi-cli/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-openapi-cli/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-openapi-cli/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-openapi-cli/v1/about-us)

Basic usage
-----------

- [ Registering an API ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/registering-an-api)
- [ Command naming ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/command-naming)
- [ Path &amp; query parameters ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/path-and-query-parameters)
- [ Sending data ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/sending-data)
- [ Listing endpoints ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/listing-endpoints)
- [ Authentication ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/authentication)
- [ Output formatting ](https://spatie.be/docs/laravel-openapi-cli/v1/basic-usage/output-formatting)

Advanced usage
--------------

- [ Error handling ](https://spatie.be/docs/laravel-openapi-cli/v1/advanced-usage/error-handling)
- [ Multiple APIs ](https://spatie.be/docs/laravel-openapi-cli/v1/advanced-usage/multiple-apis)
- [ Debugging ](https://spatie.be/docs/laravel-openapi-cli/v1/advanced-usage/debugging)
- [ Command reference ](https://spatie.be/docs/laravel-openapi-cli/v1/advanced-usage/command-reference)

 Laravel OpenAPI CLI
=====================

 Create Laravel commands for your OpenAPI specs

 [    Repository ](https://github.com/spatie/laravel-openapi-cli)

    762

    38

Introduction
------------

###  On this page

1. [ We got badges ](#content-we-got-badges)

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](https://laravel-zero.com), 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
-----------------------------------------------------------------------------------------------

[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-openapi-cli.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-openapi-cli)[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/spatie/laravel-openapi-cli/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/spatie/laravel-openapi-cli/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/spatie/laravel-openapi-cli/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/spatie/laravel-openapi-cli/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-openapi-cli.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-openapi-cli)
