Skip to content

OpenAPI & Clients

PAM HTTP knows every route you register, so it can describe them. $app->openApi() produces an OpenAPI 3.1 document and simple typed clients straight from the router, with no annotations to keep in sync.

Add metadata to a route with the PendingRoute methods:

index.php
$app->post('/users', [UserController::class, 'store'])
->name('users.store')
->summary('Create a user')
->tags(['Users'])
->input(StoreUserRequest::class)
->output(UserResource::class);
Method Effect on the operation
name() operationId. Without a name, PAM derives one from the method and path, such as get.users.id.
summary() summary.
tags() tags.
input() A required JSON requestBody referencing a component schema named after the class.
output() A JSON schema reference on the 200 response.

Route parameters become required path parameters. Every operation lists 200, 422 and 500 responses.

bin/openapi
$document = $app->openApi('Example API', '1.0.0');
file_put_contents(__DIR__ . '/../openapi.json', $document->toJson());

generate() returns the document as an array and toJson() as pretty-printed JSON. Paths are sorted, so the output is stable in version control.

use Pam\Api\OpenApi\ClientLanguage;
$typescript = $document->client(ClientLanguage::TypeScript);
$kotlin = $document->client(ClientLanguage::Kotlin);
$swift = $document->client(ClientLanguage::Swift);

The generated client is a thin class with one method per operation. You supply the transport. The TypeScript client looks like this:

client.ts
export class PamApiClient {
constructor(private readonly request: (method: string, path: string) => Promise<unknown>) {}
get_admin_me = () => this.request('GET', `/admin/me`);
delete_admin_posts_id = (id: string | number) => this.request('DELETE', `/admin/posts/${encodeURIComponent(String(id))}`);
}

ClientGenerator can also be used directly with any OpenAPI array: (new ClientGenerator($document))->generate($language).

Compare the committed document with the current one in CI:

bin/api-check
use Pam\Api\OpenApi\CompatibilityChecker;
$previous = json_decode(file_get_contents('openapi.json'), true, flags: JSON_THROW_ON_ERROR);
$changes = (new CompatibilityChecker())->breakingChanges($previous, $app->openApi()->generate());
foreach ($changes as $change) {
fwrite(STDERR, "[{$change->code->value}] {$change->location}: {$change->message}\n");
}
exit($changes === [] ? 0 : 1);

Each CompatibilityChange has a code, a location and a message. The checker currently reports removed paths (CompatibilityChangeCode::PathRemoved = 1) and removed operations (OperationRemoved = 2).