Guide
API
Endpoints through API providers, a clear JSON contract, and access rules in `Api.yaml`.
Register endpoints through providers
Register application API endpoints through ApiEndpointProviderInterface, not frontend routing. This keeps registration aligned with OpenAPI and API authorization.
<?php
declare(strict_types=1);
namespace App\Api;
use Lemonade\Framework\Api\Endpoint\ApiAccess;
use Lemonade\Framework\Api\Endpoint\ApiEndpointProviderInterface;
use Lemonade\Framework\Api\Endpoint\ApiEndpointRegistry;
final class AppApiEndpointProvider implements ApiEndpointProviderInterface
{
public function register(ApiEndpointRegistry $registry): void
{
$registry->get(
'/articles',
'App\\Controllers\\Api\\ArticleController@index',
'articles.index',
'List articles',
'Returns published articles.',
ApiAccess::Public,
);
}
}Return predictable JSON
Keep the response shape explicit. Validate input before building the payload and use json() with a clear, stable contract.
return $this->json([
'ok' => true,
'data' => [
'id' => $article['id'],
'title' => $article['title'],
],
]);Protect an endpoint
API access rules belong in app/Config/Api.yaml. Use bearer scopes for protected endpoints and allow public access only where it is intentional.
module: api
config:
enabled: true
prefix: /api
security:
static_bearer:
enabled: true
token:
$env: API_TOKEN
type: string
scopes:
- api:admin