Output formatting | 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)  Basic-usage  Output formatting

 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)

 Output formatting
=================

###  On this page

1. [ Human-readable (default) ](#content-human-readable-default)
2. [ JSON output ](#content-json-output)
3. [ YAML output ](#content-yaml-output)
4. [ Minified output ](#content-minified-output)
5. [ YAML as default ](#content-yaml-as-default)
6. [ JSON as default ](#content-json-as-default)
7. [ Response headers ](#content-response-headers)
8. [ HTML responses ](#content-html-responses)
9. [ Syntax highlighting ](#content-syntax-highlighting)

Human-readable (default)
----------------------------------------------------------------------------------------------------------------------------

```
php artisan bookstore:get-books
# # Data
#
# | ID | Title             |
# |----|-------------------|
# | 1  | The Great Gatsby  |
#
# # Meta
#
# Total: 1
```

JSON responses are automatically converted into readable markdown-style output: tables for arrays of objects, key-value lines for simple objects, and section headings for wrapper patterns like `{"data": [...], "meta": {...}}`.

JSON output
-----------------------------------------------------------------------------------------

```
php artisan bookstore:get-books --json
# {
#     "data": [
#         { "id": 1, "title": "The Great Gatsby" }
#     ]
# }
```

The `--json` flag outputs raw JSON (pretty-printed by default).

YAML output
-----------------------------------------------------------------------------------------

```
php artisan bookstore:get-books --yaml
# data:
#   -
#     id: 1
#     title: 'The Great Gatsby'
```

The `--yaml` flag converts JSON responses to YAML. If `--json` or `--minify` is also passed, those take priority.

Minified output
-----------------------------------------------------------------------------------------------------

```
php artisan bookstore:get-books --minify
# {"data":[{"id":1,"title":"The Great Gatsby"}]}
```

The `--minify` flag implies `--json` — no need to pass both.

YAML as default
-----------------------------------------------------------------------------------------------------

If you prefer YAML output as the default for a specific registration, use the `yamlOutput()` config method:

```
OpenApiCli::register(base_path('openapi/api.yaml'), 'api')
    ->baseUrl('https://api.example.com')
    ->yamlOutput();
```

With `yamlOutput()`, commands output YAML by default. The `--json` and `--minify` flags override this and produce JSON output instead.

JSON as default
-----------------------------------------------------------------------------------------------------

If you prefer JSON output as the default for a specific registration, use the `jsonOutput()` config method:

```
OpenApiCli::register(base_path('openapi/api.yaml'), 'api')
    ->baseUrl('https://api.example.com')
    ->jsonOutput();
```

With `jsonOutput()`, commands output pretty-printed JSON by default. The `--json` and `--minify` flags still work as expected.

Response headers
--------------------------------------------------------------------------------------------------------

```
php artisan bookstore:get-books --headers
# HTTP/1.1 200 OK
# Content-Type: application/json
# X-RateLimit-Remaining: 99
#
# # Data
#
# | ID | Title             |
# |----|-------------------|
# | 1  | The Great Gatsby  |
```

HTML responses
--------------------------------------------------------------------------------------------------

When an API returns HTML (e.g., an error page), the body is hidden by default to avoid flooding the terminal. You'll see a hint instead:

```
Response is not JSON (content-type: text/html, status: 500, content-length: 1234)

Use --output-html to see the full response body.
```

Pass `--output-html` to show the body:

```
php artisan bookstore:get-books --output-html
```

To always show HTML bodies for a specific registration, use the `showHtmlBody()` config method:

```
OpenApiCli::register(base_path('openapi/api.yaml'), 'api')
    ->baseUrl('https://api.example.com')
    ->showHtmlBody();
```

Non-HTML non-JSON responses (e.g., `text/plain`) are always shown in full.

Syntax highlighting
-----------------------------------------------------------------------------------------------------------------

JSON and human-readable output are syntax-highlighted by default when running in a terminal. JSON output gets keyword/value coloring via [tempest/highlight](https://github.com/tempestphp/highlight), and human-readable output gets colored headings, keys, and table formatting.

To disable highlighting (e.g. when piping output), use the built-in `--no-ansi` flag:

```
php artisan bookstore:get-books --no-ansi
php artisan bookstore:get-books --json --no-ansi
```

Highlighting is automatically disabled when output is not a TTY (e.g. piped to a file or another command).
