# Enums as API Resources API Platform provides support for PHP 8.1+ `BackedEnum`s, allowing them to be exposed as first-class API resources. This enables clients to discover available enum cases and their associated metadata directly through your API. ## Exposing BackedEnums To expose a `BackedEnum` as an API resource, simply apply the `#[ApiResource]` attribute to your enum class: ```php name; } } ``` #### Adding Custom Properties You can add custom properties to your enum resource by defining public methods and marking them with `#[ApiProperty]`: ```php 'Article is not ready for public consumption', self::PUBLISHED => 'Article is publicly available', self::ARCHIVED => 'Article content is outdated or superseded', }; } } ``` With the above, `GET /statuses/0` might return: ```json { "name": "DRAFT", "value": 0, "description": "Article is not ready for public consumption" } ``` #### Custom State Providers For more advanced customization, you can implement custom state providers for your enum resources. A common pattern is to use a trait to provide common functionality: ```php value; } public function getValue(): int|string { return $this->value; } public static function getCases(): array { return self::cases(); } public static function getCase(Operation $operation, array $uriVariables): ?BackedEnum { $id = is_numeric($uriVariables['id']) ? (int) $uriVariables['id'] : $uriVariables['id']; return array_reduce(self::cases(), static fn($c, BackedEnum $case) => $case->name === $id || $case->value === $id ? $case : $c, null); } } ``` Then, apply the trait and specify the providers in your enum: ```php