Quick start
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'];
}
}
/**
* @mixin Book
*/
final class BookResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'price' => $this->price,
'published_at' => $this->published_at,
];
}
}
final class StoreBookRequest extends FormRequest
{
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'tags' => 'array',
'tags.*' => 'string|distinct',
];
}
}
final class BookController
{
/**
* Show one book.
*/
public function show(Book $book): BookResource
{
return new BookResource($book);
}
/**
* Add a book.
*
* The price is in euros.
*/
public function store(StoreBookRequest $request): BookResource
{
return new BookResource(Book::query()->create($request->validated()));
}
}
Route::middleware('auth:sanctum')->group(function () {
Route::post('books', [BookController::class, 'store'])
->name('books.store');
Route::get('books/{book}', [BookController::class, 'show'])
->name('books.show');
});
Generate the document
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:
/**
* 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',
{
"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():
return [
'id' => $this->id,
'title' => $this->title,
'price' => $this->price,
'published_at' => $this->published_at,
];
{
"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
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
- How it works: what is inferred and what you declare.
- Responses: collections, pagination, declared responses.
- Continuous integration: fail the build when the documentation degrades.