Chapter 12 of 15
JSON API.
Return consistent JSON responses or use the API module for metadata, OpenAPI, and authorization.
A simple JSON route
For a small endpoint, use a regular route and a controller with an injected Responses service. json() accepts an array and an optional HTTP status.
API controller
<?php
declare(strict_types=1);
use Lemonade\Framework\Http\Response\Responses;
use Psr\Http\Message\ResponseInterface;
final readonly class StatusController
{
public function __construct(private Responses $responses) {}
public function show(): ResponseInterface
{
return $this->responses->json(['data' => ['status' => 'ready']]);
}
}Set 422 for validation failure or 201 when creating a resource. Content-Type describes the request body; Accept describes the requested response format.
The API endpoint registry
Use the API module when you need a central endpoint registry, access policy, OpenAPI, and built-in docs. Register an endpoint provider in Api.yaml.
Endpoint provider
<?php
declare(strict_types=1);
$registry->get(
path: '/app/status',
controllerAction: ControllerAction::for(StatusController::class, 'show'),
name: 'app.status',
summary: 'Application status',
description: 'Returns application availability.',
access: ApiAccess::Public,
);Every endpoint still uses explicit ControllerAction::for() and declares public or protected access.