API Platform up and running in 5 minutes 🚀
Tags: symfony, php, api, tutorial
Published: 2023-07-07 18:08:47

[API Platform](https://api-platform.com/) is a framework for API-first projects, built on top of Symfony components. Let's see how to create a minimal and lightweight starter project in just 5 minutes!

The only requirements are [Docker Desktop](https://www.docker.com/products/docker-desktop), [Visual Studio Code](https://code.visualstudio.com/) with the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) installed.

## Setup

Download [Symfony Sail](https://github.com/gremo/symfony-sail/releases/latest/download/symfony-sail-p.zip) (disclaimer, I'm the author 😊) which provides a ready-to-go developement environment for Symfony.

1. Extract the zip in a directory `api-platform-playground`
2. Create an empty `.env.local` in that folder
3. Start Visual Studio Code, type F1 and choose *Dev Containers: Reopen in Container*

From now on, every command must be run inside Visual Studio Code built-in terminal, that is **inside the container** itself.

Let's download and install **Symfony skeleton**:

```bash
curl -O https://raw.githubusercontent.com/symfony/skeleton/6.2/composer.json
composer install
```

We need to add two **required libraries** in order to be able to navigate API Platform documentation:

```bash
composer require symfony/asset symfony/twig-pack
```

And finally the **API Platform** package:

```bash
composer require api-platform/core
```

Your API documentation is available at [http://localhost/api/docs](http://localhost/api/docs) 🎉🎉🎉

## Your first resource

Time to add the first resource! We want to **validate** the input data and **persist** our resource. And being lazy, we take the advantage of [MakerBundle](https://symfony.com/bundles/SymfonyMakerBundle/current/index.html) also:

> **Do not** execute the Docker recipe as we already have Docker configuration!

```bash
composer require symfony/validator symfony/orm-pack symfony/maker-bundle
```

Wait a moment, what about the database? Thanks to [Symfony Sail](https://github.com/gremo/symfony-sail), we already have a running database instance as `db:3306`. Let's set the `DATABASE_URL` in the `.env.local` file:

```env
DATABASE_URL="mysql://root:@db:3306/db_name?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
```

Now we can finally generate our first resource named `BlogPost`:

```bash
php bin/console make:entity BlogPost --api-resource
```

Using the wizard, add a required `title` property of `64` character length, then modify `App\Entity\BlogPost` as follows:

```php
<?php

namespace App\Entity;

// ...
use Symfony\Component\Validator\Constraints as Assert;

// ...
class BlogPost
{
    // ...

    // ...
    #[Assert\NotBlank]
    #[Assert\Length(max: 64)]
    private ?string $title = null;
}
```

Almost done! Generate a migration and execute it:

```bash
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate -n
```

Finish! 😉

Test a simple POST request using the API Platform page at [http://localhost/api/docs](http://localhost/api/docs), with a simple payload like `{ title: "My first blog post!" }`.

**Bonus**: static analysis with PHPStan and formatting upon saving using PHP_CodeSniffer? 🤓

Easy:

> **Do** install the recipes of these libraries.

```bash
composer require --dev phpstan/phpstan squizlabs/php_codesniffer
```