Getting started

Quick start

Document a first endpoint from a model, a form request, a resource and a controller.

This page documents two endpoints of a book shop, POST /api/books and GET /api/books/{book}, without writing a line of OpenAPI. Everything below is ordinary Laravel code.

Write the endpoint

/**
 * @property int $id
 * @property string $title
 * @property float $price
 * @property \Illuminate\Support\Carbon|null $published_at
 */
final class Book extends Model
{
    protected $guarded = [];

    protected function casts(): array
    {
        return ['price' => 'float', 'published_at' => 'datetime'];
    }
}

Generate the document

Terminal
php artisan openapi:generate
  v1 ............................................................ openapi/openapi-v1.json

Read the result

The form request gives the body, the return type gives the response, the middleware gives the security:

StoreBookRequest and BookController::store()
/**
 * Add a book.
 *
 * The price is in euros.
 */
public function store(StoreBookRequest $request): BookResource

// StoreBookRequest::rules()
'title' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'tags' => 'array',
'tags.*' => 'string|distinct',
paths./api/books.post
{
  "tags": ["Book"],
  "summary": "Add a book.",
  "description": "The price is in euros.",
  "operationId": "POST:books.store",
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": ["title", "price"],
          "properties": {
            "title": { "type": "string", "maxLength": 255 },
            "price": { "type": "number", "minimum": 0 },
            "tags": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
          }
        }
      }
    }
  },
  "responses": {
    "201": {
      "description": "Created",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "required": ["data"],
            "properties": { "data": { "$ref": "#/components/schemas/Book" } }
          }
        }
      }
    },
    "401": { "$ref": "#/components/responses/Unauthenticated" },
    "422": { "$ref": "#/components/responses/ValidationFailed" }
  },
  "security": [{ "sanctum": [] }, { "session": [], "xsrf": [] }]
}

The resource was run on a sampled Book: its @property tags and casts give the type of each value. Every key of toArray() is required, because none is computed with when() or whenLoaded():

BookResource::toArray()
return [
    'id' => $this->id,
    'title' => $this->title,
    'price' => $this->price,
    'published_at' => $this->published_at,
];
components.schemas.Book
{
  "type": "object",
  "required": ["id", "title", "price", "published_at"],
  "properties": {
    "id": { "type": "integer", "example": 1 },
    "title": { "type": "string", "example": "string" },
    "price": { "type": "number", "example": 1.5 },
    "published_at": { "type": "string", "format": "date-time", "example": "2024-01-01T00:00:00.000000Z" }
  }
}

GET /api/books/{book} gets a 200 with the same Book, an integer path parameter book (the key of the bound model) and a 404, because the model may not exist.

Check one route

Terminal
php artisan openapi:inspect books.store

It shows the operation the generator built for that route and every diagnostic about it. Use it whenever an endpoint does not look as expected; see Diagnostics.

Next