Skip to content
A glittering, brightly colored logo A glittering, brightly colored logo

Build type-safe REST APIs

PSX provides a set of PHP components to build type-safe APIs with automatic OpenAPI and SDK generation

OpenAPI generation

Instead of adding many attributes to each controller our components build the specification based on your controller arguments and DTO classes.

DTO generation

We provide a console command to automatically generate DTOs based on a TypeSchema specification, which helps to easily generate DTOs to describe your API.

SDK generation

We provide an advanced SDK generator which allows you to automatically generate client SDKs i.e. for TypeScript or PHP.

Framework independent

Our components are framework independent, we support Symfony and Laravel and you can easily integrate them into your custom framework.

To give you a first impression, the following code shows a simple CRUD controller of our Symfony integration:

src/Controller/PostController.php
final class PostController extends AbstractController
{
public function __construct(private PostService $service)
{
}
#[Route('/post', methods: ['GET'])]
public function getAll(#[Query] ?string $filter): PostCollection
{
return $this->service->findAll($filter);
}
#[Route('/post/{id}', methods: ['GET'])]
public function get(#[Param] int $id): Post
{
return $this->service->find($id);
}
#[Route('/post', methods: ['POST'])]
public function create(#[Body] Post $payload): Message
{
return $this->service->create($payload);
}
#[Route('/post/{id}', methods: ['PUT'])]
public function update(#[Param] int $id, #[Body] Post $payload): Message
{
return $this->service->update($id, $payload);
}
#[Route('/post/{id}', methods: ['DELETE'])]
public function delete(#[Param] int $id): Message
{
return $this->service->delete($id);
}
}

The Query, Param and Body attributes are PSX specific and describe values from the HTTP request. With PSX the idea is that your controller never interacts with a raw request object instead the controller defines all HTTP parameter as argument. Through this it is possible to combine OpenAPI/SDK generation with the actual controller code.

The code above is enough to automatically generate an OpenAPI specification or TypeScript SDK which can be used at any frontend for type-safe communication s.

php bin/console generate:sdk spec-openapi
php bin/console generate:sdk client-typescript
TypeScript
const client = new Client('https://api.acme.com', new HttpBearer('my_token'));
const collection = await client.post().getAll();
const post = await client.post().get(1);
const message = await client.post().create({title: 'My new post'});
const message = await client.post().update(2, {title: 'My updated post'});
const message = await client.post().delete(2);

To describe the request and response data in your controller you need to build DTOs. You can do this manually but we also provide a powerful TypeSchema based DTO generator which helps with this task.

php bin/console generate:model
TypeSchema
{
"definitions": {
"Post": {
"type": "struct",
"properties": {
"title": {
"type": "string"
}
}
}
},
"root": "Post"
}

For more information you can start by choosing one of our integration options.

Symfony Laravel Custom