# Reference # API Platform Versioning > **Versions.** Written for **API Platform 5.0** (Symfony 7.4 LTS / 8.x). It also > runs on **4.4**, the 4 to 5 bridge release (same features, plus the APIs that > 5.0 removed), except where flagged. **4.3** is the last release supporting > **Symfony 6.4 LTS**; **3.4** is unmaintained (legacy). Deltas are flagged > inline as **5.0**, **4.4+**, **4.3** or **3.4**. > **Official recommendation (v3 to 5.0): prefer DEPRECATION over versioning.** API Platform's guidance is to evolve a single API and deprecate fields/operations with a sunset window, rather than maintaining parallel `/v1`, `/v2` surfaces. Reach for path versioning only when a breaking representation change genuinely cannot be expressed through deprecation + groups. Start at the "Deprecation (recommended)" section below; the multi-version strategies after it are the fallback. ## Deprecation (recommended) ### `deprecationReason` Marks a resource, operation, or property as deprecated. Rendered in JSON-LD/Hydra, GraphQL, and OpenAPI (Swagger UI / GraphiQL): ```php '@1767225600', // RFC 9745: timestamp the resource became deprecated 'Link' => '; rel="successor-version"', ], )] class Parchment {} ``` > Both `deprecationReason` and `sunset` are stable v3→v4. The RFC 9745 `Deprecation` header support is **v4-era**. (For pre-v4 codebases, emit `Deprecation`/`Sunset`/`Link` from a kernel response listener — see the listener pattern in the multi-version section.) --- ## Path versioning (fallback strategy) Only when deprecation cannot express the change. The patterns below maintain multiple representations in parallel. ## Versioning Strategies ### 1. URI Versioning ```php ['product:read:v1']], )] // Version 2 - same entity, different representation #[ApiResource( uriTemplate: '/v2/products', shortName: 'ProductV2', operations: [ new GetCollection(uriTemplate: '/v2/products'), new Get(uriTemplate: '/v2/products/{id}'), ], normalizationContext: ['groups' => ['product:read:v2']], )] class Product { #[Groups(['product:read:v1', 'product:read:v2'])] private ?int $id = null; #[Groups(['product:read:v1', 'product:read:v2'])] private string $name; // V1: price in cents as integer #[Groups(['product:read:v1'])] private int $price; // V2: price as Money object #[Groups(['product:read:v2'])] private Money $priceAmount; // V2 only: new field #[Groups(['product:read:v2'])] private ?string $sku = null; } ``` ### 2. Separate DTOs per Version ```php itemProvider->provide($operation, $uriVariables, $context); if (!$product) { return null; } $request = $this->requestStack->getCurrentRequest(); $version = $request?->headers->get('X-API-Version', 'v2'); return match ($version) { 'v1' => $this->transformToV1($product), 'v2' => $this->transformToV2($product), default => $this->transformToV2($product), }; } private function transformToV1(Product $product): ProductOutputV1 { return new ProductOutputV1( id: $product->getId(), name: $product->getName(), price: $product->getPrice(), ); } private function transformToV2(Product $product): ProductOutputV2 { return new ProductOutputV2( id: $product->getId(), name: $product->getName(), price: [ 'amount' => $product->getPrice(), 'currency' => 'EUR', ], sku: $product->getSku(), metadata: $product->getMetadata(), ); } } ``` ## Deprecation on a versioned endpoint ### Mark Deprecated Operations ```php use ApiPlatform\OpenApi\Model; #[ApiResource( operations: [ // Deprecated v1 endpoint new Get( uriTemplate: '/v1/products/{id}', deprecationReason: 'Use /v2/products/{id} instead. Will be removed in 2026.', sunset: '2026-12-31', // v4: typed OpenApi\Model\Operation (openapiContext array is deprecated in v4) openapi: new Model\Operation(deprecated: true), ), // Current v2 endpoint new Get( uriTemplate: '/v2/products/{id}', ), ], )] class Product { /* ... */ } ``` ### Sunset / Deprecation headers via a response listener (pre-v4 fallback) On v4, prefer the operation `headers` option (RFC 9745, shown in "Deprecation (recommended)"). On older versions, emit the headers from a listener: ```php 'onResponse', ]; } public function onResponse(ResponseEvent $event): void { $request = $event->getRequest(); $path = $request->getPathInfo(); // Add sunset header for v1 endpoints if (str_starts_with($path, '/api/v1/')) { $response = $event->getResponse(); $response->headers->set('Sunset', 'Sat, 01 Jan 2025 00:00:00 GMT'); // RFC 9745: an @-prefixed Unix timestamp, not 'true' $response->headers->set('Deprecation', '@1704067200'); $response->headers->set( 'Link', '; rel="successor-version"' ); } } } ``` ## Migration Guide Pattern ```php 1999]); $response = static::createClient()->request('GET', '/api/v1/products/' . $product->getId()); $this->assertResponseIsSuccessful(); $data = $response->toArray(); // V1 format: price as integer $this->assertIsInt($data['price']); $this->assertEquals(1999, $data['price']); } public function testV2ReturnsNewFormat(): void { $product = ProductFactory::createOne(['price' => 1999]); $response = static::createClient()->request('GET', '/api/v2/products/' . $product->getId()); $this->assertResponseIsSuccessful(); $data = $response->toArray(); // V2 format: price as object $this->assertIsArray($data['price']); $this->assertEquals(1999, $data['price']['amount']); $this->assertEquals('EUR', $data['price']['currency']); } ``` ## Best Practices 1. **Deprecation first** - path versioning only for breaking changes that deprecation and groups cannot express 2. **Groups for minor changes** - add fields without new version 3. **Set sunset dates** - give consumers time to migrate 4. **Document changes** - changelog per version 5. **Test all versions** - maintain test coverage 6. **Limit active versions** - max 2-3 at a time ## Validation commands - php bin/console debug:router - php bin/console api:openapi:export - ./vendor/bin/phpunit --filter=Version