PHPackages                             coagus/php-api-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. coagus/php-api-builder

ActiveLibrary[API Development](/categories/api)

coagus/php-api-builder
======================

Build RESTful APIs in PHP in minutes. Define entities, get automatic CRUD, JWT auth, validation, and OpenAPI docs.

1.3.3(2mo ago)2212—6.1%MITPHPPHP ^8.4CI failing

Since Oct 17Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/coagus/php-api-builder)[ Packagist](https://packagist.org/packages/coagus/php-api-builder)[ Docs](https://github.com/coagus/php-api-builder)[ RSS](/packages/coagus-php-api-builder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (32)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/4875256723f4b66ada6a08fdfedb982b4c1fe867cf2e2cb3d8598b807ec39a28/68747470733a2f2f706f7365722e707567782e6f72672f636f616775732f7068702d6170692d6275696c6465722f762f737461626c65)](https://packagist.org/packages/coagus/php-api-builder)[![Total Downloads](https://camo.githubusercontent.com/8869aa0a5444b1b124209cca7be8fdd6bc35c716563faf5e6596361824c02dc5/68747470733a2f2f706f7365722e707567782e6f72672f636f616775732f7068702d6170692d6275696c6465722f646f776e6c6f616473)](https://packagist.org/packages/coagus/php-api-builder)[![License](https://camo.githubusercontent.com/9d3f31f7599398be23e05c532fd818d1e314d1315eabe1288fe8fc5ea0821353/68747470733a2f2f706f7365722e707567782e6f72672f636f616775732f7068702d6170692d6275696c6465722f6c6963656e7365)](https://packagist.org/packages/coagus/php-api-builder)[![Tests](https://github.com/coagus/php-api-builder/actions/workflows/release.yml/badge.svg)](https://github.com/coagus/php-api-builder/actions)[![PHP 8.4+](https://camo.githubusercontent.com/2aed50cc19486e0407775311c22f530383b2791ca08b8c9583ebb80cb5e364e7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e342532422d626c75652e737667)](https://www.php.net/)[![Docker](https://camo.githubusercontent.com/bd576d25bf8581259e6423c54d4d7c85894ab86c8e400118dc3d4567000dbeea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f636b65722d72656164792d3234393645442e737667)](https://hub.docker.com/r/coagus/php-api-builder)

PHP API Builder v2
==================

[](#php-api-builder-v2)

Build RESTful APIs in PHP in minutes. Define your entities, get CRUD automatically, and focus on your business logic.

```
#[Table('products')]
#[SoftDelete]
class Product extends Entity
{
    #[PrimaryKey]
    public private(set) int $id;

    #[Required, MaxLength(100)]
    public string $name { set => trim($value); }

    #[Required]
    public float $price {
        set {
            if ($value < 0) throw new \InvalidArgumentException('Price must be positive');
            $this->price = round($value, 2);
        }
    }

    #[Required, Email, Unique]
    public string $email { set => strtolower(trim($value)); }

    #[Hidden]
    public string $password { set => password_hash($value, PASSWORD_ARGON2ID); }

    #[BelongsTo(Category::class)]
    public int $categoryId;

    #[HasMany(Review::class)]
    public array $reviews;
}
```

That's it. You now have a fully functional API with `GET`, `POST`, `PUT`, `PATCH`, `DELETE` endpoints, pagination, filtering, sorting, validation, soft deletes, and relationships. No controllers, no routes to configure, no boilerplate.

Features
--------

[](#features)

- **Automatic CRUD** from entity definitions with zero configuration
- **Powerful ORM** with Active Record pattern, relationships, and 5-level Query Builder
- **PHP 8.4** property hooks, asymmetric visibility, typed properties, attributes as metadata
- **Multi-database** support via PDO (MySQL, PostgreSQL, SQLite)
- **JWT Authentication** with OAuth 2.1 security practices (short-lived tokens, refresh rotation, scopes)
- **Auto-generated OpenAPI/Swagger** documentation from your entity attributes
- **Validation via attributes** (`#[Required]`, `#[Email]`, `#[MaxLength]`, `#[Unique]`) -- no config files
- **Rate limiting** middleware with file-based storage -- no external dependencies
- **REST conventions** -- lowerCamelCase JSON keys, snake\_case query params, RFC 7807 errors
- **Security built-in** with OWASP headers, CORS, input sanitization, SQL injection protection
- **Docker-first** workflow -- start a project without PHP installed locally
- **CLI scaffolding** for entities, services, middleware, and tests
- **Error traceability** with request ID correlation across all layers
- **AI development skill** included -- install it and your AI assistant knows the library

Quick Start
-----------

[](#quick-start)

### With PHP installed

[](#with-php-installed)

1. Create a new project:

```
composer create-project coagus/php-api-builder-skeleton my-api
```

2. Initialize and start:

```
cd my-api && ./api init
```

### Without PHP (Docker only)

[](#without-php-docker-only)

1. Create your project directory:

```
mkdir my-api && cd my-api
```

2. Initialize the project:

```
docker run --rm -it -v $(pwd):/app coagus/php-api-builder init
```

3. Start the services:

```
docker compose up -d
```

4. Verify it works:

```
curl http://localhost:8080/api/v1/health
```

> **Running CLI commands without PHP:** Once `docker compose up -d` is running, enter the container and use the CLI from there:
>
> ```
> docker compose exec app bash
> php vendor/bin/api make:entity Product
> ```
>
>
>
> Alternatively, the `./api` wrapper auto-detects Docker and works without entering the container.

Try the Demo
------------

[](#try-the-demo)

Explore all library features with a ready-made Blog API demo:

1. Install the demo (after init + docker compose up):

```
./api demo:install
```

2. Open Swagger UI at `http://localhost:8080/api/v1/docs/swagger`
3. When done exploring, clean up:

```
./api demo:remove
```

The demo creates a complete Blog API with Users, Posts, Comments, and Tags -- showcasing entities, services, relationships, JWT auth, validation, rate limiting, middleware, and OpenAPI documentation.

Create Your First Entity
------------------------

[](#create-your-first-entity)

```
./api make:entity User --fields="name:string,email:string,password:string" --soft-delete
```

This generates `entities/User.php`:

```
